-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrammar.py
56 lines (43 loc) · 1.83 KB
/
grammar.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 __future__ import print_function
from __future__ import absolute_import
import os
import json
from dom import *
# BASE_DIR_NAME = os.path.dirname(__file__)
DSL_MAPPING_FILEPATH = "dsl-mapping.json"
class Grammar:
def __init__(self):
style_json = DSL_MAPPING_FILEPATH
with open(style_json) as data_file:
self.dsl_mapping = json.load(data_file)
self.opening_tag = self.dsl_mapping["opening-tag"]
self.closing_tag = self.dsl_mapping["closing-tag"]
self.content_holder = self.opening_tag + self.closing_tag
self.root = Dom("body", None, self.content_holder)
def compile(self, generated_gui):
dsl_file = generated_gui
#Parse fix
dsl_file = dsl_file[1:-1]
dsl_file = ' '.join(dsl_file)
dsl_file = dsl_file.replace('{', '{8').replace('}', '8}8')
dsl_file = dsl_file.replace(' ', '')
dsl_file = dsl_file.split('8')
dsl_file = list(filter(None, dsl_file))
current_parent = self.root
for token in dsl_file:
token = token.replace(" ", "").replace("\n", "")
if token.find(self.opening_tag) != -1:
token = token.replace(self.opening_tag, "")
element = Dom(token, current_parent, self.content_holder)
current_parent.add_child(element)
current_parent = element
elif token.find(self.closing_tag) != -1:
current_parent = current_parent.parent
else:
tokens = token.split(",")
for t in tokens:
element = Dom(t, current_parent, self.content_holder)
current_parent.add_child(element)
output_html = self.root.render(self.dsl_mapping)
if output_html is None: return "HTML Parsing Error"
return output_html