-
Notifications
You must be signed in to change notification settings - Fork 4
/
github_formatter.py
65 lines (55 loc) · 2.09 KB
/
github_formatter.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
62
63
64
65
from pathlib import Path
import re
def parser(markdown):
"""Parses original markdown formatting into Github-compatible version
Parameters
----------
markdown : Path
Path object pointing to markdown file
"""
with open(Path('.', markdown), encoding='utf8') as f:
math = False
i = 0
lines = f.readlines()
newlines = []
for line in lines:
if re.search(pattern=r'\$\$', string=line) and not re.search(
pattern=r'\$\$(.*)\$\$', string=line
):
if not math:
math = True
# strip all non-dollar text
newline = line.replace('$$', '').replace('\n', '').strip()
newline = ['\n', '$$\n', '\\begin{aligned}\n', newline]
for each in newline:
newlines.append(each)
else:
math = False
#
newline = ['\\end{aligned}\n', '$$\n', '\n']
for each in newline:
newlines.append(each)
else:
if math:
newline = line.replace('sw_', 'sw \\_ ')
newline = re.sub(r'\\tag\{(.+)\}', '', newline)
newline = re.sub(r'_(?<!\\)', r'\\_', newline)
newline = re.sub(r'\|', r'\\|', newline)
newline = re.sub(r'<', r' \\< ', newline)
newline = re.sub(r'>', r' \\> ', newline)
if not newline.strip() == '':
newlines.append(newline)
else:
newlines.append(line)
i += 1
f.close()
### write to new file
with open(Path('.', markdown), 'w', encoding='utf8') as f:
f.writelines(newlines)
f.close()
return
if __name__ == '__main__':
### List all markdown files
wd = Path('.')
files = [x for x in wd.glob(pattern='**/README.md')]
[x for x in map(parser, files)]