class documentation

class BaseRenderer(object):

Known subclasses: mistletoe.ast_renderer.ASTRenderer, mistletoe.html_renderer.HTMLRenderer, mistletoe.latex_renderer.LaTeXRenderer

View In Hierarchy

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 __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.
def __enter__(self):
Make renderer classes into context managers.
def __exit__(self, exception_type, exception_val, traceback):

Make renderer classes into context managers.

Reset block_token._token_types and span_token._token_types.

def __getattr__(self, name):

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.

def __init__(self, *extras):
def render(self, token):

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):

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.
def render_raw_text(self, token):
Default render method for RawText. Simply return token.content.
footnotes: dict =

Undocumented

render_map: dict =
maps tokens to their corresponding render functions.
@classmethod
def _cls_to_func(cls, cls_name):

Undocumented

@staticmethod
def _tokens_from_module(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.
_parse_name =

Undocumented

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