#!/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)