class documentation

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__();
  • ... add additional render functions by appending to self.render_map;
Usage:

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.

Naming conventions:
  • The keys of self.render_map should exactly match the class name of tokens;
  • Render function names should be of form: "render_" + the "snake-case" form of token's class name.
Method __enter__ Make renderer classes into context managers.
Method __exit__ Make renderer classes into context managers.
Method __init__ Undocumented
Method render Grabs the class name from input token and finds its corresponding render function.
Method render_auto_link Undocumented
Method render_block_code Undocumented
Method render_document Undocumented
Method render_emphasis Undocumented
Method render_escape_sequence Undocumented
Method render_heading Undocumented
Method render_image Undocumented
Method render_inline_code Undocumented
Method render_inner Recursively renders child tokens. Joins the rendered strings with no space in between.
Method render_line_break Undocumented
Method render_link Undocumented
Method render_list Undocumented
Method render_list_item Undocumented
Method render_paragraph Undocumented
Method render_quote Undocumented
Method render_raw_text Default render method for RawText. Simply return token.content.
Method render_strikethrough Undocumented
Method render_strong Undocumented
Method render_table Undocumented
Method render_table_cell Undocumented
Method render_table_row Undocumented
Method render_thematic_break Undocumented
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.
def __enter__(self): (source)

Make renderer classes into context managers.

def __exit__(self, exception_type, exception_val, traceback): (source)

Make renderer classes into context managers.

Reset block_token._token_types and span_token._token_types.

def render(self, token): (source)

Grabs the class name from input token and finds its corresponding render function.

Basically a janky way to do polymorphism.

Parameters
tokenwhose __class__.__name__ is in self.render_map.
def render_inner(self, token) -> str: (source)

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
tokena branch node who has children attribute.
Returns
strUndocumented
footnotes: dict = (source)

Undocumented

render_map: dict = (source)

maps tokens to their corresponding render functions.

@classmethod
def _cls_to_func(cls, cls_name): (source)

Undocumented

@staticmethod
def _tokens_from_module(module): (source)

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.

_parse_name = (source)

Undocumented

a list of custom tokens to be added to the parsing process.