]> git.phdru.name Git - bookmarks_db.git/blobdiff - parse_html/bkmk_parse_html.py
Change parse_html to parse strings, not files
[bookmarks_db.git] / parse_html / bkmk_parse_html.py
index 42cb5ce1575abf2f0e1eb073943ca62240bcccb2..e951cdc19509bb7fbf6658961fa61e39dd682ead 100644 (file)
@@ -1,16 +1,14 @@
 """HTML Parsers
 
 This file is a part of Bookmarks database and Internet robot.
+
 """
 
-__version__ = "$Revision$"[11:-2]
-__revision__ = "$Id$"[5:-2]
-__date__ = "$Date$"[7:-2]
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 1997-2011 PhiloSoft Design"
+__copyright__ = "Copyright (C) 1997-2014 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-__all__ = ['parse_html', 'universal_charset']
+__all__ = ['parse_html', 'parse_filename', 'universal_charset']
 
 
 import codecs
@@ -20,12 +18,6 @@ DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
 
 parsers = []
 
-# Statistics by parser - successfully parsed HTML pages:
-# 4358 beautifulsoup
-# 4310 htmlparser
-# 4307 html5
-# 4250 lxml
-
 try:
    from . import bkmk_ph_beautifulsoup
 except ImportError:
@@ -35,25 +27,25 @@ else:
    parsers.append(bkmk_ph_beautifulsoup.parse_html)
 
 try:
-   from . import bkmk_ph_htmlparser
+   from . import bkmk_ph_html5
 except ImportError:
    pass
 else:
-    parsers.append(bkmk_ph_htmlparser.parse_html)
+   parsers.append(bkmk_ph_html5.parse_html)
 
 try:
-   from . import bkmk_ph_html5
+   from . import bkmk_ph_lxml
 except ImportError:
    pass
 else:
-   parsers.append(bkmk_ph_html5.parse_html)
+    parsers.append(bkmk_ph_lxml.parse_html)
 
 try:
-   from . import bkmk_ph_lxml
+   from . import bkmk_ph_htmlparser
 except ImportError:
    pass
 else:
-    parsers.append(bkmk_ph_lxml.parse_html)
+    parsers.append(bkmk_ph_htmlparser.parse_html)
 
 # ElementTidy often segfaults
 #try:
@@ -92,7 +84,10 @@ def recode_entities(title, charset):
    return ''.join(output)
 
 
-def parse_html(filename, charset=None, log=None):
+import os
+BKMK_DEBUG_HTML_PARSERS = os.environ.get("BKMK_DEBUG_HTML_PARSERS")
+
+def parse_html(html_text, charset=None, log=None):
    if not parsers:
        return None
 
@@ -109,31 +104,37 @@ def parse_html(filename, charset=None, log=None):
          charsets.remove(charset)
       charsets.insert(0, charset)
 
-   #_parsers = []
+   if BKMK_DEBUG_HTML_PARSERS:
+      _parsers = []
    for p in parsers:
       parser = None
       for c in charsets:
          try:
-            parser = p(filename, c, log)
+            parser = p(html_text, c, log)
          except UnicodeError:
             pass
          else:
             if parser:
-               if log: log("   Parser %s: ok" % p.__module__)
-               #_parsers.append(parser)
+               if BKMK_DEBUG_HTML_PARSERS:
+                  if log: log("   Parser %s: ok" % p.__module__)
+                  _parsers.append((p, parser))
                break
       else:
          if log: log("   Parser %s: fail" % p.__module__)
-      if parser:
+      if not BKMK_DEBUG_HTML_PARSERS and parser:
          break
 
-   #if not _parsers:
-   if not parser:
-       if log: log("   All parser has failed")
+   if BKMK_DEBUG_HTML_PARSERS:
+      if not _parsers:
+         if log: log("   All parsers have failed")
+         return None
+   elif not parser:
+       if log: log("   All parsers have failed")
        return None
 
-   #parser = _parsers[0]
-   if log: log("   Using %s" % parser.__module__)
+   if BKMK_DEBUG_HTML_PARSERS:
+      p, parser = _parsers[0]
+   if log: log("   Using %s" % p.__module__)
 
    converted_title = title = parser.title
    if title and (not parser.charset):
@@ -183,3 +184,11 @@ def parse_html(filename, charset=None, log=None):
            if parser.charset:
                parser.icon = icon.encode(parser.charset)
    return parser
+
+def parse_filename(filename, charset=None, log=None):
+    fp = open(filename, 'r')
+    try:
+        parser = parse_html(fp.read(), charset=charset, log=log)
+    finally:
+        fp.close()
+    return parser