"""The side-by-side page (TRIAL, P1): the same concepts in every build, every source link checked. It calls no model.
render --begreper FILE --col LABEL=DIR ... [--col LABEL=-] --kilde-root DIR --out FILE
One self-contained HTML page: one row per concept in begreper.txt (name, a tab, aliases joined by ;), one
column per build. A cell shows the pages whose normalised title or alias equals the concept's (for the L4
column: whose normalised title or description contains it), each with its body and its sources
(file › line or span). An empty cell says why: "ikke bygget" (a build given as -, stopped), "ikke i
registeret" (D has no register page for it), "ingen side" (built, no page).
check --html FILE --kilde-root DIR
A source link resolves when its file exists under kilde/ and its line or span lies inside the file.
Prints "resolved X of N"; exit 1 when X < N.
Exit 0 = done, 1 = an unresolved link (check), 2 = could not run.
"""
from __future__ import annotations
import argparse
import os
import re
import sys
from pathlib import Path
from typing import Any
from okfwiki.audit import load_bundle
from okfwiki.view import esc
from p1.kpack import norm
from p1.measure import cited
Concept = tuple[str, tuple[str, ...]]
SRC = re.compile(r'')
STYLE = """
:root { --bg: #fdfdfb; --fg: #1d1d1b; --muted: #6b6b66; --line: #ddd9cf; --cell: #ffffff; }
@media (prefers-color-scheme: dark) { :root { --bg: #1b1b1a; --fg: #ecebe6; --muted: #a3a29b; --line: #3a3934;
--cell: #242422; } }
body { background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, sans-serif; margin: 16px; }
table { border-collapse: collapse; width: 100%; table-layout: fixed; }
th, td { border: 1px solid var(--line); vertical-align: top; padding: 8px; background: var(--cell); }
th.concept-name { width: 10em; text-align: left; }
.empty { color: var(--muted); font-style: italic; }
.page h3 { margin: 0 0 4px; font-size: 15px; }
.sources { color: var(--muted); font-size: 13px; }
.wrap { overflow-x: auto; }
"""
class RunError(Exception):
"""The command cannot run as asked; nothing was written."""
def read_begreper(path: Path) -> list[Concept]:
out: list[Concept] = []
for line in path.read_text(encoding="utf-8").splitlines():
if line.strip():
name, _, aliases = line.partition("\t")
out.append((name.strip(), tuple(a.strip() for a in aliases.split(";") if a.strip()) or (name.strip(),)))
return out
def _l4_norm(text: str) -> str:
return f" {norm(text.replace('-', ' '))} "
def find(bundle: Path, concept: Concept, l4: bool = False) -> list[str]:
wanted = {norm(n) for n in (concept[0], *concept[1])} - {""}
found: list[str] = []
for d in load_bundle(bundle, bundle).concepts:
fm = d.frontmatter or {}
if l4:
hay = _l4_norm(str(fm.get("title", ""))) + _l4_norm(str(fm.get("description", "")))
if any(f" {norm(w.replace('-', ' '))} " in hay for w in wanted):
found.append(d.rel)
continue
aliases = fm.get("x-aliases")
names = {norm(str(fm.get("title", "")))} | ({norm(str(a)) for a in aliases} if isinstance(aliases, list)
else set())
if names & wanted:
found.append(d.rel)
return found
def _sources_html(entries: list[dict[str, Any]], kilde: Path, out_dir: Path) -> str:
items = []
for s in entries:
file = Path(str(s.get("resource", ""))).name
loc = s.get("x-locator")
line = loc.get("line") if isinstance(loc, dict) else None
span = s.get("x-span")
a, z = (span[0], span[1]) if isinstance(span, list) and len(span) == 2 else (line, line)
if not isinstance(a, int) or not isinstance(z, int):
continue
href = os.path.relpath(kilde / file, out_dir).replace(os.sep, "/") + f"#L{a}"
where = f"{a}" if a == z else f"{a}–{z}"
items.append(f'' + "".join(items) + "
"
def _cell(bundle: Path | None, label: str, concept: Concept, kilde_root: Path, out_dir: Path) -> str:
if bundle is None:
return '
{esc(p)}
" for p in paras if not p.startswith("[^")) parts.append(f'| Begrep | {head}
|---|