Render inline AST nodes to HTML or plain text.
(children: list[SyntaxTreeNode], *, html: bool)
| 64 | |
| 65 | |
| 66 | def _render_inline(children: list[SyntaxTreeNode], *, html: bool) -> str: |
| 67 | """Render inline AST nodes to HTML or plain text.""" |
| 68 | parts: list[str] = [] |
| 69 | for child in children: |
| 70 | match child.type: |
| 71 | case "text": |
| 72 | parts.append(str(escape(child.content)) if html else child.content) |
| 73 | case "html_inline": |
| 74 | if html: |
| 75 | parts.append(str(escape(child.content))) |
| 76 | case "softbreak": |
| 77 | parts.append(" ") |
| 78 | case "code_inline": |
| 79 | parts.append(f"<code>{escape(child.content)}</code>" if html else child.content) |
| 80 | case "link": |
| 81 | inner = _render_inline(child.children, html=html) |
| 82 | if html: |
| 83 | href = str(escape(_href(child))) |
| 84 | parts.append(f'<a href="{href}" target="_blank" rel="noopener">{inner}</a>') |
| 85 | else: |
| 86 | parts.append(inner) |
| 87 | case "em": |
| 88 | inner = _render_inline(child.children, html=html) |
| 89 | parts.append(f"<em>{inner}</em>" if html else inner) |
| 90 | case "strong": |
| 91 | inner = _render_inline(child.children, html=html) |
| 92 | parts.append(f"<strong>{inner}</strong>" if html else inner) |
| 93 | return "".join(parts) |
| 94 | |
| 95 | |
| 96 | def render_inline_html(children: list[SyntaxTreeNode]) -> str: |
no test coverage detected