-
Notifications
You must be signed in to change notification settings - Fork 21
/
process_md
executable file
·52 lines (37 loc) · 1.07 KB
/
process_md
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
#!/usr/bin/env python3
import re
import sys
include_file_re = re.compile(r'^<!-- include "([^"]*)" -->\n$')
end_include_re = re.compile(r'^<!-- end_include -->\n$')
def find_include_file(line):
m = include_file_re.match(line)
return m.group(1) if m else None
def find_end_include(line):
return end_include_re.match(line)
def include(filename):
with open(filename) as f:
return """```
%s```
<!-- end_include -->
""" % f.read()
def fast_forward_to_end_include(it):
for line in it:
if find_end_include(line):
break
return it
def main(argv):
assert len(argv) == 2
filename = argv[1]
contents = ""
with open(filename, "r") as f:
it = f.__iter__()
for line in it:
contents += line
include_filename = find_include_file(line)
if include_filename is not None:
contents += include(include_filename)
it = fast_forward_to_end_include(it)
with open(filename, "w") as f:
f.write(contents)
if __name__ == "__main__":
main(sys.argv)