#!/usr/bin/env python3 """Build the dependency-free Ravenous Effects Library static site.""" import argparse import hashlib import json import re import shutil import sys import tempfile from pathlib import Path import yaml ROOT = Path(__file__).resolve().parents[2] WEB = ROOT / "effects_web" SRC = WEB / "src" SOURCE = ROOT / "ravenous_typst/data/src/glossaries/alterations.yaml" sys.path.insert(0, str(ROOT)) from tools.build import (_replace_balanced_calls, effect_reminder_metadata, # noqa: E402 read_glossary_labels, read_theme_palettes, translate_typst) def translate_alteration(text: str, glossary: dict[str, str]) -> str: """Render alteration-only macros before the shared conservative translator.""" text = re.sub(r'#reference\(<[^>]+>,\s*fallback:\s*\[([^]]+)]\)', r'\1', text) def alteration_mark(arguments: str) -> str: positional = re.findall(r'(? list[dict]: glossary = read_glossary_labels() rows = yaml.safe_load(SOURCE.read_text(encoding="utf-8")) or [] effects = [] for row in rows: key = str(row.get("key", "")).strip() if not key.startswith("al_") or row.get("group") == "Types d'altérations": continue custom = row.get("custom") if isinstance(row.get("custom"), dict) else {} raw_description = str(row.get("description", "")) description = translate_alteration(raw_description, glossary) category = translate_typst(str(custom.get("category", "")), glossary) timing = effect_reminder_metadata(description) effects.append({ "id": key, "effect_id": key, "name": str(row.get("short") or key), "group": str(row.get("group") or "Altérations"), "description": description, "resisted_by": str(custom.get("resisted_by", "")), "difficulty": custom.get("difficulty", ""), "category": category, "type": "alteration", "enabled": True, "duration": "", "duration_type": timing.get("duration_type", "manual"), "remaining": timing.get("remaining", 0), "countdown_phase": timing.get("countdown_phase", "end"), "start_reminder": timing.get("start_reminder", ""), "end_reminder": timing.get("end_reminder", ""), "apply_reminder": "", "removal_reminder": "", }) return effects def theme_css(palettes: dict[str, dict[str, str]]) -> str: keys = {"ink": "ink", "muted": "muted-ink", "gold": "gold", "dark-gold": "dark-gold", "paper": "parchment", "surface": "parchment-light", "accent": "parchment-accent", "border": "border", "blood": "blood"} return "\n".join( f'[data-theme="{name}"]{{' + "".join(f"--{css}:{palette[source].lower()};" for css, source in keys.items()) + "}" for name, palette in palettes.items() ) def generated_files() -> dict[str, str]: effects = effects_catalog() palettes = read_theme_palettes() packed = json.dumps({"version": 1, "effects": effects, "themes": list(palettes)}, ensure_ascii=False, separators=(",", ":")) digest = hashlib.sha256(packed.encode("utf-8")).hexdigest()[:12] replacements = { "{{THEME_OPTIONS}}": "".join(f'' for name in palettes), "{{THEME_CSS}}": theme_css(palettes), "{{CATALOG_HASH}}": digest, } files = {} for name in ("index.html", "styles.css", "app.js"): content = (SRC / name).read_text(encoding="utf-8") for marker, value in replacements.items(): content = content.replace(marker, value) files[name] = content files["catalog.js"] = "window.RAVENOUS_EFFECTS_CATALOG=" + packed + ";\n" files["ravenous-effects-v1.schema.json"] = (WEB / "schemas/ravenous-effects-v1.schema.json").read_text(encoding="utf-8") files["build-info.json"] = json.dumps({"catalog_hash": digest, "effects": len(effects), "groups": sorted({item["group"] for item in effects}), "themes": list(palettes)}, ensure_ascii=False, indent=2) + "\n" return files def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--output", type=Path, default=WEB / "build") parser.add_argument("--check", action="store_true") args = parser.parse_args() files = generated_files() if args.check: stale = [name for name, content in files.items() if not (args.output / name).is_file() or (args.output / name).read_text(encoding="utf-8") != content] if stale: raise SystemExit("Effects website files are stale: " + ", ".join(stale)) print(f"Effects website is current ({len(files)} files).") return args.output.mkdir(parents=True, exist_ok=True) for old in args.output.iterdir(): if old.is_file(): old.unlink() elif old.is_dir(): shutil.rmtree(old) for name, content in files.items(): (args.output / name).write_text(content, encoding="utf-8") print(f"Built effects website: {len(files)} files, {len(effects_catalog())} effects.") if __name__ == "__main__": main()