Skip to main content

CommonMark-py

CommonMark-py is a pure Python port of jgm’s commonmark.js, a Markdown parser and renderer for the CommonMark specification, using only native modules. Once both this project and the CommonMark specification are stable we will release the first 1.0 version and attempt to keep up to date with changes in commonmark.js.

CommonMark-py is tested against the CommonMark spec with Python versions 2.6, 2.7, 3.3, 3.4, 3.5, 3.6, and 3.7.

Current version: 0.8.1

Build Status Documentation Status

Installation

$ pip install commonmark

Usage

>>> import commonmark
>>> commonmark.commonmark('*hello!*')
'<p><em>hello!</em></p>\n'

Or, without the syntactic sugar:

import commonmark
parser = commonmark.Parser()
ast = parser.parse("Hello *World*")

renderer = commonmark.HtmlRenderer()
html = renderer.render(ast)
print(html) # <p>Hello <em>World</em><p/>

# inspecting the abstract syntax tree
json = commonmark.dumpJSON(ast)
commonmark.dumpAST(ast) # pretty print generated AST structure

There is also a CLI:

$ cmark README.md -o README.html
$ cmark README.md -o README.json -aj # output AST as JSON
$ cmark README.md -a # pretty print generated AST structure
$ cmark -h
usage: cmark [-h] [-o [O]] [-a] [-aj] [infile]

Process Markdown according to the CommonMark specification.

positional arguments:
  infile      Input Markdown file to parse, defaults to stdin

optional arguments:
  -h, --help  show this help message and exit
  -o [O]      Output HTML/JSON file, defaults to stdout
  -a          Print formatted AST
  -aj         Output JSON AST

Contributing

If you would like to offer suggestions/optimizations/bugfixes through pull requests please do! Also if you find an error in the parser/renderer that isn’t caught by the current test suite please open a new issue and I would also suggest you send the commonmark.js project a pull request adding your test to the existing test suite.

Tests

To work on CommonMark-py, you will need to be able to run the test suite to make sure your changes don’t break anything. To run the tests, you can do something like this:

$ pyvenv venv
$ ./venv/bin/python setup.py develop test

The tests script, commonmark/tests/run_spec_tests.py, is pretty much a devtool. As well as running all the tests embedded in spec.txt it also allows you to run specific tests using the -t argument, provide information about passed tests with -p, percentage passed by category of test with -s, and enter markdown interactively with -i (In interactive mode end a block by inputting a line with just end, to quit do the same but with quit). -d can be used to print call tracing.

$ ./venv/bin/python commonmark/tests/run_spec_tests.py -h
usage: run_spec_tests.py [-h] [-t T] [-p] [-f] [-i] [-d] [-np] [-s]

script to run the CommonMark specification tests against the CommonMark-py
parser.

optional arguments:
  -h, --help  show this help message and exit
  -t T        Single test to run or comma separated list of tests (-t 10 or -t 10,11,12,13)
  -p          Print passed test information
  -f          Print failed tests (during -np...)
  -i          Interactive Markdown input mode
  -d          Debug, trace calls
  -np         Only print section header, tick, or cross
  -s          Print percent of tests passed by category

Authors

Metadata

Release files for commonmark 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for commonmark 0.8.1
File Size Uploaded
commonmark-0.8.1.tar.gz 90.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for commonmark 0.8.1
File Interpreter ABI Platform
commonmark-0.8.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 138.2 kB

Release files / commonmark-0.8.1.tar.gz

Download URL commonmark-0.8.1.tar.gz
Size 90.8 kB
Tags Source
SHA-256 checksum
How to use checksums
abcbc854e0eae5deaf52ae5e328501b78b4a0758bf98ac8bb792fce993006084
BLAKE2b-256 checksum
How to use checksums
21ba5133e424a9112aa76cf09ce82cbc2af712691f4a80abd123b6de3aa47f07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

Release files / commonmark-0.8.1-py2.py3-none-any.whl

Download URL commonmark-0.8.1-py2.py3-none-any.whl
Size 47.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9f6dda7876b2bb88dd784440166f4bc8e56cb2b2551264051123bacb0b6c1d8a
BLAKE2b-256 checksum
How to use checksums
abca439c88039583a29564a0043186875258e9a4f041fb5c422cd387b8e10175
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

Release history Release notifications | RSS feed

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.5

2 release files

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page