Renderers#
- class wenmode.renderers.RenderContext#
Base render context passed through renderer handlers.
- wenmode.renderers.RenderHandler#
alias of
Callable[[…],str]
- class wenmode.renderers.BaseRenderer#
Dispatch-based base class for renderers.
Subclasses register handlers with
register(). A handler receives the renderer instance, the node, and the render context, and returns rendered text.- property supports_streaming: bool#
Return whether this renderer can omit root hooks during streaming.
- streaming_blockers()#
Return root hook labels that prevent streaming output.
- create_context(node=None)#
Create a render context for one render call.
- Parameters:
node (Node | None) – Root node being rendered, when available.
- Returns:
New render context.
- Return type:
- render(node, context=None)#
Render a node.
- Parameters:
node (Node) – Node to render.
context (RenderContext | None) – Optional render context. When omitted, a new context is created.
- Returns:
Rendered output.
- Return type:
str
- render_root(node, context)#
Render a root node with registered root pre/post render hooks.
- render_iter(nodes)#
Render an iterable of nodes with one shared context.
- Parameters:
nodes (Iterable[Node]) – Nodes to render.
- Returns:
Iterator of rendered chunks.
- Return type:
Iterator[str]
- render_node(node, context)#
Render one node with an existing context.
- render_children(children, context)#
Render child nodes and concatenate their output.
- classmethod register(node_type)#
Register a render handler for a node type.
- Parameters:
node_type (str) – Value of
node.typehandled by the decorated function. Useroot:preorroot:postfor root-level hooks.- Returns:
Decorator that stores the handler on the renderer class.
- Return type:
Callable[[Callable[[…], str]], Callable[[…], str]]
- register_handler(node_type, handler)#
Register a render handler or root-level hook on this renderer instance.
- render_unknown(node, context)#
Render a node without a registered handler.
The default behavior renders child nodes, then literal
valuefields, and otherwise returns an empty string.
- class wenmode.HTMLRenderer(escape=True, sanitize_urls=True, sanitize_attrs=True, directives=())#
Render Wenmode nodes as HTML.
- Parameters:
escape (bool) – Escape raw HTML nodes when
True.sanitize_urls (bool) – Drop unsafe URL schemes from links and images when
True.sanitize_attrs (bool) – Drop event handler and style attribute names when
True.directives (Iterable[DirectiveHtmlRenderer]) – Directive renderers to register at construction time.
- create_context(node=None)#
Create an HTML render context.
- register_directive_renderer(directive)#
Register one directive renderer.
- Parameters:
directive (DirectiveHtmlRenderer) – Directive renderer implementation.
- render_unknown(node, context)#
Render an unknown node without allowing implicit HTML markup.
- escape(value)#
Escape raw HTML when renderer escaping is enabled.
- escape_html(value)#
Escape a string for HTML text or attribute output.
- render_attrs(attrs)#
Render a mapping as HTML attributes.
NoneandFalsevalues are omitted.Truevalues render as boolean attributes.
- is_safe_attr_name(name)#
Return whether an attribute name is safe for generated HTML.
- sanitize_url(value)#
Return a URL if its scheme is allowed, otherwise
None.
- class wenmode.MarkdownRenderer#
Render Wenmode nodes as normalized Markdown.
The renderer serializes the AST and is not source preserving. Syntax details that are not represented in the node tree may be normalized.
- escape_text(value)#
Escape Markdown punctuation in plain text.
- escape_destination(value)#
Escape a link or image destination.
- escape_title(value)#
Escape a link or image title.
- class wenmode.RSTRenderer#
Render Wenmode nodes as reStructuredText.
- create_context(node=None)#
Create a reStructuredText render context.
- escape_text(value)#
Escape reStructuredText punctuation in plain text.
- escape_inline_literal(value)#
Escape text inside an inline literal.
- escape_interpreted_text(value)#
Escape text inside a reStructuredText interpreted text role.
- escape_link_label(value)#
Escape text inside a reStructuredText hyperlink label.
- escape_link_target(value)#
Escape a link or image target.
- escape_directive_option(value)#
Escape text inside a one-line reStructuredText directive option.
- class wenmode.AsciiDocRenderer#
Render Wenmode nodes as normalized AsciiDoc.
The renderer is a best-effort serializer for Wenmode’s core AST nodes. It is not source preserving and does not install handlers for built-in plugin nodes.
- create_context(node=None)#
Create an AsciiDoc render context.
- escape_text(value)#
Escape common AsciiDoc inline punctuation in plain text.
- escape_link_target(value)#
Escape a link or image target.
- escape_attribute_value(value)#
Escape text inside an AsciiDoc macro attribute.