-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnoweb.py
executable file
·47 lines (43 loc) · 1.44 KB
/
noweb.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
#! /usr/local/bin/python
#
# noweb.py
# By Jonathan Aquino ([email protected])
#
# This program extracts code from a literate programming document in "noweb" format.
# It was generated from noweb.py.txt, itself a literate programming document.
# For more information, including the original source code and documentation,
# see http://jonaquino.blogspot.com/2010/04/nowebpy-or-worlds-first-executable-blog.html
#
import sys, re
filename = sys.argv[-1]
outputChunkName = sys.argv[-2][2:]
file = open(filename)
chunkName = None
chunks = {}
OPEN = "<<"
CLOSE = ">>"
for line in file:
match = re.match(OPEN + "([^>]+)" + CLOSE + "=", line)
if match:
chunkName = match.group(1)
# If chunkName exists in chunks, then we'll just add to the existing chunk.
if not chunkName in chunks:
chunks[chunkName] = []
else:
match = re.match("@", line)
if match:
chunkName = None
elif chunkName:
chunks[chunkName].append(line)
def expand(chunkName, indent):
chunkLines = chunks[chunkName]
expandedChunkLines = []
for line in chunkLines:
match = re.match("(\s*)" + OPEN + "([^>]+)" + CLOSE + "\s*$", line)
if match:
expandedChunkLines.extend(expand(match.group(2), indent + match.group(1)))
else:
expandedChunkLines.append(indent + line)
return expandedChunkLines
for line in expand(outputChunkName, ""):
print line,