#!/usr/bin/env python3 """Build the self-contained HTML viewer for rootfs-triage-wiki.""" from __future__ import annotations import json import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "viewer.html" SOUND_INDEX_OUT = ROOT / "sound-index.json" def read_text(path: Path) -> str: return path.read_text(encoding="utf-8", errors="replace") def title_for(path: Path, text: str) -> str: match = re.search(r"^#\s+(.+?)\s*$", text, re.MULTILINE) if match: return match.group(1) return path.stem.replace("-", " ").replace("_", " ").title() def content_group_for(path: Path) -> str: """Separate narrative research from generated catalogs and galleries.""" name = path.name if path.suffix != ".md": return "source" if name.startswith("sound-best-named-gallery-") or name in { "sound-asr-catalog-labels.md", "sound-asr-draft-labels.md", "sound-asr-review-candidates.md", "sound-pokedex-voice.md", }: return "voice" if re.match(r"^(asset-.+-gallery|radium-section3-bitmap-gallery|sound-gallery)-", name): return "media" return "wiki" def page_order() -> list[str]: readme = read_text(ROOT / "README.md") ordered = ["README.md"] for target in re.findall(r"\[[^\]]+\]\(([^)#?]+)", readme): target_path = ROOT / target if target_path.suffix not in {".md", ".c"}: continue if target not in ordered and target_path.exists(): ordered.append(target) for path in sorted(ROOT.glob("*.md")): name = path.name if name not in ordered: ordered.append(name) c_export = "conagent-pseudocode.c" if (ROOT / c_export).exists() and c_export not in ordered: ordered.append(c_export) return ordered def build_pages() -> list[dict[str, str]]: pages = [] for name in page_order(): path = ROOT / name text = read_text(path) kind = "markdown" if path.suffix == ".md" else "source" pages.append( { "path": name, "title": title_for(path, text), "kind": kind, "group": content_group_for(path), "text": text, } ) return pages def parse_directive_attrs(raw: str) -> dict[str, str]: return dict(re.findall(r'([A-Za-z0-9_-]+)="([^"]*)"', raw)) def build_sound_index() -> dict[str, object]: """Build one canonical entry for every best-named main/menu sound.""" sounds: list[dict[str, str]] = [] source_pages: list[str] = [] seen_sources: set[str] = set() for path in sorted(ROOT.glob("sound-best-named-gallery-*.md")): source_pages.append(path.name) for match in re.finditer(r"^::audio\{(.*)\}\s*$", read_text(path), re.MULTILINE): attrs = parse_directive_attrs(match.group(1)) src = attrs.get("src", "") if not src or src in seen_sources: continue seen_sources.add(src) sounds.append( { "id": f"sound-{len(sounds) + 1:04d}", "src": src, "filename": Path(src).name, "title": attrs.get("title", Path(src).name), "meta": attrs.get("meta", ""), "source_page": path.name, } ) return { "schema_version": 1, "generated_from": source_pages, "count": len(sounds), "sounds": sounds, } HTML_TEMPLATE = r"""