OSDN Git Service

add SGML declare handler
[htmltree-py/htmltree.git] / htmltree.py
index a98c0e0..668c521 100644 (file)
@@ -60,13 +60,15 @@ class HTMLRenderer(Renderer):
         elif elem.is_root():
             for child in elem:
                 self._recursive(child, texts)
-        
+        elif elem.is_decl():
+            texts.append("<!" + elem.name + ">")
 
 class HTMLElement(list):
     """HTML element object to use as tree nodes."""
     ROOT = 0
     TAG = 100
     TEXT = 200
+    DECL = 300
 
     def __init__(self, type, name="", attrs={}):
         """
@@ -94,6 +96,8 @@ class HTMLElement(list):
     def __repr__(self):
         if self.type == HTMLElement.TAG:
             return "<TAG:%s %s>" % (self.name, self._attrs2str())
+        elif self.type == HTMLElement.DECL:
+            return "<DECL:'%s'>" % self.name
         elif self.type == HTMLElement.TEXT:
             return "<TEXT:'%s'>" % self._text
         else:
@@ -244,6 +248,9 @@ class HTMLElement(list):
     def is_root(self):
         return self.type == HTMLElement.ROOT
 
+    def is_decl(self):
+        return self.type == HTMLElement.DECL
+
     def is_descendant(self, tagname):
         p = self.parent()
         while p != None:
@@ -408,6 +415,11 @@ class HTMLTree(HTMLParser.HTMLParser):
             elem._text = data
         self._cursor.append(elem)
 
+    def handle_decl(self, decl):
+        elem = HTMLElement(HTMLElement.DECL, decl)
+        elem._parent = self._cursor
+        self._cursor.append(elem)
+
     # Accessor
     def root(self):
         return self._htmlroot