DOM API

This is the API for the md4c.domparser module, which provides a DOM-like parser built on top of the bindings from the main md4c module.

DOM Parser

class md4c.domparser.DOMParser(*args, **kwargs)

A subclass of md4c.ParserObject that produces document object model-like output. In other words, it produces a tree representation of the Markdown document’s block, inline, and text elements.

If the goal is simply to render a Markdown document in a suitable output format, this is a slow way to do it (in fact, that was one of the main motivations behind the underlying MD4C library). You should consider using an md4c.HTMLRenderer, a md4c.GenericParser, or subclasssing md4c.ParserObject instead. However, certain tasks, such as transformations on the document structure, can be difficult without a full abstract syntax tree, as produced by objects of this class.

Arguments to the constructor are passed through to md4c.GenericParser. Check that class and Option Flags for more information about that.

parse(markdown)

Produce an AST from the given Markdown document.

Parameters:

markdown (str or bytes) – The Markdown text to parse

Returns:

The root ASTNode of the document, by default a Document

Return type:

ASTNode

Base AST Classes

class md4c.domparser.ASTNode(element_type, use_bytes=False, **kwargs)

Base class for all AST nodes. When constructed, automatically constructs the appropriate subtype instead.

The default classes for particular Markdown element types can be replaced by subclassing with the appropriate class argument. (Since every other node class in this module already inherits from ASTNode, this works with any of them as well.) For example, to use a custom class to handle md4c.BlockType.HR:

class MyHorzontalRule(md4c.domparser.HorizontalRule,
                      element_type=md4c.BlockType.HR):
    '''My custom horizonal rule class'''
Parameters:
  • element_type – A md4c.BlockType, md4c.SpanType, or md4c.TextType representing the type of this element

  • use_bytes (bool, optional) – True if this node should render as bytes, False if it should render as str. Defaults to False.

type

A md4c.BlockType, md4c.SpanType, or md4c.TextType representing the type of element this object represents

bytes

True if this node should render as bytes, False if it should render as str. Normally gets set according to whether the Markdown input was a str or bytes.

parent

The parent of this node, or None (for the root Document node and attribute nodes)

attr_to_ast(attribute)

md4c.GenericParser represents attributes as lists of 2-tuples (or None). This static method converts them to a list of text ASTNode.

Parameters:

attribute – List of tuples or None

Returns:

List of text ASTNode or None

render_attr(attribute, url_escape=False)

Render an attribute given as a list of ASTNode or None

Parameters:
  • attribute – List of ASTNode or None

  • url_escape (bool, optional) – If True, perform URL escaping on the text. Otherwise, perform the default (HTML) escaping.

Returns:

Rendered output.

Return type:

str or bytes

render(**kwargs)

Render this node and its children. This base implementation returns an empty string, but subclasses should override as appropriate.

Parameters:

kwargs – Data passed from the parent node that may be useful for rendering. Non-leaf nodes should also pass this data on to their own children.

Returns:

Rendered output. The default AST types render HTML, but they can be replaced to render any output format necessary.

Return type:

str or bytes

class md4c.domparser.ContainerNode(element_type, **kwargs)

Base class for all AST nodes that may have children (blocks and spans except HorizontalRule).

Parameters:

element_type – A md4c.BlockType, or md4c.SpanType representing the type of this element

children

A list of this node’s children.

append(node)

Add a new child to the node and set its parent to this object

Parameters:

node – The child to add

insert(i, node)

Insert a new child into the node at index i

Parameters:
  • i – The new child will be at this index.

  • node – The child to insert

render_pre(**kwargs)

Render the opening for this node. This base implementation returns an empty string, but subclasses should override as appropriate.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered output. The default AST types render HTML, but they can be replaced to render any output format necessary.

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this node. This base implementation returns an empty string, but subclasses should override as appropriate.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered output. The default AST types render HTML, but they can be replaced to render any output format necessary.

Return type:

str or bytes

render(**kwargs)

A render implementation that should suit elements with children well: Renders the opening for this node, then all its children, then the closing for this node.

Parameters:

kwargs – Data passed from the parent node that may be useful for rendering. This data is also passed on to children.

Returns:

Rendered output. The default AST types render HTML, but they can be replaced to render any output format necessary.

Return type:

str or bytes

class md4c.domparser.TextNode(element_type, text, **kwargs)

Base class for all AST nodes representing text.

