First public release, published as one snapshot. What changed between versions is in CHANGELOG.md. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
185 lines
8.4 KiB
Python
185 lines
8.4 KiB
Python
"""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'<a class="src" href="[^"]*" data-file="([^"]+)" data-start="(\d+)" data-end="(\d+)">')
|
||
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'<li><a class="src" href="{esc(href)}" data-file="{esc(file)}" data-start="{a}" '
|
||
f'data-end="{z}">{esc(file)} › {where}</a></li>')
|
||
return '<ul class="sources">' + "".join(items) + "</ul>"
|
||
|
||
|
||
def _cell(bundle: Path | None, label: str, concept: Concept, kilde_root: Path, out_dir: Path) -> str:
|
||
if bundle is None:
|
||
return '<td class="cell empty">ikke bygget</td>'
|
||
rels = find(bundle, concept, l4=label == "L4")
|
||
if not rels:
|
||
return f'<td class="cell empty">{"ikke i registeret" if label == "D" else "ingen side"}</td>'
|
||
docs = {d.rel: d for d in load_bundle(bundle, kilde_root).concepts}
|
||
parts = []
|
||
for rel in rels:
|
||
d = docs[rel]
|
||
fm = d.frontmatter or {}
|
||
srcs = fm.get("sources")
|
||
entries = cited(d) or ([s for s in srcs if isinstance(s, dict)] if isinstance(srcs, list) else [])
|
||
paras = [p.strip() for p in re.split(r"\n\s*\n", d.body) if p.strip() and not p.strip().startswith("# ")]
|
||
body = "".join(f"<p>{esc(p)}</p>" for p in paras if not p.startswith("[^"))
|
||
parts.append(f'<div class="page"><h3>{esc(str(fm.get("title", rel)))} <small>({esc(rel)})</small></h3>'
|
||
f"{body}{_sources_html(entries, kilde_root / 'kilde', out_dir)}</div>")
|
||
return '<td class="cell">' + "".join(parts) + "</td>"
|
||
|
||
|
||
def render(begreper: list[Concept], columns: list[tuple[str, Path | None]], kilde_root: Path, out: Path) -> None:
|
||
out_dir = out.parent
|
||
head = "".join(f"<th>{esc(label)}</th>" for label, _ in columns)
|
||
rows = []
|
||
for concept in begreper:
|
||
cells = "".join(_cell(b, label, concept, kilde_root, out_dir) for label, b in columns)
|
||
rows.append(f'<tr class="concept"><th class="concept-name">{esc(concept[0])}</th>{cells}</tr>')
|
||
html = (f'<!doctype html><html lang="no"><head><meta charset="utf-8">'
|
||
f'<meta name="viewport" content="width=device-width, initial-scale=1"><title>P1 side om side</title>'
|
||
f"<style>{STYLE}</style></head><body><h1>P1: tre begreper, side om side</h1>"
|
||
f'<div class="wrap"><table><tr><th class="concept-name">Begrep</th>{head}</tr>{"".join(rows)}'
|
||
f"</table></div></body></html>\n")
|
||
out.parent.mkdir(parents=True, exist_ok=True)
|
||
out.write_text(html, encoding="utf-8")
|
||
|
||
|
||
def check(html_path: Path, kilde_root: Path) -> tuple[int, int]:
|
||
html = html_path.read_text(encoding="utf-8")
|
||
resolved = total = 0
|
||
sizes: dict[str, int | None] = {}
|
||
for file, a, z in SRC.findall(html):
|
||
total += 1
|
||
if file not in sizes:
|
||
p = kilde_root / "kilde" / file
|
||
sizes[file] = len(p.read_text(encoding="utf-8").splitlines()) if p.is_file() else None
|
||
n = sizes[file]
|
||
resolved += n is not None and 1 <= int(a) <= int(z) <= n
|
||
return resolved, total
|
||
|
||
|
||
def build_parser() -> argparse.ArgumentParser:
|
||
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||
sub = p.add_subparsers(dest="command", required=True)
|
||
r = sub.add_parser("render")
|
||
r.add_argument("--begreper", required=True)
|
||
r.add_argument("--col", action="append", required=True, help="LABEL=DIR, or LABEL=- for a stopped build")
|
||
r.add_argument("--kilde-root", required=True)
|
||
r.add_argument("--out", required=True)
|
||
c = sub.add_parser("check")
|
||
c.add_argument("--html", required=True)
|
||
c.add_argument("--kilde-root", required=True)
|
||
return p
|
||
|
||
|
||
def main(argv: list[str] | None = None) -> int:
|
||
args = build_parser().parse_args(argv)
|
||
try:
|
||
if args.command == "render":
|
||
columns: list[tuple[str, Path | None]] = []
|
||
for col in args.col:
|
||
label, _, path = col.partition("=")
|
||
if not label or not path:
|
||
raise RunError(f"--col {col!r} is not LABEL=DIR")
|
||
columns.append((label, None if path == "-" else Path(path)))
|
||
render(read_begreper(Path(args.begreper)), columns, Path(args.kilde_root), Path(args.out))
|
||
print(f"side by side: {len(columns)} columns -> {args.out}")
|
||
return 0
|
||
resolved, total = check(Path(args.html), Path(args.kilde_root))
|
||
print(f"resolved {resolved} of {total}")
|
||
return 0 if total and resolved == total else 1
|
||
except (RunError, OSError) as exc:
|
||
print(f"sidebyside cannot run: {exc}", file=sys.stderr)
|
||
return 2
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|