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