Embed gzipped web assets at build time and type LED edit commands.
Generate the web UI headers from c/web/ via scripts/embed_file.py (minify + gzip) and serve them with Content-Encoding, replacing the checked-in web_*.h blobs. Move the LED driver/presets into lib/leds, and parse preset edits into a typed cmd_preset_t on core0 instead of passing raw JSON to core1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3e9fc9663
commit
bf0429c505
19 files changed
+395
-2205
No files matched your search
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Minify + gzip a web asset, then emit a C byte-array header."""
|
||||
from __future__ import annotations
|
||||
|
||||
import gzip
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def minify_html(text: str) -> str:
|
||||
text = re.sub(r"<!--.*?-->", "", text, flags=re.DOTALL)
|
||||
text = re.sub(r">\s+<", "><", text)
|
||||
text = re.sub(r"\s+", " ", text)
|
||||
return text.strip()
|
||||
|
||||
|
||||
def minify_css(text: str) -> str:
|
||||
text = re.sub(r"/\*.*?\*/", "", text, flags=re.DOTALL)
|
||||
text = re.sub(r"\s+", " ", text)
|
||||
text = re.sub(r"\s*([{};:,>~+])\s*", r"\1", text)
|
||||
text = re.sub(r";}", "}", text)
|
||||
return text.strip()
|
||||
|
||||
|
||||
def minify_js(text: str, src: Path) -> str:
|
||||
terser = shutil.which("terser")
|
||||
cmd = None
|
||||
if terser:
|
||||
cmd = [terser, str(src), "--compress", "--mangle"]
|
||||
elif shutil.which("npx"):
|
||||
cmd = ["npx", "--yes", "terser", str(src), "--compress", "--mangle"]
|
||||
if cmd:
|
||||
try:
|
||||
r = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
||||
if r.stdout.strip():
|
||||
return r.stdout
|
||||
except (subprocess.CalledProcessError, OSError) as e:
|
||||
print(f"embed_file: terser failed ({e}); using light JS minify", file=sys.stderr)
|
||||
|
||||
# Light fallback: drop block/line comments and collapse blank lines.
|
||||
text = re.sub(r"/\*.*?\*/", "", text, flags=re.DOTALL)
|
||||
text = re.sub(r"(?m)^\s*//.*$", "", text)
|
||||
text = re.sub(r"\n\s*\n+", "\n", text)
|
||||
return text.strip() + "\n"
|
||||
|
||||
|
||||
def minify(src: Path, data: bytes) -> bytes:
|
||||
suffix = src.suffix.lower()
|
||||
text = data.decode("utf-8")
|
||||
if suffix in (".html", ".htm"):
|
||||
return minify_html(text).encode("utf-8")
|
||||
if suffix == ".css":
|
||||
return minify_css(text).encode("utf-8")
|
||||
if suffix == ".js":
|
||||
return minify_js(text, src).encode("utf-8")
|
||||
return data
|
||||
|
||||
|
||||
def write_header(out: Path, symbol: str, src: Path, data: bytes, raw_len: int, min_len: int) -> None:
|
||||
lines = [
|
||||
f"/* auto-generated from {src} (minified+gzip) — do not edit */",
|
||||
f"/* raw={raw_len} minified={min_len} gzipped={len(data)} */",
|
||||
"#pragma once",
|
||||
"#include <stddef.h>",
|
||||
"#include <stdint.h>",
|
||||
f"static const uint8_t {symbol}[] = {{",
|
||||
]
|
||||
for i in range(0, len(data), 16):
|
||||
chunk = ", ".join(f"0x{b:02x}" for b in data[i : i + 16])
|
||||
lines.append(f" {chunk},")
|
||||
lines.append("};")
|
||||
lines.append(f"static const size_t {symbol}_len = sizeof({symbol});")
|
||||
lines.append("")
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
out.write_text("\n".join(lines), encoding="utf-8")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) != 4:
|
||||
print(f"usage: {sys.argv[0]} <input> <symbol> <output.h>", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
src = Path(sys.argv[1])
|
||||
symbol = sys.argv[2]
|
||||
out = Path(sys.argv[3])
|
||||
raw = src.read_bytes()
|
||||
mini = minify(src, raw)
|
||||
gz = gzip.compress(mini, compresslevel=9, mtime=0)
|
||||
write_header(out, symbol, src, gz, len(raw), len(mini))
|
||||
print(f"embed {src.name}: {len(raw)} -> {len(mini)} min -> {len(gz)} gzip")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user