‹› markdown.blockprocessors

A block processor parses blocks of text and adds new elements to the ElementTree. Blocks of text, separated from other text by blank lines, may have a different syntax and produce a differently structured tree than other Markdown. Block processors excel at handling code formatting, equation layouts, tables, etc.

‹› markdown.blockprocessors.build_block_parser(md: Markdown, **kwargs: Any) -> BlockParser

Build the default block parser used by Markdown.

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

Class Instance Name Priority
EmptyBlockProcessor empty 100
ListIndentProcessor indent 90
CodeBlockProcessor code 80
HashHeaderProcessor hashheader 70
SetextHeaderProcessor setextheader 60
HRProcessor hr 50
OListProcessor olist 40
UListProcessor ulist 30
BlockQuoteProcessor quote 20
ReferenceProcessor reference 15
ParagraphProcessor paragraph 10

‹› markdown.blockprocessors.BlockProcessor(parser: BlockParser)

Base class for block processors.

Each subclass will provide the methods below to work with the source and tree. Each processor will need to define it’s own test and run methods. The test method should return True or False, to indicate whether the current block should be processed by this processor. If the test passes, the parser will call the processors run method.

Attributes:

  • BlockProcessor.parser (BlockParser) –

    The BlockParser instance this is attached to.

  • BlockProcessor.tab_length (int) –

    The tab length set on the Markdown instance.

‹› markdown.blockprocessors.BlockProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.BlockProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.BlockProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.BlockProcessor.test(parent: etree.Element, block: str) -> bool

Test for block type. Must be overridden by subclasses.

As the parser loops through processors, it will call the test method on each to determine if the given block of text is of that type. This method must return a boolean True or False. The actual method of testing is left to the needs of that particular block type. It could be as simple as block.startswith(some_string) or a complex regular expression. As the block type may be different depending on the parent of the block (i.e. inside a list), the parent etree element is also provided and may be used as part of the test.

Other Parameters:

  • parent (Element) –

    An etree element which will be the parent of the block.

  • block (str) –

    A block of text from the source which has been split at blank lines.

‹› markdown.blockprocessors.BlockProcessor.run(parent: etree.Element, blocks: list[str]) -> bool | None

Run processor. Must be overridden by subclasses.

When the parser determines the appropriate type of a block, the parser will call the corresponding processor’s run method. This method should parse the individual lines of the block and append them to the etree.

Note that both the parent and etree keywords are pointers to instances of the objects which should be edited in place. Each processor must make changes to the existing objects as there is no mechanism to return new/different objects to replace them.

This means that this method should be adding SubElements or adding text to the parent, and should remove (pop) or add (insert) items to the list of blocks.

If False is returned, this will have the same effect as returning False from the test method.

Other Parameters:

  • parent (Element) –

    An etree element which is the parent of the current block.

  • blocks (list[str]) –

    A list of all remaining blocks of the document.

‹› markdown.blockprocessors.ListIndentProcessor(*args)

Bases: BlockProcessor

Process children of list items.

Example

* a list item
    process this part

    or this part

‹› markdown.blockprocessors.ListIndentProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.ListIndentProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.ListIndentProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.ListIndentProcessor.ITEM_TYPES class-attribute instance-attribute

List of tags used for list items.

Defined Value:

ITEM_TYPES = ['li']

‹› markdown.blockprocessors.ListIndentProcessor.LIST_TYPES class-attribute instance-attribute

Types of lists this processor can operate on.

Defined Value:

LIST_TYPES = ['ul', 'ol']

‹› markdown.blockprocessors.ListIndentProcessor.create_item(parent: etree.Element, block: str) -> None

Create a new li and parse the block with it as the parent.

‹› markdown.blockprocessors.ListIndentProcessor.get_level(parent: etree.Element, block: str) -> tuple[int, etree.Element]

Get level of indentation based on list level.

‹› markdown.blockprocessors.CodeBlockProcessor(parser: BlockParser)

Bases: BlockProcessor

Process code blocks.

‹› markdown.blockprocessors.CodeBlockProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.CodeBlockProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.CodeBlockProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.BlockQuoteProcessor(parser: BlockParser)

Bases: BlockProcessor

Process blockquotes.

‹› markdown.blockprocessors.BlockQuoteProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.BlockQuoteProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.BlockQuoteProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.BlockQuoteProcessor.clean(line: str) -> str

