diff emeraldtree/html.py @ 100:9d39d4bc2deb

ported to python 2.7 / >= 3.3 using six the port itself was half automated (using python-modernize) and half manual fixes and cleanups.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 19 May 2013 20:37:50 +0200
parents 54c60c7e7e35
children
line wrap: on
line diff
--- a/emeraldtree/html.py	Sun Dec 23 19:39:04 2012 +0100
+++ b/emeraldtree/html.py	Sun May 19 20:37:50 2013 +0200
@@ -31,10 +31,11 @@
 # Tools to build element trees from HTML files.
 ##
 
-import htmlentitydefs
-from HTMLParser import HTMLParser as HTMLParserBase
-
 from . import tree
+import six
+from six.moves import html_entities
+from six.moves import html_parser
+HTMLParserBase = html_parser.HTMLParser
 
 
 ##
@@ -148,7 +149,7 @@
     # (Internal) Handles entity references.
 
     def handle_entityref(self, name):
-        entity = htmlentitydefs.entitydefs.get(name)
+        entity = html_entities.entitydefs.get(name)
         if entity:
             if len(entity) == 1:
                 entity = ord(entity)
@@ -165,9 +166,9 @@
     # (Internal) Handles character data.
 
     def handle_data(self, data):
-        if isinstance(data, str):
+        if isinstance(data, six.binary_type):
             # convert to unicode, but only if necessary
-            data = unicode(data, self.encoding, "ignore")
+            data = data.decode(self.encoding, "ignore")
         self.__builder.data(data)
 
     ##