]> git.phdru.name Git - bookmarks_db.git/blobdiff - parse_html/bkmk_ph_htmlparser.py
Change parse_html to parse strings, not files
[bookmarks_db.git] / parse_html / bkmk_ph_htmlparser.py
index 8cdd240a06c7fb3514e79eb9df571dcf1b67279c..0798467386bda9ab993df160f1d4937b1e1802d9 100644 (file)
@@ -1,10 +1,11 @@
 """HTML Parser using Pythons' HTMLParser
 
 This file is a part of Bookmarks database and Internet robot.
+
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 1997-2012 PhiloSoft Design"
+__copyright__ = "Copyright (C) 1997-2014 PhiloSoft Design"
 __license__ = "GNU GPL"
 
 __all__ = ['parse_html']
@@ -40,6 +41,9 @@ class HTMLParser(_HTMLParser):
                http_equiv = value.lower()
             elif attrname == 'content':
                content = value
+            elif (attrname == 'charset') and (not self.charset):
+               self.charset = value.lower()
+               self.meta_charset = 1
 
       if (not self.charset) and (http_equiv == "content-type"):
          try:
@@ -76,17 +80,13 @@ class HTMLParser(_HTMLParser):
          self.icon = href
 
 
-def parse_html(filename, charset=None, log=None):
-   infile = open(filename, 'r')
+def parse_html(html_text, charset=None, log=None):
    parser = HTMLParser(charset)
 
-   for line in infile:
-      try:
-         parser.feed(line)
-      except (HTMLParseError, HTMLHeadDone):
-         break
-
-   infile.close()
+   try:
+      parser.feed(html_text)
+   except (HTMLParseError, HTMLHeadDone):
+      pass
 
    try:
       parser.close()