Files
rengine/reports/render/css.py
T
Yogesh OjhaandClaude Opus 5 2cb8d73280 style: strip explanatory comments across the codebase
The comment rule in CLAUDE.md is one line maximum, never narration: no
multi-line blocks, no module-header explainers, no sentences that explain or
justify the code to the reader. The tree had drifted a long way from it, and
most of the drift is mine.

Removed:
  232  lines of multi-line comment blocks
  203  single-line comments that argued instead of stating
  154  comments joined by so/because/otherwise/rather-than
  143  narrating comments in the frontend
  112  test docstrings (the test name already says what it asserts)
   25  test module docstrings and 25 decorative section dividers

Collapsed:
  324  multi-line docstrings to one factual line
  271  one-line docstrings that had a justifying clause bolted on

Kept: functional directives, section dividers, short field labels, alembic
revision headers, and the eight FastAPI route docstrings that render in /docs.
Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs.

284 python tests and 34 frontend tests pass, ruff and ruff-format clean across
ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
2026-09-11 14:11:23 +05:30

132 lines
4.1 KiB
Python

"""Assemble one stylesheet: faces, theme tokens, page setup, then the theme's own CSS."""
from __future__ import annotations
import re
from functools import lru_cache
from pathlib import Path
from reports.theme import css_variables
from shared.definitions.report_theme import ThemeTokens
from shared.definitions.reports import (
PAGE_SIZE_CSS,
SLOT_TOKEN_VALUES,
Orientation,
ReportStyle,
)
ASSETS = Path(__file__).resolve().parent.parent / "assets"
TEMPLATES = Path(__file__).resolve().parent.parent / "templates"
_TOKEN_RE = re.compile("(" + "|".join(re.escape(t) for t in SLOT_TOKEN_VALUES) + ")")
RUNNING_CLEAR = 8.0
_COUNTER_SLOTS = {
"{page}": "counter(page)",
"{pages}": "counter(pages)",
"{section}": "string(section)",
}
@lru_cache(maxsize=1)
def base_css() -> str:
return (TEMPLATES / "base.css").read_text(encoding="utf-8")
def _escape(value: str) -> str:
return value.replace("\\", "\\\\").replace('"', '\\"')
def slot_content(template: str, values: dict[str, str]) -> str:
"""A header or footer slot as a CSS content value."""
if not template.strip():
return "none"
parts: list[str] = []
for piece in _TOKEN_RE.split(template):
if not piece:
continue
if piece in _COUNTER_SLOTS:
parts.append(_COUNTER_SLOTS[piece])
elif piece.startswith("{") and piece.endswith("}"):
literal = values.get(piece[1:-1], "")
if literal:
parts.append(f'"{_escape(literal)}"')
else:
parts.append(f'"{_escape(piece)}"')
return " ".join(parts) if parts else "none"
def page_css(style: ReportStyle, values: dict[str, str]) -> str:
size = PAGE_SIZE_CSS.get(style.page_size, "A4")
orientation = (
"landscape" if style.orientation == Orientation.LANDSCAPE.value else "portrait"
)
margins = (
f"{style.margin_top}mm {style.margin_right}mm "
f"{style.margin_bottom}mm {style.margin_left}mm"
)
boxes: list[str] = []
slots = (
("top-left", style.header_left if style.show_header else ""),
("top-center", style.header_center if style.show_header else ""),
("top-right", style.header_right if style.show_header else ""),
("bottom-left", style.footer_left if style.show_footer else ""),
("bottom-center", style.footer_center if style.show_footer else ""),
("bottom-right", style.footer_right if style.show_footer else ""),
)
for box, template in slots:
content = slot_content(template, values)
if content == "none":
continue
at_top = box.startswith("top")
boxes.append(
f"@{box}{{content:{content};font-family:var(--r-font-body);"
f"font-size:var(--r-micro);letter-spacing:var(--r-run-track);"
f"color:var(--r-ink-faint);white-space:nowrap;"
f"padding-{'bottom' if at_top else 'top'}:{RUNNING_CLEAR}mm;"
f"vertical-align:{'bottom' if at_top else 'top'}}}"
)
blank = "".join(
f"@{box}{{content:none}}"
for box in (
"top-left",
"top-center",
"top-right",
"bottom-left",
"bottom-center",
"bottom-right",
)
)
return (
f"@page{{size:{size} {orientation};margin:{margins};background:var(--r-page);{''.join(boxes)}}}"
f"@page cover{{margin:0;background:var(--r-page);{blank}}}"
f"@page appendix{{{''.join(boxes)}}}"
"h1{bookmark-level:1;bookmark-label:content(text)}"
".section__title{string-set:section attr(data-run);bookmark-level:1;bookmark-label:attr(data-run)}"
".sub__title{bookmark-level:2;bookmark-label:content(text)}"
".finding__title{bookmark-level:3;bookmark-label:content(text)}"
)
def stylesheet(
tokens: ThemeTokens,
style: ReportStyle,
values: dict[str, str],
*,
faces: str = "",
families: dict[str, str] | None = None,
) -> str:
return "\n".join(
(
faces,
css_variables(tokens, style, families=families),
base_css(),
page_css(style, values),
tokens.css or "",
)
)