-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinsert_equations.py
37 lines (31 loc) · 972 Bytes
/
insert_equations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import urllib2
import re
import sys
RENDERER_URL = "http://latex.codecogs.com/png.latex"
def percent_encode(match):
return "![equation](" + RENDERER_URL + "?" + urllib2.quote(match.group().encode("utf-8")) + ")"
def replace_equations(s):
return re.sub("\$\$(.*?)\$\$", percent_encode, s)
if __name__ == "__main__":
# If no argument passed, encode README in the current directory
if len(sys.argv) == 1:
filename = "README"
try:
in_file = open(filename, "r")
except:
print "README not found."
sys.exit(0)
# If argument passed, see if it is README, then open file
if len(sys.argv) == 2:
filename = sys.argv[1]
try:
in_file = open(filename, "r")
except:
print "File not found."
sys.exit(0)
# Read README content, encode equatins and save to REAMD.md in the same folder
text = in_file.read()
in_file.close()
out_text = replace_equations(text)
out_file = open(filename+".md", "w")
out_file.write(out_text)