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):
83 self.accumulator += "<TITLE>"
86 self.accumulator += "</TITLE>"
90 def start_h1(self, attrs):
91 root_folder = Folder()
92 self.current_object = root_folder
93 self.root_folder = root_folder
94 self.current_folder = root_folder
95 self.folder_stack = [root_folder]
97 self.root_folder.header = self.accumulator.strip()
101 accumulator = self.accumulator
102 self.accumulator = ''
104 debug("Root folder name: `%s'" % accumulator)
105 self.root_folder.name = accumulator
109 def start_h3(self, attrs):
110 for attrname, value in attrs:
111 value = value.strip()
112 if attrname == 'add_date':
115 debug("New folder...")
116 folder = Folder(add_date)
117 self.current_object = folder
118 self.current_folder.append(folder)
119 self.folder_stack.append(folder) # push new folder
120 self.current_folder = folder
124 accumulator = self.accumulator
125 self.accumulator = ''
127 debug("Folder name: `%s'" % accumulator)
128 self.current_folder.name = accumulator
132 def start_a(self, attrs):
137 for attrname, value in attrs:
138 value = value.strip()
139 if attrname == "href":
141 elif attrname == "add_date":
143 elif attrname == "last_visit":
145 elif attrname == "last_modified":
146 last_modified = value
147 elif attrname == "shortcuturl":
150 debug("Bookmark points to: `%s'" % href)
151 bookmark = Bookmark(href, add_date, last_visit, last_modified, keyword or '')
152 self.current_object = bookmark
153 self.current_folder.append(bookmark)
158 accumulator = self.accumulator
159 self.accumulator = ''
161 debug("Bookmark name: `%s'" % accumulator)
162 bookmark = self.current_folder[-1]
163 bookmark.name = accumulator
167 accumulator = self.accumulator
170 self.accumulator = ''
172 current_object = self.current_object
174 current_object.comment += accumulator.strip()
175 debug("Comment: `%s'" % current_object.comment)
178 def start_dl(self, attrs):
188 debug("Folder stack: %s" % dump_names(self.folder_stack))
189 if self.folder_stack:
190 del self.folder_stack[-1] # pop last folder
191 if self.folder_stack:
192 self.current_folder = self.folder_stack[-1]
194 debug("FOLDER STACK is EMPTY!!! (1)")
196 debug("FOLDER STACK is EMPTY!!! (2)")
197 self.current_object = None
201 HTMLParser.close(self)
202 if self.folder_stack:
203 raise ValueError, "wrong folder stack: %s" % self.folder_stack
206 def do_dd(self, attrs):
213 def do_hr(self, attrs):
216 self.current_folder.append(Ruler())
217 self.current_object = None
222 def do_br(self, attrs):
223 self.accumulator += "<BR>"
226 # Allow < in the text
227 def unknown_starttag(self, tag, attrs):
228 self.accumulator += "<%s>" % tag
231 # Do not allow unknow end tags
232 def unknown_endtag(self, tag):
233 raise NotImplementedError("Unknow end tag `%s'" % tag)