2 Parser for Netscape Navigator's and Mozilla's bookmarks.html
4 Written by BroytMann. Copyright (C) 1997-2004 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()
75 def start_title(self, attrs):
76 self.accumulator += "<TITLE>"
79 self.accumulator += "</TITLE>"
83 def start_h1(self, attrs):
84 root_folder = Folder()
85 self.current_object = root_folder
86 self.root_folder = root_folder
87 self.current_folder = root_folder
88 self.folder_stack = [root_folder]
90 self.root_folder.header = self.accumulator.strip()
94 accumulator = self.accumulator
97 debug("Root folder name: `%s'" % accumulator)
98 self.root_folder.name = accumulator
102 def start_h3(self, attrs):
103 for attrname, value in attrs:
104 value = value.strip()
105 if attrname == 'add_date':
108 debug("New folder...")
109 folder = Folder(add_date)
110 self.current_object = folder
111 self.current_folder.append(folder)
112 self.folder_stack.append(folder) # push new folder
113 self.current_folder = folder
117 accumulator = self.accumulator
118 self.accumulator = ''
120 debug("Folder name: `%s'" % accumulator)
121 self.current_folder.name = accumulator
125 def start_a(self, attrs):
130 for attrname, value in attrs:
131 value = value.strip()
132 if attrname == "href":
134 elif attrname == "add_date":
136 elif attrname == "last_visit":
138 elif attrname == "last_modified":
139 last_modified = value
140 elif attrname == "shortcuturl":
143 debug("Bookmark points to: `%s'" % href)
144 bookmark = Bookmark(href, add_date, last_visit, last_modified, keyword)
145 self.current_object = bookmark
146 self.current_folder.append(bookmark)
151 accumulator = self.accumulator
152 self.accumulator = ''
154 debug("Bookmark name: `%s'" % accumulator)
155 bookmark = self.current_folder[-1]
156 bookmark.name = accumulator
160 accumulator = self.accumulator
163 self.accumulator = ''
165 current_object = self.current_object
167 current_object.comment += accumulator.strip()
168 debug("Comment: `%s'" % current_object.comment)
171 def start_dl(self, attrs):
181 debug("Folder stack: %s" % dump_names(self.folder_stack))
182 if self.folder_stack:
183 del self.folder_stack[-1] # pop last folder
184 if self.folder_stack:
185 self.current_folder = self.folder_stack[-1]
187 debug("FOLDER STACK is EMPTY!!! (1)")
189 debug("FOLDER STACK is EMPTY!!! (2)")
190 self.current_object = None
194 HTMLParser.close(self)
195 if self.folder_stack:
196 raise ValueError, "wrong folder stack: %s" % self.folder_stack
199 def do_dd(self, attrs):
206 def do_hr(self, attrs):
209 self.current_folder.append(Ruler())
210 self.current_object = None
215 def do_br(self, attrs):
216 self.accumulator += "<BR>"
219 # Allow < in the text
220 def unknown_starttag(self, tag, attrs):
221 self.accumulator += "<%s>" % tag
224 # Do not allow unknow end tags
225 def unknown_endtag(self, tag):
226 raise NotImplementedError("Unknow end tag `%s'" % tag)