#!/usr/bin/env python3 """Generate CATALOG.md: every skill (name, description), every rule (title, impact), every steering file and workflow, from the files themselves. Re-run after any change; tools/check.py does not, so a stale catalogue is the one thing CI must catch with `tools/catalog.py --check`. Stdlib only. """ import os, re, sys REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = os.path.join(REPO, "CATALOG.md") ORDER = {"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3} def fm(path): t = open(path, encoding="utf-8").read() head = t[4:].split("\n---\n", 1)[0] if t.startswith("---\n") else "" out, key = {}, None for line in head.splitlines(): m = re.match(r"^([a-zA-Z][\w-]*):\s*(.*)$", line) if m: key, v = m.group(1), m.group(2).strip() out[key] = "" if v in (">", "|", ">-", "|-") else v.strip("\"'") elif key and line.startswith(" "): out[key] = (out[key] + " " + line.strip()).strip() return out def build(): L = ["# Catalogue", "", "Generated by `tools/catalog.py` from the files; do not edit by hand.", "", "## Personas (`solnix//SKILL.md`)", "", "| skill | when |", "|---|---|"] for d in sorted(os.listdir(os.path.join(REPO, "solnix"))): p = os.path.join(REPO, "solnix", d, "SKILL.md") if os.path.exists(p): m = fm(p) L.append(f"| [`{m.get('name')}`](solnix/{d}/SKILL.md) | {m.get('description', '').split('. Use')[0]}. |") L += ["", "## Rules (`solnix/rules/`)", "", "| rule | area | impact |", "|---|---|---|"] rules = [] rd = os.path.join(REPO, "solnix", "rules") for f in sorted(os.listdir(rd)): if f.endswith(".md") and not f.startswith("_") and f != "SKILL.md": m = fm(os.path.join(rd, f)) rules.append((m.get("area", ""), ORDER.get(m.get("impact"), 9), f, m)) for area, _, f, m in sorted(rules): L.append(f"| [{m.get('title', f)}](solnix/rules/{f}) | {area} | {m.get('impact', '')} |") for title, d in (("Steering (`steering/`, always on)", "steering"), ("Workflows (`workflows/`)", "workflows")): L += ["", f"## {title}", ""] full = os.path.join(REPO, d) for f in sorted(os.listdir(full)) if os.path.isdir(full) else []: if f.endswith(".md") and f != "README.md": m = fm(os.path.join(full, f)) L.append(f"- [`{m.get('name', f[:-3])}`]({d}/{f}): {m.get('description', '').split('. Use')[0]}.") return "\n".join(L) + "\n" def main(): c = build() if "--check" in sys.argv: cur = open(OUT, encoding="utf-8").read() if os.path.exists(OUT) else "" if cur != c: print("CATALOG.md is stale: run tools/catalog.py") sys.exit(1) print("CATALOG.md is current") return open(OUT, "w", encoding="utf-8").write(c) print("wrote CATALOG.md") if __name__ == "__main__": main()