-
Notifications
You must be signed in to change notification settings - Fork 4
/
replace_tex.py
25 lines (20 loc) · 875 Bytes
/
replace_tex.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
import sys
import re
interline_tag = '\n<img src="https://www.zhihu.com/equation?tex={}" alt="{}\\\\" class="ee_img tr_noresize" eeimg="1">\n'
interline_pattern = '\$\$\n*(.*?)\n*\$\$'
inline_tag = '<img src="https://www.zhihu.com/equation?tex={}" alt="{}" class="ee_img tr_noresize" eeimg="1">'
inline_pattern = '\$\n*(.*?)\n*\$'
def replace_tex(content):
def dashrepl(matchobj, tag):
formular = matchobj.group(1)
return tag.format(formular, formular)
content = re.sub(interline_pattern, lambda mo: dashrepl(mo, interline_tag), content)
content = re.sub(inline_pattern, lambda mo: dashrepl(mo, inline_tag), content)
return content
if __name__=='__main__':
assert len(sys.argv) > 1, "Error: need filename as a argument"
filename = sys.argv[1]
with open(filename, 'r') as f:
content = f.read()
with open(filename, 'w') as f:
f.write(replace_tex(content))