Parsing Functions

Functions useful for simple parses:

mistletoe.markdown(iterable, renderer=<class 'mistletoe.renderers.html.HTMLRenderer'>, init_token=<class 'mistletoe.block_tokens.Document'>, **kwargs)[source]

Render text with a given renderer.

Parameters
  • iterable – string or list of strings

  • init_token – The initial token to use for parsing the text token.read

  • kwargs – key-word arguments to parse to the renderer initialisation

mistletoe.block_tokenizer.tokenize_main(iterable, token_types=None, start_line=0, expand_spans=True, store_definitions=False)[source]

Searches for token_types in an iterable.

Parameters
  • iterable – list of strings (each line must end with a newline n!).

  • token_types – override block-level tokens set in global context

  • start_line – the source line number corresponding to iterable[0]

  • expand_spans – After the initial parse the span text is not yet tokenized, but stored instead as raw text in SpanContainer, in order to ensure all link definitons are read first. Setting True, runs a second walk of the syntax tree to replace these SpanContainer with the final span tokens.

  • store_definitions – store LinkDefinitions specifically in the syntax tree (or just record their target mappings globally and discard.)

Returns

list of block-level token instances.

mistletoe.span_tokenizer.tokenize_span(string, token_types=None)[source]

Convert a string to a list of span tokens.

Parameters
  • string – the string to parse

  • token_types – override block-level tokens set in global context

Returns

list of span-level token instances.

class mistletoe.base_elements.TokenEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

A JSON encoder for mistletoe tokens.

default(obj)[source]

Convert tokens to {token.name: token.to_dict()}, and expand SpanContainer.

mistletoe.base_elements.serialize_tokens(tokens, as_dict=False)[source]

Serialize one or more tokens, to a JSON representation.

Global Context

During a single parse, the require token sets and link definitions are stored as a (thread-safe) global instance. You can set this directly before using the functions above.

class mistletoe.parse_context.ParseContext(find_blocks=None, find_spans=None, link_definitions=None)[source]

Bases: object

copy()[source]
mistletoe.parse_context.get_parse_context(reset=False) → mistletoe.parse_context.ParseContext[source]

Return the current ParseContext.

mistletoe.parse_context.set_parse_context(parse_context)[source]

Set an existing ParseContext.

mistletoe.parse_context.tokens_from_classes(classes)[source]

Helper method; take a list of classes and/or class paths (e.g. mistletoe.span_tokens.Math) and return the loaded classes.

mistletoe.parse_context.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 single module.