1 """Parser for Netscape Navigator's and Mozilla's bookmarks.html
3 This file is a part of Bookmarks database and Internet robot.
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 1997-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
13 __all__ = ['BkmkParser']
17 from m_lib.net.www.html import HTMLParser
18 from bkmk_objects import DEFAULT_CHARSET, Folder, Bookmark, Ruler
21 DEBUG = os.environ.has_key("BKMK_DEBUG")
27 def dump_names(folder_stack):
29 for object in folder_stack:
32 return "'%s'" % "' '".join(l)
40 class BkmkParser(HTMLParser):
42 HTMLParser.__init__(self)
50 def handle_data(self, data):
52 if self.charset and DEFAULT_CHARSET:
53 data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "xmlcharrefreplace")
54 self.accumulator += data
56 # Mozilla - get charset
57 def do_meta(self, attrs):
61 for attrname, value in attrs:
63 if attrname == 'http-equiv':
64 http_equiv = value.lower()
65 elif attrname == 'content':
68 if http_equiv == "content-type":
70 # extract charset from "text/html; charset=UTF-8"
71 self.charset = content.split('=')[1]
75 def start_title(self, attrs):
77 self.accumulator += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%s">\n' % DEFAULT_CHARSET
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 or '', '', icon, charset)
155 self.current_object = bookmark
156 self.current_folder.append(bookmark)
161 accumulator = self.accumulator
162 self.accumulator = ''
164 debug("Bookmark name: `%s'" % accumulator)
165 bookmark = self.current_folder[-1]
166 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)
179 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
200 HTMLParser.close(self)
201 if self.folder_stack:
202 raise ValueError, "wrong folder stack: %s" % self.folder_stack
204 def do_dd(self, attrs):
210 def do_hr(self, attrs):
213 self.current_folder.append(Ruler())
214 self.current_object = None
218 def do_br(self, attrs):
219 self.accumulator += "<BR>"
221 # Allow < in the text
222 def unknown_starttag(self, tag, attrs):
223 self.accumulator += "<%s>" % tag
225 # Do not allow unknow end tags
226 def unknown_endtag(self, tag):
227 raise NotImplementedError("Unknow end tag `%s'" % tag)