-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpreprocess.py
executable file
·65 lines (53 loc) · 1.45 KB
/
preprocess.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
#!/usr/bin/env python3
"""
Preprocess the given markdown input.
"""
import sys
import re
import urllib
TAB = ' '
def catfile(s):
"""
Find all occurences of 'Catfile <filepath>' and replace them by the content
of <filepath>.
"""
while True:
try:
m = next(re.finditer('\nCatxfile .*\n', s))
filepath = s[m.start() + 9:m.end() - 1]
with open(filepath, "r") as fh:
content = fh.read()
content = TAB + content.replace('\n', '\n' + TAB)
content = content[:-len(TAB)] # remove trailing \t
s = ''.join([s[:m.start()+1], content, s[m.end():]])
except StopIteration:
break
return s
def caturl(s):
"""
Find all occurences of 'Caturl <url>' and replace them by the content
of <url>.
"""
while True:
try:
m = next(re.finditer('\nCaturl .*\n', s))
url = s[m.start() + 8:m.end() - 1]
f = urllib.urlopen(url)
content = f.read()
s = ''.join([s[:m.start()+1], content, s[m.end():]])
except StopIteration:
break
return s
def preprocess(str):
str = catfile(str)
str = caturl(str)
return str
def main():
input_md = sys.argv[1]
with open(input_md, "r") as fh:
str = fh.read()
str = preprocess(str)
with open(input_md, "w") as fh:
fh.write(str)
if __name__ == '__main__':
main()