-
Notifications
You must be signed in to change notification settings - Fork 0
/
BS4GenUtils.py
56 lines (50 loc) · 2.1 KB
/
BS4GenUtils.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
from parglare import NodeNonTerm, NodeTerm
import sys
TAG_CLASSES = {
'TAG_CONTF': "container-fluid",
'TAG_CONT': "container",
'TAG_ROW': "row",
'TAG_COL': "col",
}
INDENT_NAMES = [
"ROW_LIST",
"COL_LIST",
"ROW_EXPR"
]
def pretty_print(ast, indentation=-1):
for node in ast:
if type(node) is NodeNonTerm:
if node.symbol.name in TAG_CLASSES.keys():
tag_content = "".join([x.value for x in node.children])
indentation_str = "\t" * indentation
print(f"{indentation_str}{tag_content}")
else:
if node.symbol.name in INDENT_NAMES:
pretty_print(node.children, indentation + 1)
else:
pretty_print(node.children, indentation)
def generate_html(ast, fout=sys.stdout, context_stack=[]):
for node in ast:
if type(node) is NodeNonTerm:
context_name = node.symbol.name
if context_name in TAG_CLASSES.keys():
indentation_str = '\t' * (len(context_stack) + 2)
if context_name != "TAG_COL":
context_stack.append(context_name)
fout.write(f"{indentation_str}<div class='{TAG_CLASSES[context_name]}'>\n")
else:
if context_stack[-1] != 'COL_ROW':
fout.write(f"{indentation_str}<div class='{TAG_CLASSES[context_name]}'></div>\n")
else:
indentation_str = '\t' * (len(context_stack) + 1)
fout.write(f"{indentation_str}<div class='{TAG_CLASSES[context_name]}'>\n")
else:
if context_name == 'COL_ROW':
context_stack.append(context_name)
generate_html(node.children, fout, context_stack)
elif type(node) is NodeTerm:
if node.symbol.name == "expr_close":
if len(context_stack) > 0:
context_stack.pop()
indentation_str = "\t" * (len(context_stack) + 2)
fout.write(f"{indentation_str}</div>\n")