Last active
October 1, 2025 17:10
-
-
Save Staars/0ccfd94bc0aa8d51bf3b10f087bd09d3 to your computer and use it in GitHub Desktop.
make BROTLI in file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import gzip, sys, re, textwrap | |
| from pathlib import Path | |
| import htmlmin, rcssmin, rjsmin, brotli | |
| OPEN_PATTERN = re.compile(r"/\*\s*STATIC_(\w+)\s+([A-Za-z_]\w*)\s*\n", re.S) | |
| STYLE_RE = re.compile(r"(<style[^>]*>)(.*?)(</style>)", re.S|re.I) | |
| SCRIPT_RE = re.compile(r"(<script[^>]*>)(.*?)(</script>)", re.S|re.I) | |
| def multi_pass_minify(text: str) -> str: | |
| # Minify CSS inside <style> blocks | |
| def css_repl(m): | |
| return m.group(1) + rcssmin.cssmin(m.group(2)) + m.group(3) | |
| text = STYLE_RE.sub(css_repl, text) | |
| # Minify JS inside <script> blocks | |
| def js_repl(m): | |
| return m.group(1) + rjsmin.jsmin(m.group(2)) + m.group(3) | |
| text = SCRIPT_RE.sub(js_repl, text) | |
| # Minify the HTML structure itself | |
| text = htmlmin.minify( | |
| text, | |
| remove_comments=True, | |
| remove_empty_space=True | |
| ) | |
| return text.strip() | |
| def to_c_array_body(data: bytes) -> str: | |
| hex_bytes = [f"0x{b:02x}" for b in data] | |
| lines = [", ".join(hex_bytes[i:i+16]) for i in range(0, len(hex_bytes), 16)] | |
| body = ",\n ".join(lines) | |
| return textwrap.dedent(f"""\ | |
| /* AUTO-GENERATED (BEGIN) */ | |
| {body} | |
| /* AUTO-GENERATED (END) */ | |
| """) | |
| def main(header_path: str): | |
| header = Path(header_path) | |
| text = header.read_text(encoding="utf-8") | |
| m_open = OPEN_PATTERN.search(text) | |
| if not m_open: | |
| sys.exit("No opening STATIC_* VAR_NAME marker found.") | |
| block_type, varname = m_open.group(1), m_open.group(2) | |
| close_marker = rf"END_STATIC_{block_type}" | |
| start_idx = m_open.end() | |
| m_close = re.search(close_marker, text[start_idx:], re.S) | |
| if not m_close: | |
| sys.exit(f"No closing marker {close_marker} found.") | |
| end_idx = start_idx + m_close.start() | |
| block = text[start_idx:end_idx].strip() | |
| # Multi-pass minification | |
| minified_block = multi_pass_minify(block) | |
| gzip_data = gzip.compress(minified_block.encode("utf-8"), compresslevel=9) | |
| brotli_data = brotli.compress(minified_block.encode("utf-8")) | |
| c_body = to_c_array_body(brotli_data) | |
| new_text, n = re.subn( | |
| r"/\* AUTO-GENERATED \(BEGIN\) \*/[\s\S]*?/\* AUTO-GENERATED \(END\) \*/", | |
| c_body.strip(), | |
| text, | |
| count=1 | |
| ) | |
| if n == 0: | |
| sys.exit("No AUTO-GENERATED (BEGIN/END) section found to replace.") | |
| new_text = re.sub( | |
| rf"(const|constexpr)\s+unsigned\s+int\s+{varname}_len\s*=\s*sizeof\({varname}\);", | |
| f"constexpr unsigned int {varname}_len = sizeof({varname});", | |
| new_text | |
| ) | |
| header.write_text(new_text, encoding="utf-8") | |
| print(f"Updated {header} for {varname} (type {block_type}).") | |
| print(f"Original size: {len(block)} bytes") | |
| print(f"Minified size: {len(minified_block)} bytes") | |
| print(f"GZIP compressed: {len(gzip_data)} bytes → {len(gzip_data)/len(minified_block):.2%} of minified") | |
| print(f"Brotli compressed: {len(brotli_data)} bytes → {len(brotli_data)/len(minified_block):.2%} of minified") | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 2: | |
| print("Usage: update_static_page.py <headerfile.h>") | |
| sys.exit(1) | |
| main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment