Minimal bindings to GitHub's fork of cmark
Project description
Minimalist Python bindings to GitHub’s fork of cmark.
Installation
This package is published on PyPI as cmarkgfm and can be installed with pip or pipenv:
pip install --user cmarkgfm pipenv install cmarkgfm
Wheels are provided for macOS, Linux, and Windows for Python 2.7, 3.5, 3.6, 3.7, and 3.8.
Usage
High-level usage is really straightforward. To render normal CommonMark markdown:
import cmarkgfm
html = cmarkgfm.markdown_to_html(markdown_text)
To render GitHub-flavored markdown:
import cmarkgfm
html = cmarkgfm.github_flavored_markdown_to_html(markdown_text)
Advanced Usage
Safe rendering
CommonMark can render potentially unsafe HTML, including raw HTML, raw Javascript, and potentially unsafe links (including links that run scripts). Although github_flavored_markdown_to_html prevents some raw HTML tags (including script) from being rendered, it does not block unsafe URLs in links.
Therefore it is recommend to call the rendering method with the SAFE option turned on. The safe option does not render raw HTML or potentially dangerous URLs. (Raw HTML is replaced by a placeholder comment; potentially dangerous URLs are replaced by empty strings.) Dangerous URLs are those that begin with javascript:, vbscript:, file:, or data: (except for image/png, image/gif, image/jpeg, or image/webp mime types) To do this, use:
import cmarkgfm
from cmarkgfm.cmark import Options as cmarkgfmOptions
html = cmarkgfm.markdown_to_html(markdown_text, options=cmarkgfmOptions.CMARK_OPT_SAFE)
# or
html = cmarkgfm.github_flavored_markdown_to_html(markdown_text, options=cmarkgfmOptions.CMARK_OPT_SAFE)
If you trust the markdown text to not include any unsafe tags and links, then you may skip this.
In version 0.4.0 and earlier, the default behavior is unsafe, as described above. In later versions, the default behavior is safe, and to render potentially unsafe HTML pass the CMARK_OPT_UNSAFE option.
Options
Both rendering methods markdown_to_html and github_flavored_markdown_to_html have an optional options argument that can be used to activate options of cmark. For example:
import cmarkgfm
from cmarkgfm.cmark import Options as cmarkgfmOptions
html = cmarkgfm.markdown_to_html(markdown_text, options=cmarkgfmOptions.CMARK_OPT_SAFE | cmarkgfmOptions.CMARK_OPT_SMART)
The options are:
Option |
Effect |
---|---|
CMARK_OPT_SAFE (≤0.4.0) |
Prevents rendering unsafe HTML and links. |
CMARK_OPT_UNSAFE (>0.4.0) |
Allows rendering unsafe HTML and links. |
CMARK_OPT_SMART |
Render curly quotes, en/em-dashes, ellipses |
CMARK_OPT_NORMALIZE |
Consolidate adjacent text nodes. |
CMARK_OPT_HARDBREAKS |
Renders line breaks within paragraphs as <br> |
CMARK_OPT_NOBREAKS |
Render soft line breaks as spaces. |
CMARK_OPT_SOURCEPOS |
Adds data-sourcepos to HTML tags indicating the corresponding line/col ranges in the input |
Contributing
Pull requests are welcome. :)
License
This project is under the MIT License. It includes components under differing copyright under the third_party directory in this source tree.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cmarkgfm-0.5.0.tar.gz
.
File metadata
- Download URL: cmarkgfm-0.5.0.tar.gz
- Upload date:
- Size: 134.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a5131a78836e55dcdb1f2c5f17bbaa40e5f83c86a205de1b71a298928e1391f |
|
MD5 | 8dd59e5dfa4ccda50b21a9a1034cd967 |
|
BLAKE2b-256 | b538176fbb9e1edd9cb51696f198fcc3f7a91ebb6a364ba7a71115a23a3e05cb |
Provenance
File details
Details for the file cmarkgfm-0.5.0-cp27-cp27m-win32.whl
.
File metadata
- Download URL: cmarkgfm-0.5.0-cp27-cp27m-win32.whl
- Upload date:
- Size: 120.6 kB
- Tags: CPython 2.7m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.6.1 requests/2.25.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/2.7.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83bfa4f23a5a67cd34ce39b11b04304daf9e99c7ca0ae02c051dedc5e181f6b1 |
|
MD5 | 60f7c130ebb8a7538467645996ba063c |
|
BLAKE2b-256 | d12d6ceb2ded7a87ee5f99f4a6069ab94b836860f8b636acc4513cf221d3cf9e |