#!/usr/bin/env python3 """Collects the header of every HANDOVER.md into one index.json that agents can download and search. Usage: python3 -I build_index.py [-o index.json] contains one folder per project, each with a HANDOVER.md. Stdlib only; understands the simple YAML subset used by the template (scalars, [lists], {maps}, # comments). """ import json import os import re import sys def parse_scalar(v): v = v.strip() if v in ("null", "~", ""): return None if v in ("true", "false"): return v == "true" if re.fullmatch(r"-?\d+", v): return int(v) if re.fullmatch(r"-?\d+\.\d+", v): return float(v) if len(v) >= 2 and v[0] == v[-1] and v[0] in "\"'": return v[1:-1] return v def split_top(s): parts, depth, cur, quote = [], 0, "", None for ch in s: if quote: cur += ch if ch == quote: quote = None continue if ch in "\"'": quote = ch elif ch in "[{": depth += 1 elif ch in "]}": depth -= 1 elif ch == "," and depth == 0: parts.append(cur) cur = "" continue cur += ch if cur.strip(): parts.append(cur) return parts def parse_value(v): v = v.strip() if v.startswith("[") and v.endswith("]"): return [parse_value(p) for p in split_top(v[1:-1])] if v.startswith("{") and v.endswith("}"): out = {} for p in split_top(v[1:-1]): k, _, val = p.partition(":") out[k.strip()] = parse_value(val) return out return parse_scalar(v) def strip_comment(line): quote = None for i, ch in enumerate(line): if quote: if ch == quote: quote = None elif ch in "\"'": quote = ch elif ch == "#" and (i == 0 or line[i - 1] in " \t"): return line[:i], line[i + 1:].strip() return line, "" def parse_handover(text): m = re.match(r"^---\s*\n(.*?)\n---\s*\n", text, re.S) if not m: return None, [] data, flags = {}, [] for raw in m.group(1).splitlines(): line, comment = strip_comment(raw) if not line.strip() or line.startswith((" ", "\t")): continue key, _, val = line.partition(":") key = key.strip() data[key] = parse_value(val) c = comment.lower() if "assumed" in c or "vermutet" in c: flags.append(f"{key}:assumed") if "open" in c or "offen" in c: flags.append(f"{key}:open") return data, flags def shorten(text, limit=600): text = re.sub(r"\s+", " ", text).strip() return text if len(text) <= limit else text[:limit].rsplit(" ", 1)[0] + " …" def section_titles(text): return re.findall(r"^## (.+)$", text, re.M) def main(): if len(sys.argv) < 2: print(__doc__) return 2 root = sys.argv[1] out = sys.argv[sys.argv.index("-o") + 1] if "-o" in sys.argv else "index.json" entries = [] for slug in sorted(os.listdir(root)): path = os.path.join(root, slug, "HANDOVER.md") if not os.path.isfile(path): continue text = open(path, encoding="utf-8").read() head, flags = parse_handover(text) if head is None: print(f"skip {slug}: no header") continue body = text.split("---", 2)[-1] first = re.search(r"## 1\.[^\n]*\n(.+?)(?=\n## )", body, re.S) entries.append({"id": slug, **head, "flags": flags, "abstract": shorten(first.group(1)) if first else None, "handover": f"{slug}/HANDOVER.md", "lines": text.count("\n")}) with open(out, "w", encoding="utf-8") as fh: json.dump({"format": "revivable-index/0.1", "count": len(entries), "projects": entries}, fh, indent=2, ensure_ascii=False) print(f"{len(entries)} projects -> {out}") return 0 if __name__ == "__main__": sys.exit(main())