Repository navigation
Expand file tree
/
Copy pathminify.py
More file actions
111 lines (91 loc) · 4.52 KB
/
Copy pathminify.py
File metadata and controls
111 lines (91 loc) · 4.52 KB
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
import os
import re
import gzip
from SCons.Script import DefaultEnvironment
Import("env")
# ---------- minify functions ----------
def minify_html(html_code):
html_code = re.sub(r'<!--.*?-->', '', html_code, flags=re.S)
html_code = re.sub(r'\s+', ' ', html_code)
return html_code.strip()
def minify_css(css_code):
css_code = re.sub(r'/\*.*?\*/', '', css_code, flags=re.S)
css_code = re.sub(r'\s+', ' ', css_code)
css_code = re.sub(r'\s*([{}:;,])\s*', r'\1', css_code)
return css_code.strip()
def minify_js(js_code):
js_code = re.sub(r'//.*?\n|/\*.*?\*/', '', js_code, flags=re.S)
js_code = re.sub(r'\s+', ' ', js_code)
return js_code.strip()
# ---------- Get config FIRST ----------
html_input = env.GetProjectOption("custom_html_input", "lib/WebPrefs/html")
html_output = env.GetProjectOption("custom_html_output", "lib/WebPrefs/page.h")
print(f"HTML Input: {html_input}")
print(f"HTML Output: {html_output}")
# ---------- Now define pagedata with actual paths ----------
# The pre/post text wraps each minified block with its structural HTML tags, so
# the concatenated blocks form a valid page (same layout the server used before).
pagedata = {
"outfile": f"{html_output}/page.h",
"infiles": [
f"{html_input}/head.html", "head", "", "",
f"{html_input}/done.html", "done_body", "</head>", "</html>",
f"src/config/settings.html", "settings_body", "</head>", "</html>",
f"{html_input}/script.js", "javascript", '<script type="text/javascript">', "</script>",
f"{html_input}/style.css", "style", "<style>", "</style>"
]
}
def c_array(name, data):
out = [f'static const uint8_t {name}[] PROGMEM = {{']
for i in range(0, len(data), 16):
row = ', '.join(f'0x{b:02x}' for b in data[i:i + 16])
out.append(f' {row},')
out.append('};')
out.append(f'static const unsigned {name}_len = {len(data)};')
return '\n'.join(out)
def before_build():
print("minify.py is running (gzip-embedded) ...")
outfile_path = pagedata['outfile']
infiles = pagedata['infiles']
os.makedirs(os.path.dirname(outfile_path), exist_ok=True)
# Minify each part and keep it (with its structural tags) so we can assemble
# the full pages below.
blocks = {}
for i in range(0, len(infiles), 4):
infile, var_name, pre_txt, post_txt = infiles[i], infiles[i + 1], infiles[i + 2], infiles[i + 3]
if not os.path.exists(infile):
print(f"File not found: {infile}")
continue
with open(infile, 'r', encoding='utf-8') as file:
content = file.read()
if infile.endswith('.html'):
minified_content = minify_html(content)
elif infile.endswith('.css'):
minified_content = minify_css(content)
elif infile.endswith('.js'):
minified_content = minify_js(content)
else:
print(f"unknown type: {infile}")
continue
blocks[var_name] = f'{pre_txt}{minified_content}{post_txt}'
print(f'[OK] {infile} was minified.')
# Assemble the full pages in the exact order the server concatenated the
# blocks before:
# index (settings) = head + javascript + style + settings_body
# done = head + javascript + style + done_body
index_html = blocks.get('head', '') + blocks.get('javascript', '') + \
blocks.get('style', '') + blocks.get('settings_body', '')
done_html = blocks.get('head', '') + blocks.get('javascript', '') + \
blocks.get('style', '') + blocks.get('done_body', '')
# gzip (max compression, mtime=0 -> deterministic output, no needless git
# churn). Served raw with "Content-Encoding: gzip"; the ESP32 never decompresses.
index_gz = gzip.compress(index_html.encode('utf-8'), compresslevel=9, mtime=0)
done_gz = gzip.compress(done_html.encode('utf-8'), compresslevel=9, mtime=0)
with open(outfile_path, 'w') as outfile:
outfile.write('// --- auto-generated by minify.py --- DO NOT EDIT ---\n')
outfile.write('// gzip-compressed settings + done pages, sent with Content-Encoding: gzip.\n')
outfile.write(c_array('index_html_gz', index_gz) + '\n')
outfile.write(c_array('done_html_gz', done_gz) + '\n')
print(f'[OK] page.h written: index {len(index_html)}->{len(index_gz)} B, '
f'done {len(done_html)}->{len(done_gz)} B (gzipped).')
before_build()