1 """Parser for Netscape Navigator's and Mozilla's bookmarks.html
3 This file is a part of Bookmarks database and Internet robot.
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 1997-2023 PhiloSoft Design"
8 __license__ = "GNU GPL"
10 __all__ = ['BkmkParser']
14 from m_lib.defenc import default_encoding
15 from m_lib.net.www.html import HTMLParser
16 from bkmk_objects import Folder, Bookmark, Ruler
19 DEBUG = os.environ.has_key("BKMK_DEBUG")
25 def dump_names(folder_stack):
27 for object in folder_stack:
30 return "'%s'" % "' '".join(l)
38 class BkmkParser(HTMLParser):
40 HTMLParser.__init__(self)
48 def handle_data(self, data):
50 if self.charset and default_encoding:
51 data = unicode(data, self.charset, "replace").\
52 encode(default_encoding, "xmlcharrefreplace")
53 self.accumulator += data
55 # Mozilla - get charset
56 def do_meta(self, attrs):
60 for attrname, value in attrs:
62 if attrname == 'http-equiv':
63 http_equiv = value.lower()
64 elif attrname == 'content':
67 if http_equiv == "content-type":
69 # extract charset from "text/html; charset=UTF-8"
70 self.charset = content.split('=')[1]
74 def start_title(self, attrs):
76 self.accumulator += '<META HTTP-EQUIV="Content-Type" '
77 'CONTENT="text/html; charset=%s">\n' % default_encoding
78 self.accumulator += "<TITLE>"
81 self.accumulator += "</TITLE>"
84 def start_h1(self, attrs):
85 root_folder = Folder()
86 self.current_object = root_folder
87 self.root_folder = root_folder
88 self.current_folder = root_folder
89 self.folder_stack = [root_folder]
91 self.root_folder.header = self.accumulator.strip()
95 accumulator = self.accumulator
98 debug("Root folder name: `%s'" % accumulator)
99 self.root_folder.name = accumulator
102 def start_h3(self, attrs):
104 for attrname, value in attrs:
105 value = value.strip()
106 if attrname == 'add_date':
108 elif attrname == 'last_modified':
109 last_modified = value
111 debug("New folder...")
112 folder = Folder(add_date, last_modified=last_modified)
113 self.current_object = folder
114 self.current_folder.append(folder)
115 self.folder_stack.append(folder) # push new folder
116 self.current_folder = folder
120 accumulator = self.accumulator
121 self.accumulator = ''
123 debug("Folder name: `%s'" % accumulator)
124 self.current_folder.name = accumulator
127 def start_a(self, attrs):
135 for attrname, value in attrs:
136 value = value.strip()
137 if attrname == "href":
139 elif attrname == "add_date":
141 elif attrname == "last_visit":
143 elif attrname == "last_modified":
144 last_modified = value
145 elif attrname == "shortcuturl":
147 elif attrname == "icon":
149 elif attrname == "last_charset":
152 debug("Bookmark points to: `%s'" % href)
153 bookmark = Bookmark(href, add_date, last_visit, last_modified,
154 keyword=keyword, icon=icon, charset=charset,
155 parser_charset=self.charset or default_encoding)
156 self.current_object = bookmark
157 self.current_folder.append(bookmark)
162 accumulator = self.accumulator
163 self.accumulator = ''
165 debug("Bookmark name: `%s'" % accumulator)
166 bookmark = self.current_folder[-1]
167 bookmark.name = accumulator
170 accumulator = self.accumulator
173 self.accumulator = ''
175 current_object = self.current_object
177 current_object.comment += accumulator.strip()
178 debug("Comment: `%s'" % current_object.comment)
180 def start_dl(self, attrs):
189 debug("Folder stack: %s" % dump_names(self.folder_stack))
190 if self.folder_stack:
191 del self.folder_stack[-1] # pop last folder
192 if self.folder_stack:
193 self.current_folder = self.folder_stack[-1]
195 debug("FOLDER STACK is EMPTY!!! (1)")
197 debug("FOLDER STACK is EMPTY!!! (2)")
198 self.current_object = None
201 HTMLParser.close(self)
202 if self.folder_stack:
203 raise ValueError("wrong folder stack: %s" % self.folder_stack)
205 def do_dd(self, attrs):
211 def do_hr(self, attrs):
214 self.current_folder.append(Ruler())
215 self.current_object = None
219 def do_br(self, attrs):
220 self.accumulator += "<BR>"
222 # Allow < in the text
223 def unknown_starttag(self, tag, attrs):
224 self.accumulator += "<%s>" % tag
226 # Do not allow unknow end tags
227 def unknown_endtag(self, tag):
228 raise NotImplementedError("Unknow end tag `%s'" % tag)