Parameters:
  • element_type – A md4c.TextType representing the type of this element

  • text (str or bytes) – The text this node represents, unprocessed

text

The unprocessed text for this node

classmethod html_escape(text)

Escape HTML special characters (&<>")

Parameters:

text (str or bytes) – Text to escape

Returns:

Escaped string or bytes

classmethod url_escape(text)

Percent-escape special characters in URLs

Parameters:

text (str or bytes) – URL to percent-escape

Returns:

Escaped string or bytes

render(url_escape=False, **kwargs)

Render the text for this node, performing HTML or URL escaping in the process. HTML escaping translates <, >, &, and " to HTML entities. URL escaping translates special characters to %xx.

Parameters:
  • url_escape (bool, optional) – If True, perform URL escaping on the text. Otherwise, perform the default HTML escaping.

  • kwargs – Other data passed from the parent node that may be useful for rendering.

Returns:

Rendered output, suitable for inclusion in an HTML document.

Return type:

str or bytes

Block AST Classes

class md4c.domparser.Document(element_type, **kwargs)

Document block. Root node of the AST. Inherits from ContainerNode.

Parameters:

element_typemd4c.BlockType.DOC

class md4c.domparser.Quote(element_type, **kwargs)

Quote block. Inherits from ContainerNode.

Parameters:

element_typemd4c.BlockType.QUOTE

render_pre(**kwargs)

Render the opening for this quote block.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this quote block.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.UnorderedList(element_type, is_tight, mark, **kwargs)

Unordered list block. Inherits from ContainerNode.

Parameters:
  • element_typemd4c.BlockType.UL

  • is_tight (bool) – Whether the list is tight or not

  • mark (str) – The character used as a bullet point

is_tight

Whether the list is tight or not

mark

The character used as a bullet point

render_pre(**kwargs)

Render the opening for this unordered list.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this unordered list.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.OrderedList(element_type, start, is_tight, mark, **kwargs)

Ordered list block. Inherits from ContainerNode.

Parameters:
  • element_typemd4c.BlockType.OL

  • start (int) – Start index of the ordered list

  • is_tight (bool) – Whether the list is tight or not

  • mark_delimiter (str) – The character used as the number delimiter

start

Start index of the ordered list

is_tight

Whether the list is tight or not

mark_delimiter

The character used as the number delimiter

render_pre(**kwargs)

Render the opening for this ordered list.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this ordered list.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.ListItem(element_type, is_task, task_mark, task_mark_offset, **kwargs)

List item block. Inherits from ContainerNode.

Parameters:
  • element_typemd4c.BlockType.LI

  • is_task (bool) – Whether the list item is a task list item

  • task_mark (str, optional) – The character used to mark the task. Not required if not a task list item.

  • task_mark_offset (int, optional) – The offset of the task mark between the []. Not required if not a task list item.

is_task

Whether the list item is a task list item

task_mark

The character used to mark the task (if a task list item)

task_mark_offset

The offset of the task mark between the [] (if a task list item)

render_pre(**kwargs)

Render the opening for this list item.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this list item.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.HorizontalRule(element_type, **kwargs)

Horizontal rule block. Inherits from ASTNode.

Parameters:

element_typemd4c.BlockType.HR

render(**kwargs)

Render this horizontal rule.

Parameters:

kwargs – Data passed from the parent node that may be useful for rendering.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Heading(element_type, level, **kwargs)

Heading block. Inherits from ContainerNode.

Parameters:
level

Heading level (1-6)

render_pre(**kwargs)

Render the opening for this heading.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this heading.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.CodeBlock(element_type, fence_char, info, lang, **kwargs)

Code block. Inherits from ContainerNode.

Parameters:
  • element_typemd4c.BlockType.CODE

  • fence_char (str or None) – Fence character. None for indented code blocks.

  • info (Attribute or None, optional) – Info string, if present.

  • lang (Attribute or None, optional) – Language, if present.

fence_char

Fence character, if a fenced code block. None otherwise.

info

Info string, as a list of ASTNode (if a fenced code block)

lang

Language, as a list of ASTNode (if a fenced code block)

render_pre(**kwargs)

Render the opening for this code block.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this heading.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.RawHTMLBlock(element_type, **kwargs)

Raw HTML block. Inherits from ContainerNode.

Parameters:

element_typemd4c.BlockType.HTML

class md4c.domparser.Paragraph(element_type, **kwargs)

Paragraph.

Parameters:

element_typemd4c.BlockType.P

render_pre(**kwargs)

Render the opening for this paragraph.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this paragraph.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Table(element_type, col_count, head_row_count, bod_row_count, **kwargs)

Table. Inherits from ContainerNode.

Parameters:
  • element_typemd4c.BlockType.TABLE

  • col_count (int) – Number of columns in the table

  • head_row_count (int) – Number of rows in the table head

  • body_row_count (int) – Number of rows in the table body

col_count

Number of columns in the table

head_row_count

Number of rows in the table head

body_row_count

Number of rows in the table body

render_pre(**kwargs)

Render the opening for this table.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this table.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.TableHead(element_type, **kwargs)

Table heading. Inherits from ContainerNode.

Parameters:

element_typemd4c.BlockType.THEAD

render_pre(**kwargs)

Render the opening for this table heading.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this table heading.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.TableBody(element_type, **kwargs)

Table body. Inherits from ContainerNode.

Parameters:

element_typemd4c.BlockType.TBODY

render_pre(**kwargs)

Render the opening for this table body.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this table body.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.TableRow(element_type, **kwargs)

Table row. Inherits from ContainerNode.

Parameters:

element_typemd4c.BlockType.TR

render_pre(**kwargs)

Render the opening for this table row.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this table row.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.TableHeaderCell(element_type, align, **kwargs)

Table header cell. Inherits from ContainerNode.

Parameters:
align

Text alignment for the cell (a md4c.BlockType.TH)

render_pre(**kwargs)

Render the opening for this table header cell.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this table header cell.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.TableCell(element_type, align, **kwargs)

Table cell. Inherits from ContainerNode.

Parameters:
align

Text alignment for the cell (a md4c.BlockType.TH)

render_pre(**kwargs)

Render the opening for this table cell.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(**kwargs)

Render the closing for this table cell.

Parameters:

kwargs – The data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

Inline AST Classes

class md4c.domparser.Emphasis(element_type, **kwargs)

Emphasis inline. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.EM

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this emphasis inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this emphasis inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Strong(element_type, **kwargs)

Strong emphasis inline. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.STRONG

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this strong emphasis inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this strong emphasis inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Underline(element_type, **kwargs)

Underline inline. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.U

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this underline inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this underline inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

Hyperlink inline. Inherits from ContainerNode.

Parameters:
href

Link URL, as a list of text ASTNode

title

Link title, as a list of text ASTNode (or None if not present)

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this link inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this link inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Image(element_type, src, title, **kwargs)

Image inline. Inherits from ContainerNode.

Parameters:
src

Image URL, as a list of ASTNode

title

Image title, as a list of ASTNode (or None if not present)

render_pre(image_nesting_level, **kwargs)

Render the opening for this image inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline, plus one for this image. HTML tags are not rendered inside image elements.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level, **kwargs)

