CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-commonmark

Python parser for the CommonMark Markdown spec

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

rendering.mddocs/

Rendering

Multiple output format renderers for converting AST nodes to various formats including HTML and reStructuredText. Renderers provide customizable options and can be extended for additional output formats.

Capabilities

HTML Renderer

Converts AST nodes to HTML output with configurable options for handling line breaks and other formatting details.

class HtmlRenderer:
    def __init__(self, options={}):
        """
        Initialize HTML renderer with optional configuration.
        
        Args:
            options (dict): Configuration options including:
                - 'softbreak': String to use for soft line breaks (default: '\n')
                - 'safe': Boolean to enable safe mode, filtering dangerous URLs (default: False)
                - 'sourcepos': Boolean to add source position attributes (default: False)
        """
        
    def render(self, ast):
        """
        Render an AST to HTML.
        
        Args:
            ast (Node): Root node of the AST to render
            
        Returns:
            str: HTML representation of the AST
        """
        
    def tag(self, name, attrs=None, selfclosing=None):
        """
        Add an HTML tag to output buffer.
        
        Args:
            name (str): Tag name
            attrs (list): List of [key, value] attribute pairs (optional)
            selfclosing (bool): Whether tag is self-closing (optional)
        """

reStructuredText Renderer

Converts AST nodes to reStructuredText format with configurable indentation.

class ReStructuredTextRenderer:
    def __init__(self, indent_char=' '):
        """
        Initialize reStructuredText renderer.
        
        Args:
            indent_char (str): Character to use for indentation (default: space)
        """
        
    def render(self, ast):
        """
        Render an AST to reStructuredText.
        
        Args:
            ast (Node): Root node of the AST to render
            
        Returns:
            str: reStructuredText representation of the AST
        """

Base Renderer Class

Abstract base class that provides common rendering functionality and can be extended to create custom renderers.

class Renderer:
    def render(self, ast):
        """
        Render an AST by walking through all nodes.
        
        Args:
            ast (Node): Root node of the AST to render
            
        Returns:
            str: Rendered output (format depends on renderer implementation)
        """
        
    def lit(self, s):
        """
        Concatenate a literal string to the output buffer.
        
        Args:
            s (str): String to add to output
        """
        
    def cr(self):
        """
        Add a newline to output if not already present at the end.
        """
        
    def out(self, s):
        """
        Concatenate a string to output, possibly with escaping.
        
        Args:
            s (str): String to add to output
        """

Usage Examples

HTML Rendering

from commonmark import Parser, HtmlRenderer

parser = Parser()
renderer = HtmlRenderer()

markdown = """
# Title

This is a paragraph with **bold** and *italic* text.

- List item 1
- List item 2
"""

ast = parser.parse(markdown)
html = renderer.render(ast)
print(html)

HTML with Custom Options

from commonmark import Parser, HtmlRenderer

parser = Parser()
renderer = HtmlRenderer(options={'softbreak': '<br />\n'})

markdown = "Line 1\nLine 2"
ast = parser.parse(markdown)
html = renderer.render(ast)
print(html)  # Soft breaks rendered as <br /> tags

reStructuredText Rendering

from commonmark import Parser, ReStructuredTextRenderer

parser = Parser()
renderer = ReStructuredTextRenderer()

markdown = """
# Main Title

## Subtitle

Some text with **bold** formatting.
"""

ast = parser.parse(markdown)
rst = renderer.render(ast)
print(rst)

Custom Indentation

from commonmark import Parser, ReStructuredTextRenderer

parser = Parser()
renderer = ReStructuredTextRenderer(indent_char='\t')  # Use tabs

markdown = """
- Item 1
  - Nested item
- Item 2
"""

ast = parser.parse(markdown)
rst = renderer.render(ast)
print(rst)  # Uses tab characters for indentation

Multiple Format Rendering

from commonmark import Parser, HtmlRenderer, ReStructuredTextRenderer

parser = Parser()
html_renderer = HtmlRenderer()
rst_renderer = ReStructuredTextRenderer()

markdown = "# Hello\n*World*"
ast = parser.parse(markdown)

html = html_renderer.render(ast)
rst = rst_renderer.render(ast)

print("HTML:", html)
print("RST:", rst)

Renderer Options

HTML Renderer Options

  • softbreak: String to use for soft line breaks (default: '\n')
  • safe: Boolean to enable safe mode, filtering dangerous URLs like javascript: and vbscript: (default: False)
  • sourcepos: Boolean to add data-sourcepos attributes to HTML elements with source position information (default: False)

reStructuredText Renderer Options

  • indent_char: Character to use for indentation (default: ' ' - space character)

Install with Tessl CLI

npx tessl i tessl/pypi-commonmark

docs

cli.md

high-level.md

index.md

parsing.md

rendering.md

utilities.md

tile.json