"""Rebuild the bundled PDF table-of-contents index (references/pdf/_toc.json). Self-contained: reads every PDF bundled under the skill's references/pdf/ and writes the multi-level TOC next to them. Run after adding/updating manuals. /mnt/c/Users/NAURA/.local/bin/uv.exe run --no-project --with pymupdf \ python .claude/skills/halcon/scripts/extract_toc.py """ import fitz, os, glob, json # references/pdf/ relative to this script (scripts/ -> ../references/pdf) HERE = os.path.dirname(os.path.abspath(__file__)) PDF_DIR = os.path.normpath(os.path.join(HERE, "..", "references", "pdf")) OUT = os.path.join(PDF_DIR, "_toc.json") out = {} for p in sorted(glob.glob(os.path.join(PDF_DIR, "*.pdf"))): name = os.path.basename(p) try: doc = fitz.open(p) out[name] = {"pages": doc.page_count, "toc": doc.get_toc()} # [level,title,page] doc.close() except Exception as e: out[name] = {"error": str(e)} with open(OUT, "w", encoding="utf-8") as f: json.dump(out, f, ensure_ascii=False, indent=1) for name, v in out.items(): if "error" in v: print(f"{name}: ERROR {v['error']}") else: print(f"{name}: {v['pages']} pages, {len(v['toc'])} toc entries") print("wrote", OUT)