]> git.phdru.name Git - bookmarks_db.git/blob - bkmk_parser.py
Implemented keywords in Mozilla bookmarks.
[bookmarks_db.git] / bkmk_parser.py
1 """
2    Parser for Netscape Navigator's and Mozilla's bookmarks.html
3
4    Written by BroytMann. Copyright (C) 1997-2004 PhiloSoft Design
5 """
6
7
8 import sys, os
9 from m_lib.net.www.html import HTMLParser
10 from bkmk_objects import Folder, Bookmark, Ruler
11
12
13 DEBUG = os.environ.has_key("BKMK_DEBUG")
14
15 if DEBUG:
16    def debug(note):
17       print note
18
19    def dump_names(folder_stack):
20       l = []
21       for object in folder_stack:
22          if object.isFolder:
23             l.append(object.name)
24       return "'%s'" % "' '".join(l)
25
26 else:
27    def debug(note):
28       pass
29    dump_names = debug
30
31
32 DEFAULT_CHARSET = None
33
34 class BkmkParser(HTMLParser):
35    def __init__(self):
36       HTMLParser.__init__(self)
37
38       self.urls = 0
39       self.objects = 0
40
41       self.charset = ""
42       self.recode = None
43
44
45    def handle_data(self, data):
46       if data:
47          if DEFAULT_CHARSET:
48             data = unicode(data, self.charset, "replace").encode(DEFAULT_CHARSET, "replace")
49          self.accumulator += data
50
51
52    # Mozilla - get charset
53    def do_meta(self, attrs):
54       http_equiv = ""
55       content = ""
56
57       for attrname, value in attrs:
58          value = value.strip()
59          if attrname == 'http-equiv':
60             http_equiv = value.lower()
61          elif attrname == 'content':
62             content = value
63
64       if http_equiv == "content-type":
65          try:
66             # extract charset from "text/html; charset=UTF-8"
67             self.charset = content.split('=')[1]
68          except IndexError:
69             pass
70          else:
71             global DEFAULT_CHARSET
72             DEFAULT_CHARSET = sys.getdefaultencoding()
73
74
75    def start_title(self, attrs):
76       self.accumulator += "<TITLE>"
77
78    def end_title(self):
79       self.accumulator += "</TITLE>"
80
81
82    # Start root folder
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]
89
90       self.root_folder.header = self.accumulator.strip()
91       self.accumulator = ''
92
93    def end_h1(self):
94       accumulator = self.accumulator
95       self.accumulator = ''
96
97       debug("Root folder name: `%s'" % accumulator)
98       self.root_folder.name = accumulator
99
100
101    # Start a folder
102    def start_h3(self, attrs):
103       for attrname, value in attrs:
104          value = value.strip()
105          if attrname == 'add_date':
106             add_date = value
107
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
114       self.objects += 1
115
116    def end_h3(self):
117       accumulator = self.accumulator
118       self.accumulator = ''
119
120       debug("Folder name: `%s'" % accumulator)
121       self.current_folder.name = accumulator
122
123
124    # Start a bookmark
125    def start_a(self, attrs):
126       last_visit = None
127       last_modified = None
128       keyword = None
129
130       for attrname, value in attrs:
131          value = value.strip()
132          if attrname == "href":
133             href = value
134          elif attrname == "add_date":
135             add_date = value
136          elif attrname == "last_visit":
137             last_visit = value
138          elif attrname == "last_modified":
139             last_modified = value
140          elif attrname == "shortcuturl":
141             keyword = value
142
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)
147       self.urls += 1
148       self.objects += 1
149
150    def end_a(self):
151       accumulator = self.accumulator
152       self.accumulator = ''
153
154       debug("Bookmark name: `%s'" % accumulator)
155       bookmark = self.current_folder[-1]
156       bookmark.name = accumulator
157
158
159    def flush(self):
160       accumulator = self.accumulator
161
162       if accumulator:
163          self.accumulator = ''
164
165          current_object = self.current_object
166          if current_object:
167             current_object.comment += accumulator.strip()
168             debug("Comment: `%s'" % current_object.comment)
169
170
171    def start_dl(self, attrs):
172       self.flush()
173
174    do_dt = start_dl
175
176
177    # End of folder
178    def end_dl(self):
179       self.flush()
180       debug("End folder")
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]
186          else:
187             debug("FOLDER STACK is EMPTY!!! (1)")
188       else:
189          debug("FOLDER STACK is EMPTY!!! (2)")
190       self.current_object = None
191
192
193    def close(self):
194       HTMLParser.close(self)
195       if self.folder_stack:
196          raise ValueError, "wrong folder stack: %s" % self.folder_stack
197
198
199    def do_dd(self, attrs):
200       pass
201
202    do_p = do_dd
203
204
205    # Start ruler
206    def do_hr(self, attrs):
207       self.flush()
208       debug("Ruler")
209       self.current_folder.append(Ruler())
210       self.current_object = None
211       self.objects += 1
212
213
214    # BR in comment
215    def do_br(self, attrs):
216       self.accumulator += "<BR>"
217
218
219    # Allow < in the text
220    def unknown_starttag(self, tag, attrs):
221       self.accumulator += "<%s>" % tag
222
223
224    # Do not allow unknow end tags
225    def unknown_endtag(self, tag):
226       raise NotImplementedError("Unknow end tag `%s'" % tag)