Remove > from beginning of a line.

‹› markdown.blockprocessors.OListProcessor(parser: BlockParser)

Bases: BlockProcessor

Process ordered list blocks.

‹› markdown.blockprocessors.OListProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.OListProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.OListProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.OListProcessor.TAG: str class-attribute instance-attribute

The tag used for the the wrapping element.

Defined Value:

TAG: str = 'ol'

‹› markdown.blockprocessors.OListProcessor.STARTSWITH: str class-attribute instance-attribute

The integer (as a string ) with which the list starts. For example, if a list is initialized as 3. Item, then the ol tag will be assigned an HTML attribute of starts="3". Default: "1".

Defined Value:

STARTSWITH: str = '1'

‹› markdown.blockprocessors.OListProcessor.LAZY_OL: bool class-attribute instance-attribute

Ignore STARTSWITH if True.

Defined Value:

LAZY_OL: bool = True

‹› markdown.blockprocessors.OListProcessor.SIBLING_TAGS: list[str] class-attribute instance-attribute

Markdown does not require the type of a new list item match the previous list item type. This is the list of types which can be mixed.

Defined Value:

SIBLING_TAGS: list[str] = ['ol', 'ul']

‹› markdown.blockprocessors.OListProcessor.get_items(block: str) -> list[str]

Break a block into list items.

‹› markdown.blockprocessors.UListProcessor(parser: BlockParser)

Bases: OListProcessor

Process unordered list blocks.

‹› markdown.blockprocessors.UListProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.UListProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.UListProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.UListProcessor.STARTSWITH: str class-attribute instance-attribute

The integer (as a string ) with which the list starts. For example, if a list is initialized as 3. Item, then the ol tag will be assigned an HTML attribute of starts="3". Default: "1".

Defined Value:

STARTSWITH: str = '1'

‹› markdown.blockprocessors.UListProcessor.LAZY_OL: bool class-attribute instance-attribute

Ignore STARTSWITH if True.

Defined Value:

LAZY_OL: bool = True

‹› markdown.blockprocessors.UListProcessor.SIBLING_TAGS: list[str] class-attribute instance-attribute

Markdown does not require the type of a new list item match the previous list item type. This is the list of types which can be mixed.

Defined Value:

SIBLING_TAGS: list[str] = ['ol', 'ul']

‹› markdown.blockprocessors.UListProcessor.get_items(block: str) -> list[str]

Break a block into list items.

‹› markdown.blockprocessors.UListProcessor.TAG: str class-attribute instance-attribute

The tag used for the the wrapping element.

Defined Value:

TAG: str = 'ul'

‹› markdown.blockprocessors.HashHeaderProcessor(parser: BlockParser)

Bases: BlockProcessor

Process Hash Headers.

‹› markdown.blockprocessors.HashHeaderProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.HashHeaderProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.HashHeaderProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.SetextHeaderProcessor(parser: BlockParser)

Bases: BlockProcessor

Process Setext-style Headers.

‹› markdown.blockprocessors.SetextHeaderProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.SetextHeaderProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.SetextHeaderProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.HRProcessor(parser: BlockParser)

Bases: BlockProcessor

Process Horizontal Rules.

‹› markdown.blockprocessors.HRProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.HRProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.HRProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.EmptyBlockProcessor(parser: BlockParser)

Bases: BlockProcessor

Process blocks that are empty or start with an empty line.

‹› markdown.blockprocessors.EmptyBlockProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.EmptyBlockProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.EmptyBlockProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.ReferenceProcessor(parser: BlockParser)

Bases: BlockProcessor

Process link references.

‹› markdown.blockprocessors.ReferenceProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.ReferenceProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.ReferenceProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.

‹› markdown.blockprocessors.ParagraphProcessor(parser: BlockParser)

Bases: BlockProcessor

Process Paragraph blocks.

‹› markdown.blockprocessors.ParagraphProcessor.lastChild(parent: etree.Element) -> etree.Element | None

Return the last child of an etree element.

‹› markdown.blockprocessors.ParagraphProcessor.detab(text: str, length: int | None = None) -> tuple[str, str]

Remove a tab from the front of each line of the given text.

‹› markdown.blockprocessors.ParagraphProcessor.looseDetab(text: str, level: int = 1) -> str

Remove a tab from front of lines but allowing dedented lines.