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/env python
import sys
import xml.etree.ElementTree as ET
def main(argv):
root = ET.parse(argv[1]).getroot()
head = root.find('ThML.head')
body = root.find('ThML.body')
for thing in list(body):
parseSomething(thing)
def nlprint(string):
"""Print, starting with a newline."""
print("\n" + string, end='')
def iprint(string):
"""Inline-printing doesn't end with newline"""
print(string, end='')
def parseSomething(thing):
if(len(thing.tag) == 4 and thing.tag[:3] == 'div'):
parseDiv(thing)
elif (thing.tag in ['p']):
parseP(thing)
elif thing.tag in ['div']:
parseBlock(thing)
elif (thing.tag == 'argument'):
nlprint("> "); parseInline(thing);
elif (thing.tag in ['a', 'cite', 'i', 'name', 'note', 'scripRef', 'span']):
parseIl(thing)
elif (thing.tag == 'ul'):
parseList(thing)
else:
for thing in list(thing):
parseSomething(thing)
def parseDiv(div):
indentLevel = int(div.tag[3:])
title = div.get('title')
nlprint('#' * indentLevel + ' ' + title)
for child in list(div):
parseSomething(child)
def parseBlock(block):
# Block level elements should start with newlines
print("")
if(block.text):
iprint(block.text.replace("\n"," "))
for thing in block:
# and may contain other block level elements
parseSomething(thing)
if(thing.tail):
iprint(thing.tail.replace("\n"," "))
def parseP(p):
# Each paragraph should start with a newline
print("")
i = p.find('i')
if (not p.text) and i is not None and len(p) == 1 and (not i.tail):
iprint("> ")
p = i
parseInline(p)
def parseIl(inline):
"""Parse an inline item, being tag-aware"""
if(inline.tag == 'i'):
# Convention: Wrapping italics in asterisks
iprint("*"); parseInline(inline); iprint("*")
elif(inline.tag == 'note'):
iprint("["); parseInline(inline); iprint("]");
else:
parseInline(inline);
def parseInline(inline):
if(inline.text):
iprint(inline.text.replace("\n"," "))
for thing in inline:
parseIl(thing)
if(thing.tail):
iprint(thing.tail.replace("\n"," "))
def parseList(ul):
for child in ul:
nlprint("* ")
parseInline(child)
if __name__ == '__main__':
main(sys.argv)
|