Python parser for the CommonMark Markdown spec
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
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.
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
"""The cmark command is available after installing the commonmark package:
pip install commonmarkConvert Markdown file to HTML:
cmark input.mdConvert from stdin:
echo "# Hello World" | cmarkProcess a single Markdown file:
cmark input.mdSave output to a file:
cmark input.md > output.htmlOr use the -o option:
cmark input.md -o output.htmlOutput pretty-printed AST:
cmark input.md -aOutput JSON AST:
cmark input.md -aj# 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# 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# Process a single file
echo "# Sample Document" > sample.md
cmark sample.md
# Outputs HTML for the file# 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# 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"
doneYou 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()The CLI implementation:
commonmark.commonmark() with format="html")-oThe 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