-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpre_build_web.py
126 lines (97 loc) · 4.27 KB
/
pre_build_web.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
#!/usr/bin/python
# pip install htmlmin
# pip install cssmin
# pip install jsmin
# if platformIO has a different python instance, use this C:\Users\redma\.platformio\penv\Scripts\pip.exe install htmlmin
import os
import re
import binascii
import subprocess
input_dir = "html" # Sub folder of webfiles
output_file = "src/webcontent.h"
# version and revision extract
version = "undefined"
revision = "rev_NO_GIT"
try:
revision = (
subprocess.check_output(["git", "rev-list", "--count", "HEAD"])
.strip()
.decode("utf-8")
)
except:
print ("No Git installed. Version number will be skipped in a version display")
defFile = open("src/definitions.h")
for line in defFile:
if line.startswith("#define MAJOR_VERSION"):
version = line.replace("#define MAJOR_VERSION ", "")
version = version.strip()
if line.startswith("#define BOARD_NAME"):
boardName = line.replace("#define BOARD_NAME ", "").replace("\"","").replace(' ', '').replace('\t', '').replace('\n\n', '\n').replace('\n', '')
boardname = version.strip()
defFile.close()
##############################################
f_output = open(output_file, "w")
f_output.write("// This file is autogenerated. DO NOT MODIFY!!!\n\n")
def replaceWildCards(string):
string = string.replace("%BOARD_NAME%", boardName);
string = string.replace("%VERSION%", "v" + version + " - " + revision );
return string
def write_to_file(file, data, dir=""):
filename, file_extension = os.path.splitext(file) # Split filename and file extension
file_extension = file_extension.replace(".","") # Remove puncuation in file extension
dir = dir.replace(input_dir,"") # Remove the first directory(input_dir)
dir = dir.replace("\\","/") # Change to /
if (dir == "/index."):
dir="/index.html"
f_output.write("// " + dir + "\n") # Print comment
f_output.write("const char* const data_" + filename + "_" + file_extension + "_path PROGMEM = \""+str(dir)+"\";\n") # print path
f_output.write("const char data_"+filename+"_"+file_extension+"[] PROGMEM = "+data+"\n\n") # print binary data
# f_output.write("#define data_" + filename + "_len " + str(data.count('0x')) +"\n")
def aschii2Hex(text):
output_str = ""
x = 1
strLen = len(text)
for character in text:
output_str += hex(ord(character))
if (x != strLen):
output_str += ","
x += 1
return output_str
def minify_js(input_file):
data = "R\"=====(\n"
sourceFile = open (input_file, "r")
with open (input_file, "r") as sourceFile:
data+=replaceWildCards(sourceFile.read().replace(' ', '').replace('\t', '').replace('\n\n', '\n'))
sourceFile.close()
data += "\n)=====\";"
return data
def minify_html(input_file):
data = "R\"=====(\n"
with open (input_file, "r") as sourceFile:
data+=replaceWildCards(sourceFile.read().replace(' ', '').replace('\t', '').replace('\n\n', '\n'))
sourceFile.close()
data += "\n)=====\";"
return data
def minify_css(input_file):
data = "R\"=====(\n"
with open (input_file, "r") as sourceFile:
data+=replaceWildCards(sourceFile.read().replace(' ', '').replace('\t', '').replace('\n\n', '\n'))
sourceFile.close()
data += "\n)=====\";"
return data
for root, dirs, files in os.walk(input_dir, topdown=False):
for name in sorted(files): # for files
print("Processing %s" % (name))
if name.endswith(".js"):
print(os.path.join(root, name))
minified = minify_js(os.path.join(root, name)) # minify javascript
write_to_file(name, minified, os.path.join(root, name)) # write to file
elif name.endswith(".html"):
print(os.path.join(root, name))
minified = minify_html(os.path.join(root, name)) # minify html
write_to_file(name, minified, os.path.join(root, name)) # write to file
elif name.endswith(".css"):
print(os.path.join(root, name))
minified = minify_css(os.path.join(root, name)) # minify css
write_to_file(name, minified, os.path.join(root, name)) # write to file
f_output.close()