]> git.phdru.name Git - bookmarks_db.git/blob - Writers/bkmk_whtml.py
1e933faf0904a3a4314e2ce4f836da47ad76f295
[bookmarks_db.git] / Writers / bkmk_whtml.py
1 """Convert a bkmk database back to bookmarks.html
2
3 This file is a part of Bookmarks database and Internet robot.
4 """
5
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) 2000-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
12
13 __all__ = ['writer_html']
14
15
16 from m_lib.defenc import default_encoding
17 from bkmk_objects import Writer, BKMK_FORMAT, quote_title
18
19
20 def dump_comment(comment):
21    comment = comment.replace("<BR>\n", "\n")
22    if BKMK_FORMAT == "NETSCAPE":
23       comment = comment.replace("\n", "<BR>\n")
24    return comment
25
26 def netscape_date(date):
27    if not date or (BKMK_FORMAT == "MOZILLA"):
28       return date
29    else:
30       return int(date)/(10**6)
31
32 ind_s = " "*4
33
34 class writer_html(Writer):
35    filename = "bookmarks.html"
36
37    def _folder(self, f, level):
38       if f.comment: self.outfile.write('<DD>%s\n' % dump_comment(f.comment))
39       self.outfile.write(ind_s*level + "<DL><p>\n")
40
41    def root_folder(self, f):
42       self.outfile.write("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n")
43       self.outfile.write(f.header + "\n")
44       self.outfile.write('<H1>%s</H1>\n\n' % quote_title(f.name))
45       self._folder(f, 0)
46
47    def start_folder(self, f, level):
48       self.outfile.write(ind_s*level + '<DT><H3 ADD_DATE="%s"' % netscape_date(f.add_date))
49       if (BKMK_FORMAT == "MOZILLA") and f.last_modified: self.outfile.write(' LAST_MODIFIED="%s"' % netscape_date(f.last_modified))
50       self.outfile.write('>%s</H3>\n' % quote_title(f.name))
51       self._folder(f, level)
52
53    def end_folder(self, f, level):
54       self.outfile.write(ind_s*level + "</DL><p>\n")
55
56    def bookmark(self, b, level):
57       self.outfile.write(ind_s*(level+1) + '<DT><A HREF="%s" ADD_DATE="%s"' % (b.href, netscape_date(b.add_date)))
58       if b.last_visit: self.outfile.write(' LAST_VISIT="%s"' % netscape_date(b.last_visit))
59       self.outfile.write(' LAST_MODIFIED="%s"' % netscape_date(b.last_modified))
60       if BKMK_FORMAT == "MOZILLA":
61          if b.keyword: self.outfile.write(' SHORTCUTURL="%s"' % b.keyword)
62          if b.icon_href: self.outfile.write(' ICON_URI="%s"' % b.icon_href)
63          if b.icon: self.outfile.write(' ICON="%s"' % b.icon)
64          if b.charset: self.outfile.write(' LAST_CHARSET="%s"' % b.charset)
65       self.outfile.write('>%s</A>\n' % quote_title(b.name))
66       if b.comment: self.outfile.write('<DD>%s\n' % dump_comment(b.comment))
67
68    def ruler(self, r, level):
69       self.outfile.write(ind_s*(level+1) + "<HR>\n")