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

cli.mddocs/

Command Line Interface

Command-line tool for processing Markdown files with support for various output formats and file I/O operations. The CLI provides a convenient way to convert Markdown files from the command line.

Capabilities

Main CLI Function

Entry point for the command-line interface that processes Markdown files and outputs converted content.

def main():
    """
    Command-line interface entry point for processing Markdown files.
    
    Processes command-line arguments and converts Markdown input to various formats.
    Available as the 'cmark' console command after installation.
    
    Command-line arguments:
        infile: Input Markdown file to parse (optional, defaults to stdin)
        -o [FILE]: Output HTML/JSON file (optional, defaults to stdout)
        -a: Print formatted AST instead of HTML
        -aj: Output JSON AST instead of HTML
        -h, --help: Show help message and exit
        
    Returns:
        None: Outputs results to stdout or specified output file, then exits
    """

Command Usage

The cmark command is available after installing the commonmark package:

pip install commonmark

Basic Usage

Convert Markdown file to HTML:

cmark input.md

Convert from stdin:

echo "# Hello World" | cmark

Input File

Process a single Markdown file:

cmark input.md

Output Redirection

Save output to a file:

cmark input.md > output.html

Or use the -o option:

cmark input.md -o output.html

Format Options

Output pretty-printed AST:

cmark input.md -a

Output JSON AST:

cmark input.md -aj

Usage Examples

Convert Single File

# Create a sample Markdown file
echo "# Hello World\n\nThis is **bold** text." > sample.md

# Convert to HTML
cmark sample.md
# Output: <h1>Hello World</h1>\n<p>This is <strong>bold</strong> text.</p>\n

Process from Standard Input

# Convert Markdown from stdin
echo "- Item 1\n- Item 2" | cmark
# Output: <ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\n

Single File Processing

# Process a single file
echo "# Sample Document" > sample.md
cmark sample.md
# Outputs HTML for the file

Advanced Examples

# Output AST for debugging
echo "# Title\n*italic*" | cmark -a

# Save JSON AST to file
cmark document.md -aj -o ast.json

# Convert to HTML with explicit output file
cmark README.md -o README.html

Integration with Other Tools

# Use with find to process Markdown files one at a time
find . -name "*.md" -exec cmark {} \;

# Process and save to HTML files
for file in *.md; do
    cmark "$file" -o "${file%.md}.html"
done

Programmatic Usage

You can also call the CLI function directly from Python:

import sys
from commonmark.cmark import main

# Simulate command-line arguments
sys.argv = ['cmark', 'input.md']
main()

Implementation Details

The CLI implementation:

  • Reads from a single file specified in command-line arguments or stdin if no file given
  • Uses the default HTML output format (same as commonmark.commonmark() with format="html")
  • Accepts only one input file at a time
  • Outputs results to stdout or specified output file with -o
  • Follows standard Unix conventions for input/output handling

The cmark console script is registered in the package's setup.py and points to commonmark.cmark:main.

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