Markdown

Python-Markdown extensions that process a document and add ANSI color codes to the output.

This module provides a set of extensions for python-markdown that render markdown elements using ANSI escape codes suitable for terminal display.

class ANSIExtension(theme=None, **kwargs)[source]

Main Python-Markdown extension that provides ANSI terminal output.

Initiate Extension and set up configs.

extendMarkdown(md)[source]

Register the ANSI processors.

Return type:

None

class ANSIMarkdown(theme=None, **kwargs)[source]

Markdown processor that converts markdown to ANSI formatted text. This is a convenience class that can be used in place of a markdown.Markdown instance to render markdown to ANSI formatted text suitable for terminal display.

Creates a new Markdown instance.

Keyword arguments:

  • extensions: A list of extensions.

    If an item is an instance of a subclass of markdown.extension.Extension, the instance will be used as-is. If an item is of type string, first an entry point will be loaded. If that fails, the string is assumed to use Python dot notation (path.to.module:ClassName) to load a markdown.Extension subclass. If no class is specified, then a makeExtension function is called within the specified module.

  • extension_configs: Configuration settings for extensions.

  • output_format: Format of output. Supported formats are:
    • “xhtml”: Outputs XHTML style tags. Default.

    • “html”: Outputs HTML style tags.

  • tab_length: Length of tabs in the source. Default: 4

class ANSIRenderer(theme=None)[source]

Base ANSI renderer that provides common formatting utilities.

Initialize the ANSI renderer with a theme.

Parameters:

theme – Theme instance to use for colors. If None, uses default theme.

_calculate_column_widths(rows)[source]

Calculate the width of each column in the table.

Return type:

List[int]

_create_table_border(col_widths, border_type)[source]

Create a table border line (top, separator, or bottom).

Return type:

str

_format_table_cells(row, col_widths, row_idx, header_row)[source]

Format all cells in a table row.

Return type:

List[str]

_strip_ansi(text)[source]

Strip ANSI escape codes from text for width calculation.

Return type:

str

blockquote(text)[source]

Format blockquote.

Return type:

str

bold(text)[source]

Format text as bold.

Return type:

str

code_block(text, language='')[source]

Format code block.

Return type:

str

code_inline(text)[source]

Format inline code.

Return type:

str

format_table(rows, header_row=True)[source]

Format a complete table with proper column alignment.

Return type:

str

header(text, level)[source]

Format header.

Return type:

str

horizontal_rule()[source]

Format horizontal rule.

Return type:

str

image(alt_text, url, title='')[source]

Format image (as text representation).

Return type:

str

italic(text)[source]

Format text as italic.

Return type:

str

link(text, url, title='')[source]

Format link.

Return type:

str

list_item(text, ordered=False, index=1)[source]

Format list item.

Return type:

str

strikethrough(text)[source]

Format text as strikethrough.

Return type:

str

underline(text)[source]

Format text as underlined.

Return type:

str

class ANSITreeProcessor(md, renderer)[source]

Tree processor that converts HTML elements to ANSI formatted text.

_format_blockquote(elem, content)[source]
Return type:

str

_format_bold(elem, content)[source]
Return type:

str

_format_br(elem, content)[source]
Return type:

str

_format_by_tag(tag, elem, content)[source]

Format content based on HTML tag.

Return type:

str

_format_code(elem, content)[source]

Format code - inline or block depending on parent.

Return type:

str

_format_code_block(elem, content)[source]

Format code block element.

Return type:

str

_format_hr(elem, content)[source]
Return type:

str

_format_image(elem, content)[source]

Format image element.

Return type:

str

_format_italic(elem, content)[source]
Return type:

str

_format_link(elem, content)[source]

Format link element.

Return type:

str

_format_list_container(elem, content)[source]
Return type:

str

_format_list_item(elem, content)[source]

Format list item element, determining if it’s ordered or unordered.

Return type:

str

_format_paragraph(elem, content)[source]
Return type:

str

_format_pre(elem, content)[source]
Return type:

str

_format_strikethrough(elem, content)[source]
Return type:

str

_format_table(elem, content)[source]

Format entire table element with proper column alignment.

Return type:

str

_format_table_element(elem, content)[source]
Return type:

str

_format_table_row(elem, content)[source]
Return type:

str

_format_underline(elem, content)[source]
Return type:

str

_is_header_tag(tag)[source]

Check if tag is a header tag (h1-h6).

Return type:

bool

_process_element(elem)[source]

Process a single element and its children.

Return type:

str

_set_parent_relationships(elem, parent=None)[source]

Set parent relationships for all elements.

Return type:

None

_setup_tag_formatters()[source]

Set up tag formatting dispatch table.

run(root)[source]

Process the element tree and convert to ANSI text.

Return type:

None

class CodeBlockPostprocessor(md, renderer)[source]

Postprocessor that replaces code block markers with ANSI formatted code.

Initialize the postprocessor.

Parameters:
  • md – The Markdown instance

  • renderer – ANSI renderer for formatting code blocks

run(text)[source]

Replace code block markers with ANSI formatted code.

Return type:

str

Parameters:

text – The processed markdown text

Returns:

Text with code blocks rendered as ANSI

class FencedCodePreprocessor(md)[source]

Preprocessor that handles fenced code blocks before markdown splits text into blocks.

This processor identifies fenced code blocks and stores them with unique markers, preventing markdown from processing the code content. The markers are later replaced by the tree processor.

Initialize the preprocessor.

Parameters:

md – The Markdown instance

FENCED_BLOCK_RE = re.compile('^[ \\t]*```(?P<lang>[\\w#+.-]*)[^\\n]*\\n(?P<code>.*?)^[ \\t]*```\\s*$', re.MULTILINE|re.DOTALL)
_create_code_html(code, language, marker_id)[source]

Create a simple marker for the code block.

Return type:

str

Parameters:
  • code – The code content

  • language – The programming language (can be empty)

  • marker_id – Unique marker ID for retrieval

Returns:

Marker string that will be replaced by postprocessor

_dedent_code(code, full_match)[source]

Remove base indentation from code block content.

Return type:

str

Parameters:
  • code – The code content

  • full_match – The full matched text including fences

Returns:

Code with base indentation removed

run(lines)[source]

Process fenced code blocks in the text.

Return type:

List[str]

Parameters:

lines – List of text lines to process

Returns:

List of processed lines with code blocks replaced by markers

markdown_to_ansi(text, theme=None, **kwargs)[source]

Convert markdown text to ANSI formatted text.

Return type:

str

Parameters:
  • text – Markdown text to convert

  • theme – Theme instance to use for colors. If None, uses default theme.

  • **kwargs – Additional arguments passed to markdown.markdown()

Returns:

ANSI formatted text suitable for terminal display