2 Parser for Netscape Navigator's and Mozilla's bookmarks.html
4 Written by BroytMann. Copyright (C) 1997-2003 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):
129 for attrname, value in attrs:
130 value = value.strip()
131 if attrname == 'href':
133 if attrname == 'add_date':
135 if attrname == 'last_visit':
137 if attrname == 'last_modified':
138 last_modified = value
140 debug("Bookmark points to: `%s'" % href)
141 bookmark = Bookmark(href, add_date, last_visit, last_modified)
142 self.current_object = bookmark
143 self.current_folder.append(bookmark)
148 accumulator = self.accumulator
149 self.accumulator = ''
151 debug("Bookmark name: `%s'" % accumulator)
152 bookmark = self.current_folder[-1]
153 bookmark.name = accumulator
157 accumulator = self.accumulator
160 self.accumulator = ''
162 current_object = self.current_object
164 current_object.comment += accumulator.strip()
165 debug("Comment: `%s'" % current_object.comment)
168 def start_dl(self, attrs):
178 debug("Folder stack: %s" % dump_names(self.folder_stack))
179 if self.folder_stack:
180 del self.folder_stack[-1] # pop last folder
181 if self.folder_stack:
182 self.current_folder = self.folder_stack[-1]
184 debug("FOLDER STACK is EMPTY!!! (1)")
186 debug("FOLDER STACK is EMPTY!!! (2)")
187 self.current_object = None
191 HTMLParser.close(self)
192 if self.folder_stack:
193 raise ValueError, "wrong folder stack: %s" % self.folder_stack
196 def do_dd(self, attrs):
203 def do_hr(self, attrs):
206 self.current_folder.append(Ruler())
207 self.current_object = None
212 def do_br(self, attrs):
213 self.accumulator += "<BR>"
216 # Allow < in the text
217 def unknown_starttag(self, tag, attrs):
218 self.accumulator += "<%s>" % tag
221 # Do not allow unknow end tags
222 def unknown_endtag(self, tag):
223 raise NotImplementedError("Unknow end tag `%s'" % tag)