-
Notifications
You must be signed in to change notification settings - Fork 147
/
makedoc.py
56 lines (43 loc) · 1.32 KB
/
makedoc.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
#!/usr/bin/env python
import re
import sys
from contextlib import contextmanager
def start_section(line):
sys.stdout.write("\n{}".format(line.lstrip()[3:]))
def emit_line(line):
sys.stdout.write("{}".format(line.lstrip()[3:]))
def emit_line_nosub(line):
sys.stdout.write(line)
def emit_empty():
sys.stdout.write("\n")
## Global state to track code section beg/end
in_code_section = False
def process_line(line):
global in_code_section
section_match = re.compile(r'^\s{0,}// #')
line_match = re.compile(r'^\s{0,}// ')
space_match = re.compile(r'^\s{0,}//$')
code_section_match = re.compile(r'^\s{0,}// ~~~~')
m = section_match.match(line)
if m:
start_section(line)
elif code_section_match.match(line):
if not in_code_section:
emit_line_nosub('```c')
emit_empty()
else:
emit_line_nosub('```')
emit_empty()
in_code_section = not in_code_section
elif line_match.match(line):
emit_line(line)
elif space_match.match(line):
emit_empty()
elif in_code_section:
emit_line_nosub(line)
def process(filename):
with open(filename, 'r') as f:
list(map(process_line, f.readlines()))
sys.stdout.flush()
if __name__ == '__main__':
process(sys.argv[1])