修订版 | 3a17d1c05da239c63cec678f8e139ecfa60d8a25 (tree) |
---|---|
时间 | 2011-07-21 17:29:33 |
作者 | Hiromichi MATSUSHIMA <hirom@offi...> |
Commiter | Hiromichi MATSUSHIMA |
add SGML declare handler
@@ -60,13 +60,15 @@ class HTMLRenderer(Renderer): | ||
60 | 60 | elif elem.is_root(): |
61 | 61 | for child in elem: |
62 | 62 | self._recursive(child, texts) |
63 | - | |
63 | + elif elem.is_decl(): | |
64 | + texts.append("<!" + elem.name + ">") | |
64 | 65 | |
65 | 66 | class HTMLElement(list): |
66 | 67 | """HTML element object to use as tree nodes.""" |
67 | 68 | ROOT = 0 |
68 | 69 | TAG = 100 |
69 | 70 | TEXT = 200 |
71 | + DECL = 300 | |
70 | 72 | |
71 | 73 | def __init__(self, type, name="", attrs={}): |
72 | 74 | """ |
@@ -94,6 +96,8 @@ class HTMLElement(list): | ||
94 | 96 | def __repr__(self): |
95 | 97 | if self.type == HTMLElement.TAG: |
96 | 98 | return "<TAG:%s %s>" % (self.name, self._attrs2str()) |
99 | + elif self.type == HTMLElement.DECL: | |
100 | + return "<DECL:'%s'>" % self.name | |
97 | 101 | elif self.type == HTMLElement.TEXT: |
98 | 102 | return "<TEXT:'%s'>" % self._text |
99 | 103 | else: |
@@ -244,6 +248,9 @@ class HTMLElement(list): | ||
244 | 248 | def is_root(self): |
245 | 249 | return self.type == HTMLElement.ROOT |
246 | 250 | |
251 | + def is_decl(self): | |
252 | + return self.type == HTMLElement.DECL | |
253 | + | |
247 | 254 | def is_descendant(self, tagname): |
248 | 255 | p = self.parent() |
249 | 256 | while p != None: |
@@ -408,6 +415,11 @@ class HTMLTree(HTMLParser.HTMLParser): | ||
408 | 415 | elem._text = data |
409 | 416 | self._cursor.append(elem) |
410 | 417 | |
418 | + def handle_decl(self, decl): | |
419 | + elem = HTMLElement(HTMLElement.DECL, decl) | |
420 | + elem._parent = self._cursor | |
421 | + self._cursor.append(elem) | |
422 | + | |
411 | 423 | # Accessor |
412 | 424 | def root(self): |
413 | 425 | return self._htmlroot |