]> git.phdru.name Git - bookmarks_db.git/blob - Writers/bkmk_whtml.py
Style: Fix flake8 E501 line too long
[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 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2000-2023 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['writer_html']
11
12
13 from m_lib.defenc import default_encoding
14 from bkmk_objects import Writer, BKMK_FORMAT, quote_title
15
16
17 def dump_comment(comment):
18     comment = comment.replace("<BR>\n", "\n")
19     if BKMK_FORMAT == "NETSCAPE":
20         comment = comment.replace("\n", "<BR>\n")
21     return comment
22
23
24 ind_s = " "*4
25
26
27 class writer_html(Writer):
28     filename = "bookmarks.html"
29
30     def _folder(self, f, level):
31         if f.comment: self.outfile.write('<DD>%s\n' % dump_comment(f.comment))
32         self.outfile.write(ind_s*level + "<DL><p>\n")
33
34     def root_folder(self, f):
35         self.outfile.write("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n")
36         self.outfile.write(f.header + "\n")
37         self.outfile.write('<H1>%s</H1>\n\n' % quote_title(f.name))
38         self._folder(f, 0)
39
40     def start_folder(self, f, level):
41         self.outfile.write(ind_s*level + '<DT><H3 ADD_DATE="%s"' % f.add_date)
42         if (BKMK_FORMAT == "MOZILLA") and f.last_modified:
43             self.outfile.write(' LAST_MODIFIED="%s"' % f.last_modified)
44         self.outfile.write('>%s</H3>\n' % quote_title(f.name))
45         self._folder(f, level)
46
47     def end_folder(self, f, level):
48         self.outfile.write(ind_s*level + "</DL><p>\n")
49
50     def bookmark(self, b, level):
51         self.outfile.write(ind_s*(level+1) + '<DT><A HREF="%s" ADD_DATE="%s"'
52                            % (b.href, b.add_date))
53         if b.last_visit: self.outfile.write(' LAST_VISIT="%s"' % b.last_visit)
54         if b.last_modified:
55             self.outfile.write(' LAST_MODIFIED="%s"' % b.last_modified)
56         if BKMK_FORMAT == "MOZILLA":
57             if b.keyword: self.outfile.write(' SHORTCUTURL="%s"' % b.keyword)
58             if b.icon_href:
59                 value = b.icon_href
60                 if isinstance(value, unicode):
61                     value = value.encode('utf-8')
62                 self.outfile.write(' ICON_URI="%s"' % value)
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")