class BaseRenderer(object):
Known subclasses: mistletoe.ast_renderer.ASTRenderer
, mistletoe.html_renderer.HTMLRenderer
, mistletoe.latex_renderer.LaTeXRenderer
Base class for renderers.
All renderers should ... * ... define all render functions specified in self.render_map; * ... be a context manager (by inheriting __enter__ and __exit__);
Custom renderers could ... * ... add additional tokens into the parsing process by passing custom
tokens to super().__init__();
Suppose SomeRenderer inherits BaseRenderer, and fin is the input file. The syntax looks something like this:
>>> from mistletoe import Document >>> from some_renderer import SomeRenderer >>> with SomeRenderer() as renderer: ... rendered = renderer.render(Document(fin))
See mistletoe.html_renderer for an implementation example.
Method | __enter__ |
Make renderer classes into context managers. |
Method | __exit__ |
Make renderer classes into context managers. |
Method | __getattr__ |
Provides a default render method for all tokens. |
Method | __init__ |
Undocumented |
Method | render |
Grabs the class name from input token and finds its corresponding render function. |
Method | render_inner |
Recursively renders child tokens. Joins the rendered strings with no space in between. |
Method | render_raw_text |
Default render method for RawText. Simply return token.content. |
Instance Variable | footnotes |
Undocumented |
Instance Variable | render_map |
maps tokens to their corresponding render functions. |
Class Method | _cls_to_func |
Undocumented |
Static Method | _tokens_from_module |
Helper method; takes a module and returns a list of all token classes specified in module.__all__. Useful when custom tokens are defined in a separate module. |
Class Variable | _parse_name |
Undocumented |
Instance Variable | _extras |
a list of custom tokens to be added to the parsing process. |
mistletoe.html_renderer.HTMLRenderer
Make renderer classes into context managers.
Reset block_token._token_types and span_token._token_types.
mistletoe.ast_renderer.ASTRenderer
Provides a default render method for all tokens.
Any token without a custom render method will simply be rendered by self.render_inner.
If name does not start with 'render_', raise AttributeError as normal, for less magic during debugging.
This method would only be called if the attribute requested has not been defined. Defined attributes will not be overridden.
I still think this is heavy wizardry. Let me know if you would like this method removed.
mistletoe.ast_renderer.ASTRenderer
Grabs the class name from input token and finds its corresponding render function.
Basically a janky way to do polymorphism.
Parameters | |
token | whose __class__.__name__ is in self.render_map. |
Recursively renders child tokens. Joins the rendered strings with no space in between.
If newlines / spaces are needed between tokens, add them in their respective templates, or override this function in the renderer subclass, so that whitespace won't seem to appear magically for anyone reading your program.
Parameters | |
token | a branch node who has children attribute. |