Render the closing for this image inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline, plus one for this image. HTML tags are not rendered inside image elements.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render(image_nesting_level=0, **kwargs)

Render this image element and the alt text within.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this image. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Code(element_type, **kwargs)

Code inline. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.CODE

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this code inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this code inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.Strikethrough(element_type, **kwargs)

Strikethrough inline. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.DEL

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this strikethrough inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this strikethrough inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.InlineMath(element_type, **kwargs)

Inline math. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.LATEXMATH

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this inline math.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this inline math.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

class md4c.domparser.DisplayMath(element_type, **kwargs)

Display math. Inherits from ContainerNode.

Parameters:

element_typemd4c.SpanType.LATEXMATH_DISPLAY

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this display math.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this display math.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

Wiki link inline. Inherits from ContainerNode.

Parameters:
target

Link target, as a list of ASTNode

render_pre(image_nesting_level=0, **kwargs)

Render the opening for this wiki link inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

render_post(image_nesting_level=0, **kwargs)

Render the closing for this wiki link inline.

Parameters:
  • image_nesting_level (int, optional) – Number of image elements enclosing this inline. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed to the render() function from the parent node.

Returns:

Rendered HTML

Return type:

str or bytes

Text AST Classes

class md4c.domparser.NormalText(element_type, text, **kwargs)

Normal text. Inherits from TextNode.

Parameters:
text

The unprocessed text for this node

render(url_escape=False, **kwargs)

