]> git.phdru.name Git - bookmarks_db.git/blobdiff - bkmk_parser.py
Fixed encoding.
[bookmarks_db.git] / bkmk_parser.py
old mode 100755 (executable)
new mode 100644 (file)
index dfe7ce2..3f2182c
@@ -1,7 +1,7 @@
 """
    Parser for Netscape Navigator's and Mozilla's bookmarks.html
 
-   Written by BroytMann. Copyright (C) 1997-2004 PhiloSoft Design
+   Written by Broytman. Copyright (C) 1997-2008 PhiloSoft Design
 """
 
 
@@ -45,7 +45,7 @@ class BkmkParser(HTMLParser):
    def handle_data(self, data):
       if data:
          if DEFAULT_CHARSET:
-            data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "replace")
+            data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "xmlcharrefreplace")
          self.accumulator += data
 
 
@@ -70,9 +70,18 @@ class BkmkParser(HTMLParser):
          else:
             global DEFAULT_CHARSET
             DEFAULT_CHARSET = sys.getdefaultencoding()
+            if DEFAULT_CHARSET == "ascii":
+               try:
+                  import locale
+               except ImportError:
+                  pass
+               else:
+                  DEFAULT_CHARSET = locale.getpreferredencoding()
 
 
    def start_title(self, attrs):
+      if DEFAULT_CHARSET:
+         self.accumulator += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%s">\n' % DEFAULT_CHARSET
       self.accumulator += "<TITLE>"
 
    def end_title(self):
@@ -100,13 +109,16 @@ class BkmkParser(HTMLParser):
 
    # Start a folder
    def start_h3(self, attrs):
+      last_modified = None
       for attrname, value in attrs:
          value = value.strip()
          if attrname == 'add_date':
             add_date = value
+         elif attrname == 'last_modified':
+            last_modified = value
 
       debug("New folder...")
-      folder = Folder(add_date)
+      folder = Folder(add_date, last_modified=last_modified)
       self.current_object = folder
       self.current_folder.append(folder)
       self.folder_stack.append(folder) # push new folder
@@ -123,9 +135,12 @@ class BkmkParser(HTMLParser):
 
    # Start a bookmark
    def start_a(self, attrs):
+      add_date = None
       last_visit = None
       last_modified = None
       keyword = None
+      icon = None
+      charset = None
 
       for attrname, value in attrs:
          value = value.strip()
@@ -139,9 +154,14 @@ class BkmkParser(HTMLParser):
             last_modified = value
          elif attrname == "shortcuturl":
             keyword = value
+         elif attrname == "icon":
+            icon = value
+         elif attrname == "last_charset":
+            charset = value
 
       debug("Bookmark points to: `%s'" % href)
-      bookmark = Bookmark(href, add_date, last_visit, last_modified, keyword or '')
+      bookmark = Bookmark(href, add_date, last_visit, last_modified,
+         keyword or '', '', icon, charset)
       self.current_object = bookmark
       self.current_folder.append(bookmark)
       self.urls += 1