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.
- 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 amakeExtensionfunction 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.
- _create_table_border(col_widths, border_type)[source]
Create a table border line (top, separator, or bottom).
- Return type:
- _format_table_cells(row, col_widths, row_idx, header_row)[source]
Format all cells in a table row.
- format_table(rows, header_row=True)[source]
Format a complete table with proper column alignment.
- Return type:
- class ANSITreeProcessor(md, renderer)[source]
Tree processor that converts HTML elements to ANSI formatted text.
- _format_code(elem, content)[source]
Format code - inline or block depending on parent.
- Return type:
- _format_list_item(elem, content)[source]
Format list item element, determining if it’s ordered or unordered.
- Return type:
- _format_table(elem, content)[source]
Format entire table element with proper column alignment.
- Return type:
- _set_parent_relationships(elem, parent=None)[source]
Set parent relationships for all elements.
- Return type:
- _setup_tag_formatters()[source]
Set up tag formatting dispatch table.
- 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
- 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:
- 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:
- Parameters:
code – The code content
full_match – The full matched text including fences
- Returns:
Code with base indentation removed
- markdown_to_ansi(text, theme=None, **kwargs)[source]
Convert markdown text to ANSI formatted text.
- Return type:
- 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