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"
14 from m_lib.net.www.html import HTMLParser
15 from bkmk_objects import Folder, Bookmark, Ruler
18 DEBUG = os.environ.has_key("BKMK_DEBUG")
24 def dump_names(folder_stack):
26 for object in folder_stack:
29 return "'%s'" % "' '".join(l)
37 DEFAULT_CHARSET = None
39 class BkmkParser(HTMLParser):
41 HTMLParser.__init__(self)
50 def handle_data(self, data):
53 data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "xmlcharrefreplace")
54 self.accumulator += data
57 # Mozilla - get charset
58 def do_meta(self, attrs):
62 for attrname, value in attrs:
64 if attrname == 'http-equiv':
65 http_equiv = value.lower()
66 elif attrname == 'content':
69 if http_equiv == "content-type":
71 # extract charset from "text/html; charset=UTF-8"
72 self.charset = content.split('=')[1]
76 global DEFAULT_CHARSET
77 DEFAULT_CHARSET = sys.getdefaultencoding()
78 if DEFAULT_CHARSET == "ascii":
84 DEFAULT_CHARSET = locale.getpreferredencoding()
87 def start_title(self, attrs):
89 self.accumulator += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%s">\n' % DEFAULT_CHARSET
90 self.accumulator += "<TITLE>"
93 self.accumulator += "</TITLE>"
97 def start_h1(self, attrs):
98 root_folder = Folder()
99 self.current_object = root_folder
100 self.root_folder = root_folder
101 self.current_folder = root_folder
102 self.folder_stack = [root_folder]
104 self.root_folder.header = self.accumulator.strip()
105 self.accumulator = ''
108 accumulator = self.accumulator
109 self.accumulator = ''
111 debug("Root folder name: `%s'" % accumulator)
112 self.root_folder.name = accumulator
116 def start_h3(self, attrs):
118 for attrname, value in attrs:
119 value = value.strip()
120 if attrname == 'add_date':
122 elif attrname == 'last_modified':
123 last_modified = value
125 debug("New folder...")
126 folder = Folder(add_date, last_modified=last_modified)
127 self.current_object = folder
128 self.current_folder.append(folder)
129 self.folder_stack.append(folder) # push new folder
130 self.current_folder = folder
134 accumulator = self.accumulator
135 self.accumulator = ''
137 debug("Folder name: `%s'" % accumulator)
138 self.current_folder.name = accumulator
142 def start_a(self, attrs):
150 for attrname, value in attrs:
151 value = value.strip()
152 if attrname == "href":
154 elif attrname == "add_date":
156 elif attrname == "last_visit":
158 elif attrname == "last_modified":
159 last_modified = value
160 elif attrname == "shortcuturl":
162 elif attrname == "icon":
164 elif attrname == "last_charset":
167 debug("Bookmark points to: `%s'" % href)
168 bookmark = Bookmark(href, add_date, last_visit, last_modified,
169 keyword or '', '', icon, charset)
170 self.current_object = bookmark
171 self.current_folder.append(bookmark)
176 accumulator = self.accumulator
177 self.accumulator = ''
179 debug("Bookmark name: `%s'" % accumulator)
180 bookmark = self.current_folder[-1]
181 bookmark.name = accumulator
185 accumulator = self.accumulator
188 self.accumulator = ''
190 current_object = self.current_object
192 current_object.comment += accumulator.strip()
193 debug("Comment: `%s'" % current_object.comment)
196 def start_dl(self, attrs):
206 debug("Folder stack: %s" % dump_names(self.folder_stack))
207 if self.folder_stack:
208 del self.folder_stack[-1] # pop last folder
209 if self.folder_stack:
210 self.current_folder = self.folder_stack[-1]
212 debug("FOLDER STACK is EMPTY!!! (1)")
214 debug("FOLDER STACK is EMPTY!!! (2)")
215 self.current_object = None
219 HTMLParser.close(self)
220 if self.folder_stack:
221 raise ValueError, "wrong folder stack: %s" % self.folder_stack
224 def do_dd(self, attrs):
231 def do_hr(self, attrs):
234 self.current_folder.append(Ruler())
235 self.current_object = None
240 def do_br(self, attrs):
241 self.accumulator += "<BR>"
244 # Allow < in the text
245 def unknown_starttag(self, tag, attrs):
246 self.accumulator += "<%s>" % tag
249 # Do not allow unknow end tags
250 def unknown_endtag(self, tag):
251 raise NotImplementedError("Unknow end tag `%s'" % tag)