mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-18 23:27:44 +03:00
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import os
|
|
import re
|
|
|
|
SRC_DIR = "src"
|
|
|
|
def minify_html(html: str) -> str:
|
|
# Tags where whitespace should be preserved
|
|
preserve_tags = ['pre', 'code', 'textarea']
|
|
preserve_regex = '|'.join(preserve_tags)
|
|
|
|
# Protect preserve blocks with placeholders
|
|
preserve_blocks = []
|
|
def preserve(match):
|
|
preserve_blocks.append(match.group(0))
|
|
return f"__PRESERVE_BLOCK_{len(preserve_blocks)-1}__"
|
|
|
|
html = re.sub(rf'<({preserve_regex})[\s\S]*?</\1>', preserve, html, flags=re.IGNORECASE)
|
|
|
|
# Remove HTML comments
|
|
html = re.sub(r'<!--.*?-->', '', html, flags=re.DOTALL)
|
|
|
|
# Collapse all whitespace between tags
|
|
html = re.sub(r'>\s+<', '><', html)
|
|
|
|
# Collapse multiple spaces inside tags
|
|
html = re.sub(r'\s+', ' ', html)
|
|
|
|
# Restore preserved blocks
|
|
for i, block in enumerate(preserve_blocks):
|
|
html = html.replace(f"__PRESERVE_BLOCK_{i}__", block)
|
|
|
|
return html.strip()
|
|
|
|
for root, _, files in os.walk(SRC_DIR):
|
|
for file in files:
|
|
if file.endswith(".html"):
|
|
html_path = os.path.join(root, file)
|
|
with open(html_path, "r", encoding="utf-8") as f:
|
|
html_content = f.read()
|
|
|
|
# minified = regex.sub("\g<1>", html_content)
|
|
minified = minify_html(html_content)
|
|
base_name = f"{os.path.splitext(file)[0]}Html"
|
|
header_path = os.path.join(root, f"{base_name}.generated.h")
|
|
|
|
with open(header_path, "w", encoding="utf-8") as h:
|
|
h.write(f"// THIS FILE IS AUTOGENERATED, DO NOT EDIT MANUALLY\n\n")
|
|
h.write(f"#pragma once\n")
|
|
h.write(f'constexpr char {base_name}[] PROGMEM = R"rawliteral({minified})rawliteral";\n')
|
|
|
|
print(f"Generated: {header_path}")
|