"""D, the trial's no-model baseline (TRIAL, P1): a generated map and a code-built concept register. It calls no model. build --blocks FILE --kilde-root DIR --out DIR [--at ISO] One map page per source (k-.md) whose body mirrors the source's headings, with one sources[] entry per section; one register page per kept term (r-.md) whose body is a pure link list to the map sections where the term occurs, with one sources[] entry per occurrence. A term is kept when it is a docs-page heading that passes concept test 2, or when it occurs in at least 2 of the documents. Bodies hold only headings and pure link lists, so the claim count is 0 by construction; the exact locators live in sources[]. --kilde-root is the folder that contains kilde/ (the auditor's SOURCES argument). Exit 0 = written, 2 = could not run. """ from __future__ import annotations import argparse import json import re import sys from dataclasses import dataclass from datetime import datetime, timezone from pathlib import Path from typing import Any import yaml from p1.split import headings_of, is_meta, occurs, terms MIN_DOCUMENTS = 2 GENERATOR = "okf-wiki/p1-dmap" class RunError(Exception): """The command cannot run as asked; nothing was written.""" @dataclass class DStats: maps: int registers: int def slug(text: str) -> str: return re.sub(r"[^a-z0-9]+", "-", text.lower()).strip("-")[:60] or "x" def frontmatter(data: dict[str, Any]) -> str: """A frontmatter block; PyYAML quotes any string that would load as another type (no, 1.10).""" return "---\n" + yaml.safe_dump(data, sort_keys=False, allow_unicode=True, width=10_000) + "---\n" def _anchors(headings: list[tuple[int, int, str]]) -> dict[int, str]: seen: dict[str, int] = {} out: dict[int, str] = {} for line, _level, text in headings: base = slug(text) n = seen.get(base, 0) seen[base] = n + 1 out[line] = base if n == 0 else f"{base}-{n}" return out def _title(name: str, headings: list[tuple[int, int, str]]) -> str: first = next((t for _, level, t in headings if level == 1), None) if first: return first return f"Release notes {headings[0][2]}" if name.startswith("release-") and headings else name def build(blocks_doc: dict[str, Any], kilde_root: Path, out: Path, at: str | None = None) -> DStats: if out.exists(): raise RunError(f"{out} exists; refusing to overwrite") at = at or datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") names = [str(n) for n in blocks_doc["order"]] kilde = kilde_root / "kilde" texts = {n: (kilde / n).read_text(encoding="utf-8") for n in names} lines = {n: t.split("\n") for n, t in texts.items()} heads = {n: headings_of(lines[n]) for n in names} anchors = {n: _anchors(heads[n]) for n in names} titles = {n: _title(n, heads[n]) for n in names} pages: dict[str, str] = {} gen = {"by": GENERATOR, "at": at} for n in names: sources = [] body = [f"# {titles[n]}", ""] for i, (line, level, text) in enumerate(heads[n], 1): sources.append({"id": f"s{i}", "resource": f"/kilde/{n}", "title": text, "x-locator": {"line": line}, "x-quote": text}) if level > 1 or text != titles[n]: body += [f"{'#' * max(level, 2)} {text}", ""] fm = {"type": "Document map", "title": titles[n], "description": f"The sections of {n}.", "sources": sources, "generated": gen} pages[f"k-{slug(n.removesuffix('.md'))}.md"] = frontmatter(fm) + "\n" + "\n".join(body).rstrip() + "\n" candidates = [t for n in names if n.startswith("docs-") for _, level, t in heads[n] if level in (2, 3)] from_headings = {t for t in candidates if not is_meta(t)} pool = list(dict.fromkeys(list(from_headings) + [t for n in names for t in terms(texts[n])])) registers: dict[str, str] = {} for term in pool: if is_meta(term): continue holders = [n for n in names if occurs(term, texts[n])] if term not in from_headings and len(holders) < MIN_DOCUMENTS: continue pattern = re.compile(r"(? argparse.ArgumentParser: p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = p.add_subparsers(dest="command", required=True) b = sub.add_parser("build") b.add_argument("--blocks", required=True) b.add_argument("--kilde-root", required=True) b.add_argument("--out", required=True) b.add_argument("--at") return p def main(argv: list[str] | None = None) -> int: args = build_parser().parse_args(argv) try: doc = json.loads(Path(args.blocks).read_text(encoding="utf-8")) s = build(doc, Path(args.kilde_root), Path(args.out), args.at) except (RunError, OSError, json.JSONDecodeError) as exc: print(f"dmap cannot run: {exc}", file=sys.stderr) return 2 print(f"D: {s.maps} map pages, {s.registers} register pages -> {args.out}") return 0 if __name__ == "__main__": sys.exit(main())