diff --git a/website/readme_parser.py b/website/readme_parser.py index 1c067d6c..cbbc30a2 100644 --- a/website/readme_parser.py +++ b/website/readme_parser.py @@ -62,46 +62,44 @@ def slugify(name: str) -> str: # --- Inline renderers ------------------------------------------------------- -def render_inline_html(children: list[SyntaxTreeNode]) -> str: - """Render inline AST nodes to HTML with proper escaping.""" +def _render_inline(children: list[SyntaxTreeNode], *, html: bool) -> str: + """Render inline AST nodes to HTML or plain text.""" parts: list[str] = [] for child in children: match child.type: case "text": - parts.append(str(escape(child.content))) + parts.append(str(escape(child.content)) if html else child.content) + case "html_inline": + if html: + parts.append(str(escape(child.content))) case "softbreak": parts.append(" ") - case "link": - href = str(escape(_href(child))) - inner = render_inline_html(child.children) - parts.append( - f'{inner}' - ) - case "em": - parts.append(f"{render_inline_html(child.children)}") - case "strong": - parts.append(f"{render_inline_html(child.children)}") case "code_inline": - parts.append(f"{escape(child.content)}") - case "html_inline": - parts.append(str(escape(child.content))) + parts.append(f"{escape(child.content)}" if html else child.content) + case "link": + inner = _render_inline(child.children, html=html) + if html: + href = str(escape(_href(child))) + parts.append(f'{inner}') + else: + parts.append(inner) + case "em": + inner = _render_inline(child.children, html=html) + parts.append(f"{inner}" if html else inner) + case "strong": + inner = _render_inline(child.children, html=html) + parts.append(f"{inner}" if html else inner) return "".join(parts) +def render_inline_html(children: list[SyntaxTreeNode]) -> str: + """Render inline AST nodes to HTML with proper escaping.""" + return _render_inline(children, html=True) + + def render_inline_text(children: list[SyntaxTreeNode]) -> str: """Render inline AST nodes to plain text (links become their text).""" - parts: list[str] = [] - for child in children: - match child.type: - case "text": - parts.append(child.content) - case "softbreak": - parts.append(" ") - case "code_inline": - parts.append(child.content) - case "em" | "strong" | "link": - parts.append(render_inline_text(child.children)) - return "".join(parts) + return _render_inline(children, html=False) # --- AST helpers -------------------------------------------------------------