-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathxmlsplit.py
49 lines (45 loc) · 1.9 KB
/
xmlsplit.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
"""Splits plastex XML into individual chapters."""
import sys
def name(num):
"""chapter number -> filename"""
if num < 10:
return "ch0" + str(num) + ".xml"
elif num < 15:
return "ch" + str(num) + ".xml"
else:
return "app" + chr(num + 82) + ".xml"
def main(xml):
src = open(xml, 'U')
num = 0
out = open(name(num), 'w')
for line in src:
# if next chapter, open new file
pos = line.find("<chapter ")
if pos < 0:
pos = line.find("<appendix ")
if pos > -1:
out.write(line[:pos])
out.write('\n')
line = line[pos:]
num += 1
out = open(name(num), 'w')
# apply various tweaks
line = line.replace(' />', '/>')
line = line.replace('></entry', '/') # table cell
line = line.replace('></title', '/') # exercises
line = line.replace('></ulink', '/') # \url
line = line.replace('<literal>', '<literal moreinfo="none">') # \tt
line = line.replace('<mml:math overflow="scroll"', '<mml:math') # inline math
line = line.replace('mode="display" overflow="scroll"', 'mode="display"') # display math
line = line.replace('<indexterm>', '<indexterm significance="normal">') # \index
line = line.replace('<orderedlist>', '<orderedlist inheritnum="ignore" continuation="restarts">') # \enumerate
line = line.replace('<literal remap="verb">', '<literal remap="verb" moreinfo="none">') # \java
line = line.replace('<programlisting language="java">\n', '<programlisting language="java" format="linespecific">') # \code
line = line.replace('<programlisting>', '<programlisting format="linespecific">') # \stdout
# output the next line
out.write(line)
if __name__ == "__main__":
if len(sys.argv) == 2:
main(sys.argv[1])
else:
print "Usage: python xmlsplit.py MAIN.xml"