-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
58 lines (49 loc) · 1.45 KB
/
tree.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
import sys
from data import tree, ID
BEAUTIFUL_SOUP = True
try:
from bs4 import BeautifulSoup as BS
BEAUTIFUL_SOUP = True
except ImportError:
pass
FILENAME = ""
if len(sys.argv) >= 2:
FILENAME = sys.argv[1]
def _generate_tree(tup):
html = ""
html += "<li role=\"treeitem\" tabindex=\"-1\">"
if type(tup) == str:
html += "<span id=\"{1}_{2}\">{0}</span></li>".format(tup, ID, tup.split(" ")[0])
return html
html += "<span id=\"{1}_{2}\">{0}</span>".format(tup[0], ID, tup[0].split(" ")[0])
if len(tup[1]) > 0:
html += "<ul role=\"group\">"
for i in tup[1]:
html += _generate_tree(i)
html += "</ul>"
html += "</li>"
return html
def generate_tree(treeobj):
html = "<ul role=\"tree\">"
html += _generate_tree(treeobj)
html += "</ul>"
return html
output = generate_tree(tree[0])
final_output = ""
if FILENAME != "":
final_output += "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"></head><body>"
final_output += output
final_output += "</body></html>"
else:
final_output += "<!-- AUTOGENERATED HTML FROM PYTHON TOOL https://git.bytetools.ca/bytetools/accessible-tree -->"
final_output += output
final_output += "<!-- END OF AUTOGENERATED HTML FROM PYTHON TOOL https://git.bytetools.ca/bytetools/accessible-tree -->"
if BEAUTIFUL_SOUP:
bs = BS(final_output, "html.parser")
final_output = bs.prettify()
if FILENAME != "":
f = open(FILENAME, "w")
f.write(final_output)
f.close()
else:
print(final_output)