]> git.phdru.name Git - bookmarks_db.git/blobdiff - bkmk_parser.py
Store charset in the DB and the generated HTML.
[bookmarks_db.git] / bkmk_parser.py
index 327e0c45f2dd2beb56711cd2e42eb0747e13bfd7..7ce99e8412447cbba334291783149f80cc2c3e46 100755 (executable)
@@ -1,7 +1,7 @@
 """
    Parser for Netscape Navigator's and Mozilla's bookmarks.html
 
-   Written by BroytMann. Copyright (C) 1997-2003 PhiloSoft Design
+   Written by BroytMann. Copyright (C) 1997-2005 PhiloSoft Design
 """
 
 
@@ -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):
@@ -98,7 +107,7 @@ class BkmkParser(HTMLParser):
       self.root_folder.name = accumulator
 
 
-   # Start next folder
+   # Start a folder
    def start_h3(self, attrs):
       for attrname, value in attrs:
          value = value.strip()
@@ -121,24 +130,27 @@ class BkmkParser(HTMLParser):
       self.current_folder.name = accumulator
 
 
-   # Start bookmark
+   # Start bookmark
    def start_a(self, attrs):
       last_visit = None
       last_modified = None
+      keyword = None
 
       for attrname, value in attrs:
          value = value.strip()
-         if attrname == 'href':
+         if attrname == "href":
             href = value
-         if attrname == 'add_date':
+         elif attrname == "add_date":
             add_date = value
-         if attrname == 'last_visit':
+         elif attrname == "last_visit":
             last_visit = value
-         if attrname == 'last_modified':
+         elif attrname == "last_modified":
             last_modified = value
+         elif attrname == "shortcuturl":
+            keyword = value
 
       debug("Bookmark points to: `%s'" % href)
-      bookmark = Bookmark(href, add_date, last_visit, last_modified)
+      bookmark = Bookmark(href, add_date, last_visit, last_modified, keyword or '')
       self.current_object = bookmark
       self.current_folder.append(bookmark)
       self.urls += 1