"""The report's HTML, rendered in code from the model's Markdown (F02 ADR 0015). Standard library only. Only the Markdown the prompt asks for is understood: headings, paragraphs, bullet and numbered lists, tables, blockquotes, **bold**, *italic* and `code`. Every character of the model's text is HTML-escaped before any markup is added, so nothing in the report (or in the estate data it quotes) can inject HTML or script into the page. """ from __future__ import annotations import html import re from typing import List STYLE = """ :root { --fg: #1f2328; --muted: #59636e; --line: #d1d9e0; --head: #f6f8fa; --bg: #ffffff; --warn: #9a6700; --warnbg: #fff8c5; } @media (prefers-color-scheme: dark) { :root { --fg: #e6edf3; --muted: #9198a1; --line: #3d444d; --head: #151b23; --bg: #0d1117; --warn: #d29922; --warnbg: #2e2a1f; } } body { background: var(--bg); color: var(--fg); font: 15px/1.55 -apple-system, "Segoe UI", Roboto, sans-serif; margin: 0; } main { max-width: 960px; margin: 0 auto; padding: 24px 16px 48px; } h1 { font-size: 1.7em; margin: 0 0 .4em; } h2 { font-size: 1.3em; margin: 1.6em 0 .5em; border-bottom: 1px solid var(--line); padding-bottom: .2em; } h3 { font-size: 1.1em; margin: 1.2em 0 .4em; } p, ul, ol, blockquote { margin: 0 0 .9em; } .table { overflow-x: auto; margin: 0 0 1em; } table { border-collapse: collapse; min-width: 100%; font-size: .93em; } th, td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; } th { background: var(--head); } code { background: var(--head); padding: 1px 4px; border-radius: 4px; font-size: .92em; } blockquote { border-left: 3px solid var(--line); padding: 0 12px; color: var(--muted); } .draft { background: var(--warnbg); color: var(--warn); border: 1px solid var(--warn); border-radius: 6px; padding: 8px 12px; margin-bottom: 18px; font-weight: 600; } """ def _inline(text: str) -> str: out = html.escape(text, quote=True) out = re.sub(r"`([^`]+)`", r"\1", out) out = re.sub(r"\*\*([^*]+)\*\*", r"\1", out) out = re.sub(r"(?\1", out) return out def _cells(line: str) -> List[str]: return [c.strip() for c in line.strip().strip("|").split("|")] def _is_rule(line: str) -> bool: return bool(re.fullmatch(r"\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)*\|?", line.strip())) def to_html(markdown: str) -> str: lines = (markdown or "").replace("\r\n", "\n").split("\n") out: List[str] = [] i = 0 while i < len(lines): line = lines[i] if not line.strip(): i += 1 continue heading = re.match(r"(#{1,4})\s+(.*)", line) if heading: level = len(heading.group(1)) out.append(f"{_inline(heading.group(2).strip())}") i += 1 elif line.lstrip().startswith("|") and i + 1 < len(lines) and _is_rule(lines[i + 1]): head = "".join(f"{_inline(c)}" for c in _cells(line)) i += 2 rows = [] while i < len(lines) and lines[i].lstrip().startswith("|"): rows.append("" + "".join(f"{_inline(c)}" for c in _cells(lines[i])) + "") i += 1 out.append(f'
{head}{"".join(rows)}
') elif re.match(r"\s*[-*]\s+", line) or re.match(r"\s*\d+[.)]\s+", line): ordered = bool(re.match(r"\s*\d+[.)]\s+", line)) pattern = r"\s*\d+[.)]\s+(.*)" if ordered else r"\s*[-*]\s+(.*)" items = [] while i < len(lines) and re.match(pattern, lines[i]): items.append(f"
  • {_inline(re.match(pattern, lines[i]).group(1))}
  • ") i += 1 tag = "ol" if ordered else "ul" out.append(f"<{tag}>{''.join(items)}") elif line.lstrip().startswith(">"): quote = [] while i < len(lines) and lines[i].lstrip().startswith(">"): quote.append(lines[i].lstrip()[1:].strip()) i += 1 out.append(f"

    {_inline(' '.join(quote))}

    ") else: para = [] while i < len(lines) and lines[i].strip() and not re.match(r"(#{1,4})\s|\s*[-*]\s|\s*\d+[.)]\s|\s*[|>]", lines[i]): para.append(lines[i].strip()) i += 1 if not para: # a line no rule above took: keep it as text rather than loop on it para, i = [line.strip()], i + 1 out.append(f"

    {_inline(' '.join(para))}

    ") return "\n".join(out) def page(markdown: str, title: str, banner: str = "") -> str: note = f'
    {html.escape(banner)}
    ' if banner else "" return ("" "" f"{html.escape(title)}" f"
    {note}{to_html(markdown)}
    ")