forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractcode.py
More file actions
36 lines (31 loc) · 995 Bytes
/
Copy pathextractcode.py
File metadata and controls
36 lines (31 loc) · 995 Bytes
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
"""
Take Markdown and extract the code sections.
"""
import sys
outputs = {}
eg = sys.stdin.read()
paragraph_break = True
label = 'default'
lines = iter(eg.splitlines())
for line in lines:
assert '\t' not in line
if paragraph_break and line.startswith(' '):
if line.strip().startswith('!!'):
label = line.strip().strip('!')
line = next(lines)
while line.startswith(' '):
outputs.setdefault(label, []).append(line[4:])
line = next(lines)
outputs.setdefault(label, []).append('')
paragraph_break = (line.strip() == '')
if not paragraph_break:
label = 'default'
parts = 'top.py assembler.py default'.split()
with open('compiler.py', 'w') as f:
f.write('\n'.join(sum(map(outputs.get, parts), [])))
for label in parts:
del outputs[label]
with open('non-compiler', 'w') as f:
for label, lines in outputs.items():
for line in lines:
f.write(label+': '+line+'\n')