From 420bf8cd9d873139b8161f7537014b6bd75d4160 Mon Sep 17 00:00:00 2001 From: Vinta Chen Date: Sun, 19 Apr 2026 22:05:35 +0800 Subject: [PATCH] refactor(readme_parser): collapse render_inline_html/text into _render_inline helper Merge the two inline-renderer implementations into a single _render_inline(children, *, html) function that handles both output modes. The original public functions become one-line wrappers so all dispatch logic lives in one place. Also aligns html_inline handling: the html=True path now escapes the raw content instead of silently dropping it in the plain-text path. Co-Authored-By: Claude --- website/readme_parser.py | 54 +++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 28 deletions(-) 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 -------------------------------------------------------------