‹› markdown.treeprocessors

Tree processors manipulate the tree created by block processors. They can even create an entirely new ElementTree object. This is an excellent place for creating summaries, adding collected references, or last minute adjustments.

‹› markdown.treeprocessors.build_treeprocessors(md: Markdown, **kwargs: Any) -> util.Registry[Treeprocessor]

Build the default treeprocessors for Markdown.

Return a Registry instance which contains the following collection of classes with their assigned names and priorities.

Class Instance Name Priority
InlineProcessor inline 20
PrettifyTreeprocessor prettify 10
UnescapeTreeprocessor unescape 0

‹› markdown.treeprocessors.isString(s: object) -> bool

Return True if object is a string but not an AtomicString.

‹› markdown.treeprocessors.Treeprocessor(md: Markdown | None = None)

Bases: Processor

Treeprocessors are run on the ElementTree object before serialization.

Each Treeprocessor implements a run method that takes a pointer to an Element and modifies it as necessary.

Treeprocessors must extend markdown.Treeprocessor.

‹› markdown.treeprocessors.Treeprocessor.run(root: etree.Element) -> etree.Element | None

Subclasses of Treeprocessor should implement a run method, which takes a root Element. This method can return another Element object, and the existing root Element will be replaced, or it can modify the current tree and return None.

‹› markdown.treeprocessors.InlineProcessor(md: Markdown)

Bases: Treeprocessor

A Treeprocessor that traverses a tree, applying inline patterns.

‹› markdown.treeprocessors.InlineProcessor.run(tree: etree.Element, ancestors: list[str] | None = None) -> etree.Element

Apply inline patterns to a parsed Markdown tree.

Iterate over Element, find elements with inline tag, apply inline patterns and append newly created Elements to tree. To avoid further processing of string with inline patterns, instead of normal string, use subclass AtomicString:

node.text = markdown.util.AtomicString("This will not be processed.")

Parameters:

  • tree (Element) –

    Element object, representing Markdown tree.

  • ancestors (list[str] | None, default: None ) –

    List of parent tag names that precede the tree node (if needed).

Returns:

  • Element

    An element tree object with applied inline patterns.

‹› markdown.treeprocessors.PrettifyTreeprocessor(md: Markdown | None = None)

Bases: Treeprocessor

Add line breaks to the html document.

‹› markdown.treeprocessors.PrettifyTreeprocessor.run(root: etree.Element) -> None

Add line breaks to Element object and its children.

‹› markdown.treeprocessors.UnescapeTreeprocessor(md: Markdown | None = None)

Bases: Treeprocessor

Restore escaped chars

‹› markdown.treeprocessors.UnescapeTreeprocessor.run(root: etree.Element) -> None

Loop over all elements and unescape all text.