Parsing Functions

Functions useful for simple parses:

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

Render text with a given renderer.

Parameters
  • iterable – string or list of strings

  • renderer – the renderer to use

  • parse_context (mistletoe.parse_context.ParseContext) – the parse context stores global parsing variables, such as the block/span tokens to search for, and link/footnote definitions that have been collected. If None, a new context will be instatiated, with the default block/span tokens for this renderer.

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

  • read_kwargs – key-word arguments to parse to the init_token.read method

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

mistletoe.block_tokenizer.tokenize_main(lines: mistletoe.base_elements.SourceLines, token_types=None, expand_spans: bool = True, skip_tokens: list = ('LinkDefinition', 'Footnote'))[source]

Searches for token_types in an iterable.

Parameters
  • lines – the source lines

  • 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.

  • skip_tokens – do not store these token.name in the syntax tree. These are usually tokens that store themselves in the global context

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, foot_definitions=None, logger: Optional[logging.Logger] = None)[source]

Bases: object

A class to contain context for a single parse.

Parameters
  • find_blocks – a list of block tokens to use during the parse. If None, the standard blocks will be used from BaseRenderer.default_block_token.

  • find_spans – a list of span tokens to use during the parse. If None, the standard blocks will be used from BaseRenderer.default_span_tokens.

  • link_definitions – a dict of link definitons, obtained from [def]: link

  • foot_definitions – a dict of footnote definitons, obtained from [^def]: link (if Footnote token active)

  • nesting_matches – a dict of matches recorded from find_nested_tokenizer

property foot_definitions
property foot_references
property logger
reset_definitions()[source]
copy()[source]
mistletoe.parse_context.get_parse_context(reset=False) → mistletoe.parse_context.ParseContext[source]

Return the current ParseContext (one per thread).

mistletoe.parse_context.set_parse_context(parse_context)[source]

Set an existing ParseContext (one per thread).

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.