-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtmlx.py
155 lines (124 loc) · 4.79 KB
/
htmlx.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import sys,os
import json,re
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Watcher:
def __init__(self,path):
self.observer = Observer()
self.path = path
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.path, recursive=True)
self.observer.start()
try:
while True:
time.sleep(10)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
print("Received created event - %s." % event.src_path)
elif event.event_type == 'modified':
# Taken any action here when a file is modified.
path, fname = os.path.split(event.src_path)
if "dist" not in path:
print("Received modified event - %s." % event.src_path)
global mode
complileDir(path,mode=mode)
def strip_lines(content):
exclude_list = [r"console\.log\(.*\);?","debugger;?"]
m = re.search("|".join(exclude_list),content)
if m:
print("Found expression in exclude_list",m.group(0))
content = content.replace(m.group(0),"") ##Replace matched with empty string
return content
def compileHTML(directory,filename,mode):
try:
fin = os.path.join(directory, filename)
content = open(fin).readlines()
except:
return "Cannot Read Input File",directory,filename
# Show the file contents line by line.
# We added the comma to print single newlines and not double newlines.
# This is because the lines contain the newline character '\n'.
print("Got mode",mode)
for i in range(len(content)):
if mode != "debug":
content[i] = strip_lines(content[i])
if "##" not in content[i]:
continue
print("found parser expression",content[i])
# take everything after ## then split it by whitespace
try:
expression = content[i].split("##")[1]
command = expression.split()[0]
loc = expression.split()[1]
if "(" in loc:
value = loc.split("(")[0]
args = loc.split("(")[1].replace(")",'').strip()
try:
args = json.loads(args)
except:
print("Unable to parse from params",args)
raise Exception('Invalid params',expression)
else:
value = loc
args = {}
print(command,value,args)
except:
print("Unidentified expression")
continue
if command == "include":
try:
layout_file = os.path.join(directory, value)
content[i] = open(layout_file).read()
layout_content = content[i].split("\n")
for j in range(len(layout_content)):
if mode != "debug":
layout_content[j] = strip_lines(layout_content[j])
content[i] = "\n".join(layout_content)
for key,value in args.items():
content[i] = content[i].replace("$"+key,value)
#content[i] = content[i].encode('utf-8','ignore')
except:
print("unable to include file: ",layout_file)
content = ''.join(content)
try:
fout = open(directory+"/dist/"+filename.replace(".htmlx",".html"),"w+")
fout.write(content)
except:
print("unable to open file",directory+"/dist/"+filename.replace(".htmlx",".html"))
return "Sucess..."
def complileDir(directory,mode):
for filename in os.listdir(directory):
if filename.endswith(".htmlx") or filename.endswith(".py"):
print(filename)
print(compileHTML(directory,filename,mode))
# print()
continue
else:
continue
if __name__=="__main__":
try:
if sys.argv[4] == "prod":
mode = "prod"
elif sys.argv[4] == "debug":
mode = "debug"
else:
print("Unkown mode. use -m debug or prod",sys.argv[4] )
sys.exit(0)
if sys.argv[1] == "-w":
w = Watcher(sys.argv[2])
w.run()
else:
complileDir(sys.argv[2],mode)
except Exception:
print("htmlx -w[Watch] -c[Complile] directory -m[prod | debug]")