Skip to main content

Python parser for the CommonMark Markdown spec, Socialhome fork

Project description

Socialhome fork of https://github.com/readthedocs/commonmark.py

Tested on Python 3.8 - 3.10.

Fork changes

Installation

$ pip install commonmark-socialhome

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

commonmark_socialhome-0.9.1.post2.tar.gz (96.8 kB view details)

Uploaded Source

Built Distribution

commonmark_socialhome-0.9.1.post2-py2.py3-none-any.whl (51.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file commonmark_socialhome-0.9.1.post2.tar.gz.

File metadata

File hashes

Hashes for commonmark_socialhome-0.9.1.post2.tar.gz
Algorithm Hash digest
SHA256 9670f94fc30d929c51dc08c3d4218905e2725fca7ca9c82975f4099564af61b5
MD5 88be60ad85d805117a432f06132d2d55
BLAKE2b-256 b0746747a8b2c2592d952e4d91a5014f2bf919801bf3ed11b83e8a01ac37f8cb

See more details on using hashes here.

File details

Details for the file commonmark_socialhome-0.9.1.post2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for commonmark_socialhome-0.9.1.post2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0776b3b3a98cd9a37c472c6ae56536c31c76ac89d620c5c6387fd0e639d03353
MD5 7f4a47cb64ec6ea71996cd9adf67b890
BLAKE2b-256 0df0c161a5377e059048438b0a3bca3093ac4631bc7e19c16db383ba4efb884c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page