1 """Parser for Netscape Navigator's and Mozilla's bookmarks.html
3 This file is a part of Bookmarks database and Internet robot.
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 1997-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
13 __all__ = ['BkmkParser']
17 from m_lib.net.www.html import HTMLParser
18 from bkmk_objects import Folder, Bookmark, Ruler
21 DEBUG = os.environ.has_key("BKMK_DEBUG")
27 def dump_names(folder_stack):
29 for object in folder_stack:
32 return "'%s'" % "' '".join(l)
41 from m_lib.defenc import default_encoding as DEFAULT_CHARSET
43 DEFAULT_CHARSET = None
45 class BkmkParser(HTMLParser):
47 HTMLParser.__init__(self)
55 def handle_data(self, data):
57 if self.charset and DEFAULT_CHARSET:
58 data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "xmlcharrefreplace")
59 self.accumulator += data
61 # Mozilla - get charset
62 def do_meta(self, attrs):
66 for attrname, value in attrs:
68 if attrname == 'http-equiv':
69 http_equiv = value.lower()
70 elif attrname == 'content':
73 if http_equiv == "content-type":
75 # extract charset from "text/html; charset=UTF-8"
76 self.charset = content.split('=')[1]
80 def start_title(self, attrs):
82 self.accumulator += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%s">\n' % DEFAULT_CHARSET
83 self.accumulator += "<TITLE>"
86 self.accumulator += "</TITLE>"
89 def start_h1(self, attrs):
90 root_folder = Folder()
91 self.current_object = root_folder
92 self.root_folder = root_folder
93 self.current_folder = root_folder
94 self.folder_stack = [root_folder]
96 self.root_folder.header = self.accumulator.strip()
100 accumulator = self.accumulator
101 self.accumulator = ''
103 debug("Root folder name: `%s'" % accumulator)
104 self.root_folder.name = accumulator
107 def start_h3(self, attrs):
109 for attrname, value in attrs:
110 value = value.strip()
111 if attrname == 'add_date':
113 elif attrname == 'last_modified':
114 last_modified = value
116 debug("New folder...")
117 folder = Folder(add_date, last_modified=last_modified)
118 self.current_object = folder
119 self.current_folder.append(folder)
120 self.folder_stack.append(folder) # push new folder
121 self.current_folder = folder
125 accumulator = self.accumulator
126 self.accumulator = ''
128 debug("Folder name: `%s'" % accumulator)
129 self.current_folder.name = accumulator
132 def start_a(self, attrs):
140 for attrname, value in attrs:
141 value = value.strip()
142 if attrname == "href":
144 elif attrname == "add_date":
146 elif attrname == "last_visit":
148 elif attrname == "last_modified":
149 last_modified = value
150 elif attrname == "shortcuturl":
152 elif attrname == "icon":
154 elif attrname == "last_charset":
157 debug("Bookmark points to: `%s'" % href)
158 bookmark = Bookmark(href, add_date, last_visit, last_modified,
159 keyword or '', '', icon, charset)
160 self.current_object = bookmark
161 self.current_folder.append(bookmark)
166 accumulator = self.accumulator
167 self.accumulator = ''
169 debug("Bookmark name: `%s'" % accumulator)
170 bookmark = self.current_folder[-1]
171 bookmark.name = accumulator
174 accumulator = self.accumulator
177 self.accumulator = ''
179 current_object = self.current_object
181 current_object.comment += accumulator.strip()
182 debug("Comment: `%s'" % current_object.comment)
184 def start_dl(self, attrs):
193 debug("Folder stack: %s" % dump_names(self.folder_stack))
194 if self.folder_stack:
195 del self.folder_stack[-1] # pop last folder
196 if self.folder_stack:
197 self.current_folder = self.folder_stack[-1]
199 debug("FOLDER STACK is EMPTY!!! (1)")
201 debug("FOLDER STACK is EMPTY!!! (2)")
202 self.current_object = None
205 HTMLParser.close(self)
206 if self.folder_stack:
207 raise ValueError, "wrong folder stack: %s" % self.folder_stack
209 def do_dd(self, attrs):
215 def do_hr(self, attrs):
218 self.current_folder.append(Ruler())
219 self.current_object = None
223 def do_br(self, attrs):
224 self.accumulator += "<BR>"
226 # Allow < in the text
227 def unknown_starttag(self, tag, attrs):
228 self.accumulator += "<%s>" % tag
230 # Do not allow unknow end tags
231 def unknown_endtag(self, tag):
232 raise NotImplementedError("Unknow end tag `%s'" % tag)