-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_to_html.py
94 lines (76 loc) · 2.91 KB
/
json_to_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright: error.d
# Date : 2013-12-31
# Create by: error.d<[email protected]>
#
import sys
import json
def usage():
print "usage: %s bookmarks html_bookmarks" % sys.argv[0]
class Bookmark:
def __init__(self):
self._json_bookmark = None
self._html_bookmark = None
self._level = 1
def to_html_url_process(self, url_obj):
self._level += 1
print 'process', '\t'* self._level, 'url name: %s id: %s'% (url_obj['name'], url_obj['id'])
self._html_bookmark += " " * self._level + '<DT><A HREF="%s">%s</A>\n' % (url_obj['url'], url_obj['name'])
self._level -= 1
def to_html_folder_process(self, folder_obj):
self._level += 1
print 'process', '\t' * self._level, 'folder name: %s id: %s' % (folder_obj['name'], folder_obj['id'])
self._html_bookmark += " " * self._level + "<DT><H3>%s</H3>\n" % folder_obj['name']
self._html_bookmark += " " * self._level + "<DL><p>\n"
self.to_html_children_process(folder_obj['children'])
self._html_bookmark += " " * self._level + "</DL><p>\n"
self._level -= 1
def to_html_children_process(self, childrens):
if not childrens:
return
for children in childrens:
if children['type'] == 'folder':
self.to_html_folder_process(children)
elif children['type'] == 'url':
self.to_html_url_process(children)
def to_html_bookmark_process(self, obj):
for bookmark_name, bookmarks in obj.items():
if bookmark_name in ('meta_info'):
continue
self.to_html_children_process(bookmarks['children'])
def to_html(self):
if not self._json_bookmark:
return
self._html_bookmark = """<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><H3 PERSONAL_TOOLBAR_FOLDER="true">Bookmarks Bar</H3>
"""
if 'roots' not in self._json_bookmark:
return
roots = self._json_bookmark['roots']
self.to_html_bookmark_process(roots)
self._html_bookmark += """
</DL><p>"""
def save_html_bookmark(self, html_filepath):
with open(html_filepath, 'w') as html_file:
html_file.write(self._html_bookmark.encode('utf8'))
def open_json_bookmark(self, json_filepath):
with open(json_filepath, 'r') as jb_file:
jb = ''.join(jb_file.readlines())
self._json_bookmark = json.loads(jb)
if __name__ == '__main__':
if len(sys.argv) < 3:
usage()
sys.exit(0)
bookmark = Bookmark()
bookmark.open_json_bookmark(sys.argv[1])
bookmark.to_html()
bookmark.save_html_bookmark(sys.argv[2])