"""Renderers for diff mode: Markdown, HTML, and JSON.""" from __future__ import annotations import json from html import escape from ..diff import ( _STATUS_LABEL, CATEGORY_ORDER, CHANGED, DRIFTED, FIXED, REGRESSED, UNCHANGED, DiffReport, RuleDelta, ) from .html import CSS as _CSS _CATEGORY_HEADING = { REGRESSED: "Regressions — a control is no longer supported", FIXED: "Fixed — a previously failing control now passes", DRIFTED: "Ongoing failures with changed evidence", CHANGED: "Other status changes", UNCHANGED: "Unchanged", } def _transition(delta: RuleDelta) -> str: return f"{_STATUS_LABEL.get(delta.old_status, delta.old_status)} → {_STATUS_LABEL.get(delta.new_status, delta.new_status)}" def _md_table(rows: list[dict[str, str]]) -> list[str]: if not rows: return [] shown = rows[:10] headers = list(shown[0].keys()) out = [ "| " + " | ".join(headers) + " |", "| " + " | ".join("---" for _ in headers) + " |", ] out += ["| " + " | ".join(str(r.get(h, "")) for h in headers) + " |" for r in shown] if len(rows) > len(shown): out.append(f"\n_+{len(rows) - len(shown)} more row(s) omitted._") return out def _render_delta_md(delta, category: str) -> list[str]: rule = delta.rule out = [ f"### {rule.title}", "", f"- **Rule:** `{rule.id}` · **Severity:** {rule.severity}", f"- **Controls:** {', '.join(rule.controls) or '—'}", f"- **Change:** {_transition(delta)}", ] if delta.new_reason: out.append(f"- **Now:** {delta.new_reason}") if category == REGRESSED and rule.remediation: out.append(f"- **Remediation:** {rule.remediation.strip()}") out.append("") if delta.evidence_added: out.append("**Newly failing rows:**") out.append("") out.extend(_md_table(delta.evidence_added)) out.append("") if delta.evidence_removed: out.append("**No longer failing rows:**") out.append("") out.extend(_md_table(delta.evidence_removed)) out.append("") return out def _render_md(diff: DiffReport) -> str: counts = diff.counts out: list[str] = [] out.append(f"# Evidence Drift — {diff.current.subject} ({diff.current.platform})") out.append("") out.append(f"- **Baseline:** `{diff.baseline.path.name}`") out.append(f"- **Current:** `{diff.current.path.name}`") out.append(f"- **Generated:** {diff.generated_at}") out.append( f"- **Drift:** {counts[REGRESSED]} regressed · {counts[FIXED]} fixed · " f"{counts[DRIFTED]} drifted · {counts[CHANGED]} changed · " f"{counts[UNCHANGED]} unchanged" ) out.append("") out.append( "> A *regression* means a setting moved into a state that no longer " "supports a control since the baseline. As always this is evidence, not " "a verdict." ) out.append("") for category in CATEGORY_ORDER: rows = diff.by_category(category) if not rows or category == UNCHANGED: continue out.append(f"## {_CATEGORY_HEADING[category]}") out.append("") for delta in rows: out.extend(_render_delta_md(delta, category)) unchanged = diff.counts[UNCHANGED] if unchanged: out.append(f"_{unchanged} rule(s) unchanged._") return "\n".join(out).rstrip() + "\n" def _html_table(rows: list[dict[str, str]], caption: str, cls: str) -> str: shown = rows[:10] headers = list(shown[0].keys()) head = "".join(f"
{escape(rule.id)} · "
f"{escape(rule.severity)}" f"{counts[REGRESSED]} regressed" f"{counts[FIXED]} fixed" f"{counts[DRIFTED]} drifted" f"{counts[CHANGED]} changed" f"{counts[UNCHANGED]} unchanged
" ) parts.append( "A regression means a setting moved into " "a state that no longer supports a control since the baseline. As always " "this is evidence, not a verdict.
" ) for category in CATEGORY_ORDER: rows = diff.by_category(category) if not rows or category == UNCHANGED: continue parts.append(f"