forked from HoTT/book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-errata
executable file
·87 lines (69 loc) · 2.06 KB
/
filter-errata
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
import re
import sys
if len(sys.argv) != 2:
print "Usage: myerrata versionmarker"
exit(1)
# Version number we're being asked about
currentversion = sys.argv[1]
currentnumber = int(currentversion.split('-')[0])
print "Filtering errata for version marker " + currentversion + "..."
# Some regexps
commentre = re.compile('^\s*%')
versionre = re.compile('^\s*&\s*([0-9]+)-g[0-9a-f]{7}')
# Old and new errata files
errata = open("errata.tex",'r')
new = ""
# Move forward until the title
line = errata.readline()
while line != "%% VERSION MARKER\n":
new += line
line = errata.readline()
# Discard the comment
# Insert the version marker in the title
new += "\\\\since version " + currentversion + "\n"
# Move forward to the description
line = errata.readline()
while line != "%% BEGIN STARTPOINT\n":
new += line
line = errata.readline()
# Insert the version marker
new += "since version marker " + currentversion + "\n"
# Discard the "so far"
line = errata.readline()
while line != "%% END STARTPOINT\n":
line = errata.readline()
# Now get up to the actual errata
line = errata.readline()
while line != "%% BEGIN ERRATA\n":
new += line
line = errata.readline()
# Parse each erratum and decide whether to include it
while line != "%% END ERRATA\n":
# Skip comments
while commentre.search(line):
new += line
line = errata.readline()
# Now get the erratum, parsing the version number when we see it
ver = float('inf')
erratum = ''
while not commentre.search(line):
erratum += line
vmatch = versionre.search(line)
if vmatch:
ver = int(vmatch.group(1))
line = errata.readline()
# If it applies to us, put it in
if ver > currentnumber:
new += erratum
# Finally, put the rest in.
while line:
new += line
line = errata.readline()
errata.close()
# New errata file
newfilename = "errata-since-" + currentversion + ".tex"
print "Writing filtered errata to " + newfilename + "."
newfile = open(newfilename,'w')
newfile.write(new)
newfile.close()