A simple HTML parser. Focused on converting the subset of HTML that appears in the documentation strings of the JSON models into simple ReST format.
| 14 | |
| 15 | |
| 16 | class DocStringParser(HTMLParser): |
| 17 | """ |
| 18 | A simple HTML parser. Focused on converting the subset of HTML |
| 19 | that appears in the documentation strings of the JSON models into |
| 20 | simple ReST format. |
| 21 | """ |
| 22 | |
| 23 | def __init__(self, doc): |
| 24 | self.tree = None |
| 25 | self.doc = doc |
| 26 | HTMLParser.__init__(self) |
| 27 | |
| 28 | def reset(self): |
| 29 | HTMLParser.reset(self) |
| 30 | self.tree = HTMLTree(self.doc) |
| 31 | |
| 32 | def feed(self, data): |
| 33 | # HTMLParser is an old style class, so the super() method will not work. |
| 34 | HTMLParser.feed(self, data) |
| 35 | self.tree.write() |
| 36 | self.tree = HTMLTree(self.doc) |
| 37 | |
| 38 | def close(self): |
| 39 | HTMLParser.close(self) |
| 40 | # Write if there is anything remaining. |
| 41 | self.tree.write() |
| 42 | self.tree = HTMLTree(self.doc) |
| 43 | |
| 44 | def handle_starttag(self, tag, attrs): |
| 45 | self.tree.add_tag(tag, attrs=attrs) |
| 46 | |
| 47 | def handle_endtag(self, tag): |
| 48 | self.tree.add_tag(tag, is_start=False) |
| 49 | |
| 50 | def handle_data(self, data): |
| 51 | self.tree.add_data(data) |
| 52 | |
| 53 | |
| 54 | class HTMLTree: |