-
Notifications
You must be signed in to change notification settings - Fork 159
/
mk-appendix.py
60 lines (54 loc) · 1.61 KB
/
mk-appendix.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
#!/usr/bin/env python
from __future__ import print_function
import re
import sys
APPENDICES = {}
IN_APPENDIX = None
CURRENT = ""
def print_syntax(val):
vl = val.split("\n")
last_empty = False
for v in vl:
if v == "":
if not last_empty:
print()
last_empty = True
else:
last_empty = False
print(v)
print()
for l in sys.stdin:
if not IN_APPENDIX:
m = re.match('%%% (.*)$', l)
if m is not None:
IN_APPENDIX = m.group(1)
else:
m = re.match('%%(#+|!) ([^{]*)({.*)? *$', l)
if m is not None:
if m.group(1) != '!':
if m.group(3) is None:
print("%s %s" % (m.group(1), m.group(2)))
else:
print("%s %s %s" % (m.group(1), m.group(2), m.group(3)))
print_syntax(APPENDICES[m.group(2).strip()])
del APPENDICES[m.group(2).strip()]
print()
else:
print(l, end='')
else:
# Strip out everything marked as RESERVED
if l.find("RESERVED") == -1:
print(l, end='')
m = re.match("\S", l)
if m is None:
CURRENT += l
else:
CURRENT += "\n"
if not IN_APPENDIX in APPENDICES:
APPENDICES[IN_APPENDIX] = ""
APPENDICES[IN_APPENDIX] += CURRENT
CURRENT = ""
IN_APPENDIX = None
if len(APPENDICES) > 0:
sys.stderr.write("Unused figures: " + str(list(APPENDICES.keys())) + "\n")
sys.exit(1)