-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt2listHtml.py
34 lines (31 loc) · 1.24 KB
/
txt2listHtml.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
import sys,os
import codecs
import math
import json
file='./json_list.txt'
def txt2html(file):
print('txt2html:%s'%file)
with codecs.open(file,'r','utf-8') as f:
lines = f.readlines()
json_list =[]
last_tab_parent = dict()
for index,line in enumerate(lines):
line = line.rstrip()
line = line.replace(' ',"\t")
offset = line.count('\t')
color = colors[offset]
if offset == 0:
last_tab_parent[0] = "root"
json_list.append({"id":"root","isroot":True,"topic":line.strip()})
else:
id = "line_%d"%index
last_tab_parent[offset]=id
if offset >expand_limit:
json_list.append({"id":id,"parentid":last_tab_parent[offset-1],"topic":line.strip(),"background-color":color,"expanded":False})
else:
json_list.append({"id":id,"parentid":last_tab_parent[offset-1],"topic":line.strip(),"background-color":color})
to_file=os.path.splitext(file)[0]+'.json'
print('write to:%s'%to_file)
with codecs.open(to_file,'w','utf-8') as f:
json.dump(json_list,f,indent=4)
txt2html(file)