build_html.py
1 import os 2 import re 3 4 SRC_DIR = "src" 5 6 def minify_html(html: str) -> str: 7 # Tags where whitespace should be preserved 8 preserve_tags = ['pre', 'code', 'textarea', 'script', 'style'] 9 preserve_regex = '|'.join(preserve_tags) 10 11 # Protect preserve blocks with placeholders 12 preserve_blocks = [] 13 def preserve(match): 14 preserve_blocks.append(match.group(0)) 15 return f"__PRESERVE_BLOCK_{len(preserve_blocks)-1}__" 16 17 html = re.sub(rf'<({preserve_regex})[\s\S]*?</\1>', preserve, html, flags=re.IGNORECASE) 18 19 # Remove HTML comments 20 html = re.sub(r'<!--.*?-->', '', html, flags=re.DOTALL) 21 22 # Collapse all whitespace between tags 23 html = re.sub(r'>\s+<', '><', html) 24 25 # Collapse multiple spaces inside tags 26 html = re.sub(r'\s+', ' ', html) 27 28 # Restore preserved blocks 29 for i, block in enumerate(preserve_blocks): 30 html = html.replace(f"__PRESERVE_BLOCK_{i}__", block) 31 32 return html.strip() 33 34 for root, _, files in os.walk(SRC_DIR): 35 for file in files: 36 if file.endswith(".html"): 37 html_path = os.path.join(root, file) 38 with open(html_path, "r", encoding="utf-8") as f: 39 html_content = f.read() 40 41 # minified = regex.sub("\g<1>", html_content) 42 minified = minify_html(html_content) 43 base_name = f"{os.path.splitext(file)[0]}Html" 44 header_path = os.path.join(root, f"{base_name}.generated.h") 45 46 with open(header_path, "w", encoding="utf-8") as h: 47 h.write(f"// THIS FILE IS AUTOGENERATED, DO NOT EDIT MANUALLY\n\n") 48 h.write(f"#pragma once\n") 49 h.write(f'constexpr char {base_name}[] PROGMEM = R"rawliteral({minified})rawliteral";\n') 50 51 print(f"Generated: {header_path}")