#!/usr/bin/env python3 """ page_index.py — write a table-of-contents page into a space. Offline. The wiki this feeds is flat: a title like `Simple Chains/Ideas/Chain core` has hierarchy in its name and nowhere else, and Gitea will not draw you a tree from it. An index page is therefore not a nicety, it is the navigation. Written as an ordinary page in the space, so it is pushed by the same command as everything else and needs no special case anywhere downstream. Links are written by TITLE rather than by URL — the wiki resolves those itself, and a link written that way survives every filename-escaping rule this layer deliberately refuses to model. page_index.py --space claude-skills/tea --prefix "Simple Chains" -> Simple-Chains.md, title `Simple Chains` page_index.py --space claude-skills/tea --title Home -> Home.md, title `Home`, listing the whole space Usage: page_index.py [--space SPACE] [--prefix TITLE] [--title TITLE] [--dry-run] [--out DIR] """ import argparse import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import page # noqa: E402 def die(msg, code=1): sys.stderr.write("%s: %s\n" % (os.path.basename(sys.argv[0]), msg)) sys.exit(code) def main(): ap = argparse.ArgumentParser() ap.add_argument("--space", default="local") ap.add_argument("--prefix", default="", help="index only this subtree; also the index's own title") ap.add_argument("--title", help="title for the index page " "(default: --prefix, else Home)") ap.add_argument("--dry-run", action="store_true") ap.add_argument("--out", help="wiki cache root (default: /tmp/wiki)") a = ap.parse_args() root = a.out or page.WIKI_ROOT space_dir = page.space_root(a.space, root) if not os.path.isdir(space_dir): die("no such space: %s (looked in %s)" % (a.space, space_dir)) manifest = page.load_manifest(a.space, root) prefix = page.sanitize_title(a.prefix) if a.prefix else "" title = a.title or prefix or "Home" body = page.render_index(manifest, prefix, heading=title) relpath = page.path_for_title(title) if a.dry_run: sys.stdout.write(body) print("-> %s (%s)" % (relpath, title)) return 0 dest = os.path.join(space_dir, relpath) os.makedirs(os.path.dirname(dest), exist_ok=True) with open(dest, "w", encoding="utf-8") as f: f.write(body) # Carries the entry's passthrough keys forward: rebuilding an index must # update the page that is already published, never publish a second one. prior = manifest["pages"].get(relpath, {}) e = dict(prior) e.update(page.entry(title, prior.get("order"))) manifest["pages"][relpath] = e page.save_manifest(manifest, root) n = len(page.children_of(manifest, prefix) if prefix else page.sorted_pages(manifest)) print("%s -> %s (%d entr%s)" % (title, relpath, n - 1, "y" if n - 1 == 1 else "ies")) return 0 if __name__ == "__main__": sys.exit(main())