Render the text for this node, performing HTML or URL escaping in the process. HTML escaping translates <, >, &, and " to HTML entities. URL escaping translates special characters to %xx.

Parameters:
  • url_escape (bool, optional) – If True, perform URL escaping on the text. Otherwise, perform the default HTML escaping.

  • kwargs – Other data passed from the parent node that may be useful for rendering.

Returns:

Rendered output, suitable for inclusion in an HTML document.

Return type:

str or bytes

class md4c.domparser.NullChar(element_type, text, **kwargs)

Null character. Inherits from TextNode.

Parameters:
  • element_typemd4c.TextType.NULLCHAR

  • text (str or bytes) – Should be a null character, but this class assumes it is and ignores it.

render(**kwargs)

Render this null character (as the Unicode replacement character, codepoint 0xFFFD)

Parameters:

kwargs – Data passed from the parent node that may be useful for rendering.

Returns:

Null character

Return type:

str or bytes

text

The unprocessed text for this node

class md4c.domparser.LineBreak(element_type, text, **kwargs)

Line break. Inherits from TextNode.

Parameters:
  • element_typemd4c.TextType.BR

  • text (str or bytes) – Should be a newline character, but this class assumes it is and ignores it.

render(image_nesting_level=0, **kwargs)

Render this line break.

Parameters:
  • image_nesting_level – Number of image elements enclosing this line break. HTML tags are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed from the parent node.

Returns:

Rendered line break tag or space

Return type:

str or bytes

text

The unprocessed text for this node

class md4c.domparser.SoftLineBreak(element_type, text, **kwargs)

Soft line break. Inherits from TextNode.

Parameters:
  • element_typemd4c.TextType.SOFTBR

  • text (str or bytes) – Should be a newline character, but this class assumes it is and ignores it.

render(image_nesting_level=0, **kwargs)

Render this soft line break.

Parameters:
  • image_nesting_level – Number of image elements enclosing this line break. Line breaks are not rendered inside image elements. 0 assumed if not provided.

  • kwargs – The rest of the data passed from the parent node.

Returns:

Newline or space

Return type:

str or bytes

text

The unprocessed text for this node

class md4c.domparser.HTMLEntity(element_type, text, **kwargs)

HTML entity. Inherits from TextNode.

Parameters:
  • element_typemd4c.TextType.ENTITY

  • text (str or bytes) – The entity, including ampersand and semicolon

render(url_escape=False, **kwargs)

Render this HTML entity.

Parameters:
  • url_escape (bool, optional) – If True, perform URL escaping on the translated entity. Otherwise, perform the default HTML escaping.

  • kwargs – Data passed from the parent node that may be useful for rendering.

Returns:

Corresponding UTF-8 text for the entity.

Return type:

str or bytes

text

The unprocessed text for this node

class md4c.domparser.CodeText(element_type, text, **kwargs)

Text in a code block or code span. Inherits from TextNode.

Parameters:
text

The unprocessed text for this node

render(url_escape=False, **kwargs)

Render the text for this node, performing HTML or URL escaping in the process. HTML escaping translates <, >, &, and " to HTML entities. URL escaping translates special characters to %xx.

Parameters:
  • url_escape (bool, optional) – If True, perform URL escaping on the text. Otherwise, perform the default HTML escaping.

  • kwargs – Other data passed from the parent node that may be useful for rendering.

Returns:

Rendered output, suitable for inclusion in an HTML document.

Return type:

str or bytes

class md4c.domparser.HTMLText(element_type, text, **kwargs)

Raw HTML text. Inherits from TextNode.

Parameters:
render(**kwargs)

Render this HTML text.

Parameters:

kwargs – Data passed from the parent node that may be useful for rendering.

Returns:

Raw HTML text

Return type:

str or bytes

text

The unprocessed text for this node

class md4c.domparser.MathText(element_type, text, **kwargs)

Text in an equation. Inherits from TextNode.

Parameters:
text

The unprocessed text for this node

render(url_escape=False, **kwargs)

Render the text for this node, performing HTML or URL escaping in the process. HTML escaping translates <, >, &, and " to HTML entities. URL escaping translates special characters to %xx.

Parameters:
  • url_escape (bool, optional) – If True, perform URL escaping on the text. Otherwise, perform the default HTML escaping.

  • kwargs – Other data passed from the parent node that may be useful for rendering.

Returns:

Rendered output, suitable for inclusion in an HTML document.

Return type:

str or bytes