2 Parser for Netscape Navigator's and Mozilla's bookmarks.html
4 Written by BroytMann. Copyright (C) 1997-2005 PhiloSoft Design
9 from m_lib.net.www.html import HTMLParser
10 from bkmk_objects import Folder, Bookmark, Ruler
13 DEBUG = os.environ.has_key("BKMK_DEBUG")
19 def dump_names(folder_stack):
21 for object in folder_stack:
24 return "'%s'" % "' '".join(l)
32 DEFAULT_CHARSET = None
34 class BkmkParser(HTMLParser):
36 HTMLParser.__init__(self)
45 def handle_data(self, data):
48 data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "replace")
49 self.accumulator += data
52 # Mozilla - get charset
53 def do_meta(self, attrs):
57 for attrname, value in attrs:
59 if attrname == 'http-equiv':
60 http_equiv = value.lower()
61 elif attrname == 'content':
64 if http_equiv == "content-type":
66 # extract charset from "text/html; charset=UTF-8"
67 self.charset = content.split('=')[1]
71 global DEFAULT_CHARSET
72 DEFAULT_CHARSET = sys.getdefaultencoding()
73 if DEFAULT_CHARSET == "ascii":
79 DEFAULT_CHARSET = locale.getpreferredencoding()
82 def start_title(self, attrs):
84 self.accumulator += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%s">\n' % DEFAULT_CHARSET
85 self.accumulator += "<TITLE>"
88 self.accumulator += "</TITLE>"
92 def start_h1(self, attrs):
93 root_folder = Folder()
94 self.current_object = root_folder
95 self.root_folder = root_folder
96 self.current_folder = root_folder
97 self.folder_stack = [root_folder]
99 self.root_folder.header = self.accumulator.strip()
100 self.accumulator = ''
103 accumulator = self.accumulator
104 self.accumulator = ''
106 debug("Root folder name: `%s'" % accumulator)
107 self.root_folder.name = accumulator
111 def start_h3(self, attrs):
112 for attrname, value in attrs:
113 value = value.strip()
114 if attrname == 'add_date':
117 debug("New folder...")
118 folder = Folder(add_date)
119 self.current_object = folder
120 self.current_folder.append(folder)
121 self.folder_stack.append(folder) # push new folder
122 self.current_folder = folder
126 accumulator = self.accumulator
127 self.accumulator = ''
129 debug("Folder name: `%s'" % accumulator)
130 self.current_folder.name = accumulator
134 def start_a(self, attrs):
139 for attrname, value in attrs:
140 value = value.strip()
141 if attrname == "href":
143 elif attrname == "add_date":
145 elif attrname == "last_visit":
147 elif attrname == "last_modified":
148 last_modified = value
149 elif attrname == "shortcuturl":
152 debug("Bookmark points to: `%s'" % href)
153 bookmark = Bookmark(href, add_date, last_visit, last_modified, keyword or '')
154 self.current_object = bookmark
155 self.current_folder.append(bookmark)
160 accumulator = self.accumulator
161 self.accumulator = ''
163 debug("Bookmark name: `%s'" % accumulator)
164 bookmark = self.current_folder[-1]
165 bookmark.name = accumulator
169 accumulator = self.accumulator
172 self.accumulator = ''
174 current_object = self.current_object
176 current_object.comment += accumulator.strip()
177 debug("Comment: `%s'" % current_object.comment)
180 def start_dl(self, attrs):
190 debug("Folder stack: %s" % dump_names(self.folder_stack))
191 if self.folder_stack:
192 del self.folder_stack[-1] # pop last folder
193 if self.folder_stack:
194 self.current_folder = self.folder_stack[-1]
196 debug("FOLDER STACK is EMPTY!!! (1)")
198 debug("FOLDER STACK is EMPTY!!! (2)")
199 self.current_object = None
203 HTMLParser.close(self)
204 if self.folder_stack:
205 raise ValueError, "wrong folder stack: %s" % self.folder_stack
208 def do_dd(self, attrs):
215 def do_hr(self, attrs):
218 self.current_folder.append(Ruler())
219 self.current_object = None
224 def do_br(self, attrs):
225 self.accumulator += "<BR>"
228 # Allow < in the text
229 def unknown_starttag(self, tag, attrs):
230 self.accumulator += "<%s>" % tag
233 # Do not allow unknow end tags
234 def unknown_endtag(self, tag):
235 raise NotImplementedError("Unknow end tag `%s'" % tag)