-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
123 lines (111 loc) · 3.9 KB
/
script.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
import re
#lxml
import lxml
from lxml.html import builder as E
from lxml.builder import ElementMaker as EM
input_file = 'input.txt' #File with ierarchy
output_file = 'output.txt' #output with html code
def get_links(lines, position, hierarchy):
links = []
pattern = re.compile('(?<=-)([^\s\$][^\s]*)\s(.+)')
while position < len(lines):
line = lines[position]
line = line.replace('\n', '')
if line == '':
position += 1
continue
match = pattern.search(line)
if match is None:
return links, position
links.append(E.DIV(E.A(match.group(2),
target='main',
href=hierarchy[-2]+'/'+match.group(1)+'/'
)))
position += 1
return links, position
def get_categories(lines, position, hierarchy):
cats = []
pattern = re.compile('(?<=-\$)(.+)')
while position < len(lines):
line = lines[position]
line = line.replace('\n', '')
if line == '':
position += 1
continue
match = pattern.search(line)
if match is None:
return cats, position
hierarchy.append(match.group(1))
links, pos = get_links(lines, position+1, hierarchy)
hierarchy.pop()
cats.append(E.DIV(E.A(E.U(match.group(1)),
id='cat',
onclick="toggle(this, '"+hierarchy[-1]+match.group(1)+"');")))
cats.append(E.DIV(*links,
id = hierarchy[-1]+match.group(1)))
position = pos
return cats, position
def get_boards(lines, position, hierarchy):
divs = []
pattern = re.compile('([^\s]+)\s(.+)')
while position < len(lines):
line = lines[position]
line = line.replace('\n', '')
if line == '':
position += 1
continue
match = pattern.search(line)
if match is None:
return divs, position
hierarchy.append(match.group(1))
cats, pos = get_categories(lines, position+1, hierarchy)
hierarchy.pop()
divs.append(E.DIV(E.CLASS('fib'),
E.A(match.group(2),
id = 'board',
target = 'main',
href = 'http://'+match.group(1)),
E.A('[+]',
id = 'plus',
onclick = "toggle(this, '"+match.group(2)+"');")
)
)
divs.append(E.DIV(*cats,
style = "display: none;",
id = match.group(2)))
position = pos
return divs, position
def get_sections(lines, position, hierarchy):
trs = []
pattern = re.compile('(?<=#)(.+)')
while position < len(lines):
line = lines[position]
line = line.replace('\n', '')
if line == '':
position += 1
continue
match = pattern.search(line)
if match is None:
return trs, position
hierarchy.append(match.group(1))
divs, pos = get_boards(lines, position+1, hierarchy)
hierarchy.pop()
trs.append(E.TR(E.TD(E.CLASS('header'), match.group(1))))
trs.append(E.TR(E.TD(E.CLASS('list'),*divs)))
position = pos
return trs, position
if __name__ == "__main__":
f = open(input_file, 'r')
hierarchy = []
trs, end_pos = get_sections(f.readlines(), 0, hierarchy) #trs are tr sections from table
html = E.TABLE(E.CLASS('category'),
E.TBODY(*trs),
width="100%",
border="0",
cellspacing="0",
cellpadding="0")
html_code = lxml.html.tostring(html)
res_f = open(output_file, 'wb')
res_f.write(html_code)
res_f.close()