Files
computer-playbook/cli/build/docs/readme/sections.py
kevinveenbirkenbachandClaude Opus 5 3d6b3900e0 refactor(cli): gather the documentation generators under cli/build/docs
Two of them lived inside docs/ next to the pages they render, which is why both had grown a __file__ upward walk to find the repository root, a sys.path insert to import from utils, and a nocheck for each. A generator parked among documents reads as documentation and drops out of the checks that apply to code.

Moving them surfaced six real violations that the old location had hidden: yaml.safe_load instead of the shared cache, two uncached read_text calls, five hard-coded role paths instead of ROLE_FILE_META_SERVICES and ROLE_FILE_META_MCP, two make targets without an alias, and a requirement reference in code. All six are fixed at the root, none with a suppression, and both generators lose their project-root exemptions because a package supplies PROJECT_ROOT.

cli/build/readme joins them so the three generators that write into docs/ sit together. A new lint keeps docs/ to .md, .html, .rst and .nocheck; it enumerates through git ls-files so untracked __pycache__ is not its business. Checked against HEAD, it names exactly the three .py files that moved.

026-unified-addon-syntax stated that the matrix generator stays under docs/requirements/. The lint makes that impossible, so the requirement now records where the generator actually lives.

Verified: every one of the twelve modules under the new tree imports, no old module path survives anywhere in the repository, 482 lint tests and 1246 cli unit tests pass, and both generators run without drift.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 23:17:55 +02:00

60 lines
1.8 KiB
Python

"""Split a Markdown README into its H2 sections (fence-aware)."""
from __future__ import annotations
import re
from dataclasses import dataclass
_H2_RE = re.compile(r"^##\s+(.*?)\s*$")
_FENCE_RE = re.compile(r"^\s*`{3,}")
@dataclass
class Readme:
"""A parsed README: everything before the first H2, then its H2 blocks."""
preamble: str
sections: list[tuple[str, str]]
def titles(self) -> list[str]:
return [title for title, _ in self.sections]
def render(self) -> str:
parts = [self.preamble.rstrip("\n")] if self.preamble.strip() else []
parts.extend(block.rstrip("\n") for _, block in self.sections)
return "\n\n".join(parts) + "\n"
def _h2_starts(lines: list[str]) -> list[int]:
"""Line indexes of every H2 heading outside a fenced code block."""
starts: list[int] = []
in_fence = False
for i, line in enumerate(lines):
if _FENCE_RE.match(line):
in_fence = not in_fence
continue
if not in_fence and _H2_RE.match(line):
starts.append(i)
return starts
def parse_readme(text: str) -> Readme:
lines = text.splitlines()
starts = _h2_starts(lines)
if not starts:
return Readme(preamble=text, sections=[])
preamble = "\n".join(lines[: starts[0]])
sections: list[tuple[str, str]] = []
for idx, start in enumerate(starts):
end = starts[idx + 1] if idx + 1 < len(starts) else len(lines)
title = _H2_RE.match(lines[start]).group(1)
block = "\n".join(lines[start:end]).rstrip("\n")
sections.append((title, block))
return Readme(preamble=preamble, sections=sections)
def h2_titles(text: str) -> list[str]:
"""Return the ordered H2 titles of a Markdown document (fence-aware)."""
return parse_readme(text).titles()