"""Code review & understanding templates. - review.pr_review: PR header + per-file findings with severity tags. - review.code_walkthrough: Annotated explanation of a module or function. - review.module_map: Box-and-arrow diagram of package structure. """ from __future__ import annotations import sys import composer as c from . import register # --------------------------------------------------------------------------- # # pr_review # # --------------------------------------------------------------------------- # @register( "review.pr_review", summary="PR review writeup: header, summary, per-file findings with severity.", spec_keys={ "title": "Page title (e.g. 'PR #247 — Review').", "subtitle": "Optional sub-headline.", "pr": "Dict {repo, number, branch, author, additions, deletions}.", "verdict": "Optional one-liner verdict ('approve', 'request changes', etc).", "summary_html": "Optional rich HTML overview (1–2 paragraphs).", "findings": "List[{file, line?, severity: 'info|nit|warn|block', title, body_html, snippet?, snippet_lang?}].", }, ) def pr_review(spec: dict) -> dict: pr = spec.get("pr") or {} pr_meta = "".join([ c.badge(f"+{pr['additions']}", "ok") if pr.get("additions") is not None else "", c.badge(f"−{pr['deletions']}", "err") if pr.get("deletions") is not None else "", c.badge(pr.get("branch"), "info") if pr.get("branch") else "", c.badge(pr.get("author")) if pr.get("author") else "", ]) pr_head = ( f'
' f'{c.esc(pr.get("repo"))}{(" · #"+str(pr.get("number"))) if pr.get("number") else ""}
' + (f'
{pr_meta}
' if pr_meta else "") ) verdict = spec.get("verdict") verdict_html = c.callout(verdict, kind="info") if verdict else "" summary_html = spec.get("summary_html") or "" sev = { "info": ("info", "i"), "nit": ("info", "·"), "warn": ("warn", "!"), "block": ("err", "✕"), } finding_cards = [] for f in spec.get("findings", []): kind, mark = sev.get(f.get("severity", "info"), ("info", "i")) snippet = "" if f.get("snippet"): snippet = c.code_block(f["snippet"], lang=f.get("snippet_lang")) loc = f.get("file", "") if f.get("line") is not None: loc = f"{loc}:{f['line']}" body = ( f'
' f'
{c.esc(loc)}
' f'

{c.esc(f.get("title"))}

' f'{c.badge(f.get("severity","info").upper(), kind)}
' + f'
{f.get("body_html","")}
' + (f'
{snippet}
' if snippet else "") ) finding_cards.append(c.card(body)) body = ( c.section(body=c.card(pr_head + (f'
{verdict_html}
' if verdict_html else ""))) + (c.section("Summary", body=summary_html) if summary_html else "") + c.section("Findings", body=c.stack(finding_cards) if finding_cards else "

No findings.

") ) return { "title": spec.get("title", "Review"), "subtitle": spec.get("subtitle"), "eyebrow_text": spec.get("eyebrow", "CODE REVIEW"), "body": body, } # --------------------------------------------------------------------------- # # code_walkthrough # # --------------------------------------------------------------------------- # @register( "review.code_walkthrough", summary="Annotated walkthrough of code: numbered steps with snippets and prose.", spec_keys={ "title": "Page title.", "subtitle": "Optional sub-headline.", "intro_html": "Optional introduction HTML.", "steps": "List[{heading, body_html, code?, lang?}].", }, ) def code_walkthrough(spec: dict) -> dict: rows = [] for i, s in enumerate(spec.get("steps", []), start=1): snippet = c.code_block(s["code"], lang=s.get("lang")) if s.get("code") else "" rows.append( f'
' f'
{i:02d}
' f'

{c.esc(s.get("heading"))}

' f'
{s.get("body_html","")}
' f'{snippet}
' ) body = ((f'
{spec.get("intro_html")}
' if spec.get("intro_html") else "") + c.section(body="".join(rows))) return { "title": spec.get("title", "Walkthrough"), "subtitle": spec.get("subtitle"), "eyebrow_text": spec.get("eyebrow", "WALKTHROUGH"), "body": body, "page_class": "page page--narrow", } # --------------------------------------------------------------------------- # # module_map # # --------------------------------------------------------------------------- # @register( "review.module_map", summary="Module map: nodes (modules) and edges (dependencies) as SVG boxes/arrows.", spec_keys={ "title": "Page title.", "subtitle": "Optional sub-headline.", "intro_html": "Optional intro HTML.", "nodes": "List[{id, label, description?, x?: int, y?: int, kind?: 'core|util|external'}]. " "If x/y are missing, nodes are auto-laid in a grid.", "edges": "List[{from: id, to: id, label?}].", "legend": "Optional list[{label, kind}] to render a legend.", }, ) def module_map(spec: dict) -> dict: nodes = spec.get("nodes") or [] edges = spec.get("edges") or [] nw, nh = 180, 70 cols, cw, ch = 3, 220, 110 # Compute coordinates locally — never mutate the caller's spec. # Each node id falls back to its index if missing; nameless nodes are # still drawable and addressable from edges by `n0`, `n1`, ... pos: dict[str, tuple[float, float]] = {} for i, n in enumerate(nodes): nid = n.get("id") or f"n{i}" x = n["x"] if n.get("x") is not None else (i % cols) * cw + 30 y = n["y"] if n.get("y") is not None else (i // cols) * ch + 30 pos[nid] = (x, y) width = max((x + nw + 30 for (x, _y) in pos.values()), default=600) height = max((y + nh + 30 for (_x, y) in pos.values()), default=400) color = {"core": "#FCEEDE", "util": "#EAE8DF", "external": "#E0E7DA"} border = {"core": "var(--clay)", "util": "var(--g300)", "external": "var(--olive)"} edge_svg = [] for e in edges: src_id, dst_id = e.get("from"), e.get("to") if src_id not in pos or dst_id not in pos: print(f"composing-html warning: module_map edge references unknown id " f"({src_id!r} -> {dst_id!r})", file=sys.stderr) continue ax, ay = pos[src_id]; bx, by = pos[dst_id] x1, y1 = ax + nw / 2, ay + nh / 2 x2, y2 = bx + nw / 2, by + nh / 2 edge_svg.append( f'' ) if e.get("label"): mx, my = (x1 + x2) / 2, (y1 + y2) / 2 edge_svg.append( f'{c.esc(e["label"])}' ) node_svg = [] for i, n in enumerate(nodes): nid = n.get("id") or f"n{i}" x, y = pos[nid] kind = n.get("kind", "util") node_svg.append( f'' f'' f'{c.esc(n.get("label"))}' + (f'{c.esc(n.get("description"))}' if n.get("description") else "") + '' ) svg = ( f'' '' '' + "".join(edge_svg) + "".join(node_svg) + '' ) legend_html = "" if spec.get("legend"): legend_html = '
' + "".join( f'{c.esc(item.get("label"))}' for item in spec["legend"]) + '
' body = ((f'
{spec.get("intro_html")}
' if spec.get("intro_html") else "") + c.section(body=c.card(svg + legend_html, soft=False))) return { "title": spec.get("title", "Module map"), "subtitle": spec.get("subtitle"), "eyebrow_text": spec.get("eyebrow", "ARCHITECTURE"), "body": body, "page_class": "page page--wide", }