Skip to main content

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 3.6, 3.7, 3.8, 3.9, 3.10 and 3.11.

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

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

options = (
    cmarkgfmOptions.CMARK_OPT_GITHUB_PRE_LANG
    | cmarkgfmOptions.CMARK_OPT_SMART
)
html = cmarkgfm.markdown_to_html(markdown_text, options)

The options are:

Option

Effect

CMARK_OPT_UNSAFE (>=0.5.0)

Allows rendering unsafe HTML and links.

CMARK_OPT_SAFE (<0.5.0)

Prevents 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

CMARK_OPT_FOOTNOTES

Parse footnotes.

CMARK_OPT_VALIDATE_UTF8

Validate UTF-8 in the input before parsing, replacing illegal sequenceswith the replacement character U+FFFD.

CMARK_OPT_GITHUB_PRE_LANG

Use GitHub-style tags for code blocks.

CMARK_OPT_LIBERAL_HTML_TAG

Be liberal in interpreting inline HTML tags.

CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE

Only parse strikethroughs if surrounded by exactly 2 tildes. Gives some compatibility with redcarpet.

CMARK_OPT_TABLE_PREFER_STYLE_ATTRIBUTES

Use style attributes to align table cells instead of align attributes.

Unsafe rendering

Since version 0.5.0, the default behavior is safe. In earlier versions, the default behavior is unsafe, as described below. To render potentially unsafe HTML since 0.5.0 pass the CMARK_OPT_UNSAFE option.

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:

# cmarkgfm<0.5.0
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.

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


Download files

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

Source Distribution

cmarkgfm-2022.10.26.tar.gz (141.0 kB view hashes)

Uploaded Source

Built Distributions

cmarkgfm-2022.10.26-cp310-cp310-win_amd64.whl (125.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

cmarkgfm-2022.10.26-cp310-cp310-win32.whl (114.7 kB view hashes)

Uploaded CPython 3.10 Windows x86

cmarkgfm-2022.10.26-cp310-cp310-musllinux_1_1_x86_64.whl (426.9 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

cmarkgfm-2022.10.26-cp310-cp310-musllinux_1_1_i686.whl (399.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

cmarkgfm-2022.10.26-cp310-cp310-musllinux_1_1_aarch64.whl (417.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

cmarkgfm-2022.10.26-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (434.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

cmarkgfm-2022.10.26-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (429.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

cmarkgfm-2022.10.26-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (407.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cmarkgfm-2022.10.26-cp310-cp310-macosx_11_0_arm64.whl (120.9 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

cmarkgfm-2022.10.26-cp310-cp310-macosx_10_9_x86_64.whl (123.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

cmarkgfm-2022.10.26-cp39-cp39-win_amd64.whl (125.7 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

cmarkgfm-2022.10.26-cp39-cp39-win32.whl (114.7 kB view hashes)

Uploaded CPython 3.9 Windows x86

cmarkgfm-2022.10.26-cp39-cp39-musllinux_1_1_x86_64.whl (427.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

cmarkgfm-2022.10.26-cp39-cp39-musllinux_1_1_i686.whl (399.6 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

cmarkgfm-2022.10.26-cp39-cp39-musllinux_1_1_aarch64.whl (417.2 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

cmarkgfm-2022.10.26-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (434.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cmarkgfm-2022.10.26-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (429.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

cmarkgfm-2022.10.26-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (407.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cmarkgfm-2022.10.26-cp39-cp39-macosx_11_0_arm64.whl (120.9 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

cmarkgfm-2022.10.26-cp39-cp39-macosx_10_9_x86_64.whl (123.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

cmarkgfm-2022.10.26-cp38-cp38-win_amd64.whl (125.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

cmarkgfm-2022.10.26-cp38-cp38-win32.whl (114.7 kB view hashes)

Uploaded CPython 3.8 Windows x86

cmarkgfm-2022.10.26-cp38-cp38-musllinux_1_1_x86_64.whl (427.4 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

cmarkgfm-2022.10.26-cp38-cp38-musllinux_1_1_i686.whl (400.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

cmarkgfm-2022.10.26-cp38-cp38-musllinux_1_1_aarch64.whl (417.7 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

cmarkgfm-2022.10.26-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (434.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cmarkgfm-2022.10.26-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (429.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

cmarkgfm-2022.10.26-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (407.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cmarkgfm-2022.10.26-cp38-cp38-macosx_11_0_arm64.whl (120.9 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

cmarkgfm-2022.10.26-cp38-cp38-macosx_10_9_x86_64.whl (123.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

cmarkgfm-2022.10.26-cp37-cp37m-win_amd64.whl (125.7 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

cmarkgfm-2022.10.26-cp37-cp37m-win32.whl (114.7 kB view hashes)

Uploaded CPython 3.7m Windows x86

cmarkgfm-2022.10.26-cp37-cp37m-musllinux_1_1_x86_64.whl (426.7 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

cmarkgfm-2022.10.26-cp37-cp37m-musllinux_1_1_i686.whl (399.4 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

cmarkgfm-2022.10.26-cp37-cp37m-musllinux_1_1_aarch64.whl (416.9 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ ARM64

cmarkgfm-2022.10.26-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (433.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

cmarkgfm-2022.10.26-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (429.1 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

cmarkgfm-2022.10.26-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (406.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cmarkgfm-2022.10.26-cp37-cp37m-macosx_10_9_x86_64.whl (123.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

cmarkgfm-2022.10.26-cp36-cp36m-win_amd64.whl (136.6 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

cmarkgfm-2022.10.26-cp36-cp36m-win32.whl (121.9 kB view hashes)

Uploaded CPython 3.6m Windows x86

cmarkgfm-2022.10.26-cp36-cp36m-musllinux_1_1_x86_64.whl (426.4 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

cmarkgfm-2022.10.26-cp36-cp36m-musllinux_1_1_i686.whl (399.4 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

cmarkgfm-2022.10.26-cp36-cp36m-musllinux_1_1_aarch64.whl (417.0 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ ARM64

cmarkgfm-2022.10.26-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (433.7 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

cmarkgfm-2022.10.26-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (428.5 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

cmarkgfm-2022.10.26-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (406.1 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cmarkgfm-2022.10.26-cp36-cp36m-macosx_10_9_x86_64.whl (123.9 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

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