-
Notifications
You must be signed in to change notification settings - Fork 4
/
post-nbconvert.py
43 lines (39 loc) · 1.28 KB
/
post-nbconvert.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
'''
A script used to post-process the README.md output from nbconvert.
Namely it formats LaTeX formulas so they are rendered in GitHub.
'''
math_mode = False
code_mode = False
indexes = []
with open('README.md', encoding='utf8') as f:
content = f.read()
new_content = ''
i = 0
while i < len(content):
if i+3 <= len(content) and content[i:i+3] == '```':
if not code_mode:
code_mode = True
else:
code_mode = False
if content[i] == '$' and not code_mode:
if not math_mode:
math_mode = True
if i+1 < len(content) and content[i+1] == '$':
i += 1
i_start = i+1
else:
math_mode = False
formula = content[i_start:i]
formula = formula.replace('&', '%26')
formula = formula.replace('+', '%2B')
formula = formula.replace('\n', ' ')
formula = formula.strip(' ')
url = rf'https://render.githubusercontent.com/render/math?math={formula}'
new_content += rf'<img src="{url}">'
if i+1 < len(content) and content[i+1] == '$':
i += 1
elif not math_mode:
new_content += content[i]
i += 1
with open('README.md', 'w', encoding='utf8') as f:
f.write(new_content)