forked from diffpy/diffpy.pdfgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixHTMLCode.py
61 lines (43 loc) · 1.24 KB
/
fixHTMLCode.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import os
import re
import sys
"""Replace all equation marks in HTML file with <img> tag to display
corresponding PNG file. This assumes PNG files are in correct sequence.
Also fix any accented characters texinfo does not get right.
"""
# constants
rc = {
"directory": "images", # directory with equation images
}
eqmark = "<!-- EquationMark -->"
##############################################################################
# business
eqcnt = 0
def eqreplace(mx):
"""helper function to replace equation marks.
mx -- regular expression match object
Return replacement string.
"""
global eqcnt
eqcnt += 1
imgfile = "eq-%02i.png" % eqcnt
imgurl = os.path.join(rc["directory"], imgfile)
s = '</p><p align="center"><img src="%s" alt="%s">' % (imgurl, imgurl)
return s
def replaceEquationMarks(s):
"""Replace equation marks in given string.
Return modified string.
"""
s1 = re.sub(eqmark, eqreplace, s)
return s1
def main():
for f in sys.argv[1:]:
with open(f) as fp:
s = fp.read()
s1 = replaceEquationMarks(s)
if s1 != s:
with open(f, "w") as fpout:
fpout.write(s1)
if __name__ == "__main__":
main()