Python bindings for MD4C
Project description
PyMD4C
Python bindings for the very fast MD4C Markdown parsing and rendering library.
- GitHub: https://github.com/dominickpastore/pymd4c
- PyPI: https://pypi.org/project/pymd4c/
- Changelog: https://github.com/dominickpastore/pymd4c/blob/master/CHANGELOG.md
- Issues: https://github.com/dominickpastore/pymd4c/issues
Introduction
The MD4C C library provides a SAX-like parser that uses callbacks to return the various blocks, inlines, and text it parses from the Markdown input. In addition, it provides an HTML renderer that wraps the generic parser to provide HTML output directly.
Accordingly, this Python module provides two classes:
md4c.GenericParser
- Wraps the generic SAX-like MD4C parser. Requires Python functions (or other callables) as callbacks.md4c.HTMLRenderer
- Wraps the HTML renderer. Produces HTML output directly.
If other renderers are added to MD4C, they will get their own Python class as
well, similar to the HTMLRenderer
.
Install from PyPI
PyMD4C is available on PyPI under the name pymd4c
. Install it with
pip like this:
pip install pymd4c
This is the recommended method to obtain PyMD4C. It should work well on most Linux distributions. It will probably work well on Windows and macOS (but these platforms are not as well-tested).
If this does not work, there are a couple potential reasons:
-
You do not have pip installed, or your version is too old. See Installing Packages - Python Packaging User Guide.
-
Your version of Python is too old. This is a platform wheel, so it is built for each Python version separately. Python versions older than 3.6 are not supported. If your Python version is older than that, try upgrading.
-
Your platform is incompatible. Again, since it is a platform wheel, it is built for each supported platform separately.
- If you are running Windows, you may be running 32-bit (x86) Python. Currently, only packages for 64-bit (x86-64) Python are built. If you can, try running 64-bit Python.
- If you are on macOS, your macOS version might be too old.
- If you are on Linux, you may be running on an architecture other than x86-64, a distribution that is too old, or a more esoteric distribution unsupported by manylinux2014. (Note that many architectures supported by manylinux2014 are not built at this time, including x86, arm64, ppc64le, and s390x.)
- If you are on some other platform, unfortunately, it is not supported by the pre-built packages.
If a build is not available for your platform (or you simply want to), you can build and install from source. The instructions below should assist.
If a build is not available or not working for your platform and you think it should be, consider opening a GitHub issue.
Build and Install from Source
Prerequisites
This package depends on the MD4C library. It may be available through your package manager. Otherwise, it can be built from source as follows:
- Make sure you have CMake and a C compiler installed.
- Download and extract the matching release from the MD4C releases page (e.g. for PyMD4C version W.X.Y.Z, download MD4C version W.X.Y).
- On Unix-like systems (including macOS):
-
Inside the extracted file, run the following:
mkdir build cd build cmake .. make # Do as root: make install
The install step must be run as root. The library will install to /usr/local by default.
-
You may need to rebuild the ldconfig cache (also as root):
ldconfig
-
- On Windows:
-
Inside the extracted file, run the following:
mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release .. cmake --build . --config Release cmake --install .
-
In addition, on Unix-like systems (including macOS), the pkg-config
tool must
be available to build PyMD4C. After PyMD4C is built, it is no longer required
(that is, it is not a prerequisite for actually using PyMD4C). This tool is
likely available on your system already, so this should not be an issue in most
cases.
Finally, note that since this package uses C extensions, development headers
for Python must be installed for the build to succeed. If you are using Linux,
some distributions split these off from the main Python package. Install
python-dev
or python-devel
to get them.
Build/Install
Build and install as you would for any Python source repository. Download and extract a release or clone the repository, and run the following inside:
pip install .
Alternatively, you can have pip fetch and build from the latest source distribution on PyPI:
pip install --no-binary pymd4c pymd4c
Note that on Windows, setup.py assumes the MD4C library was installed at "C:/Program Files (x86)/MD4C/" (this is the default location when building MD4C from source, as described above). If this is not the case, installation will fail.
Class GenericParser
import md4c
generic_parser = md4c.GenericParser(parser_flags)
Initialize a new GenericParser
. Parameters:
parser_flags
- Anint
made up of some combination of the parser option flags or'd together, e.g.md4c.MD_FLAG_TABLES | md4c.MD_FLAG_STRIKETHROUGH
. For the default options, use0
, which will parse according to the base CommonMark specification. See the "Module-Wide Constants" section below for a full list of parser option flags.
Note: If the end goal of parsing is to produce HTML, strongly consider
using an HTMLRenderer
instead. All rendering will be performed by native C
code, which will be much faster.
Parse Method
import md4c
generic_parser = md4c.GenericParser(...)
generic_parser.parse(input,
enter_block_callback,
leave_block_callback,
enter_span_callback,
leave_span_callback,
text_callback)
Parse markdown text using the provided callbacks. Parameters:
input
- Astr
orbytes
containing the Markdown document to parse. If abytes
, it must be UTF-8 encoded.enter_block_callback
- A function (or other callable) to be called whenever the parser enters a new block element in the Markdown source.leave_block_callback
- A function (or other callable) to be called whenever the parser leaves a block element in the Markdown source.enter_span_callback
- A function (or other callable) to be called whenever the parser enters a new inline element in the Markdown source.leave_span_callback
- A function (or other callable) to be called whenever the parser leaves an inline element in the Markdown source.text_callback
- A function (or other callable) to be called whenever the parser has text to add to the current block or inline element.
The parse()
method will raise md4c.ParseError
in the event of a problem
during parsing, such as running out of memory. This does not signal invalid
syntax, as there is no such thing in Markdown. It can also emit any exception
raised by any of the callbacks (except md4c.StopParsing
, which is caught and
handled quietly).
Callback Details
enter_block_callback
, leave_block_callback
, enter_span_callback
, and
leave_span_callback
all must accept two parameters:
-
type
- Anmd4c.BlockType
ormd4c.SpanType
representing the type of block or span. See the "Enums" section for more info. -
details
- Adict
that contains extra information for certain types of blocks and spans, for example, the level of a heading. Keys arestr
s. Values areint
s, single-characterstr
s, or (forMD_ATTRIBUTE
) lists of tuples or None.This dict will contain the information provided by
MD_BLOCK_*_DETAIL
andMD_SPAN_*_DETAIL
structs in MD4C'smd4c.h
, with the following exceptions:- Non-task
MD_BLOCK_LI
blocks (whenis_task
isFalse
) do not includetask_mark
andtask_mark_offset
keys. - Indented
MD_BLOCK_CODE
blocks do not include thefence_char
key.
Regarding
MD_ATTRIBUTE
s: These are used where a block or span can contain some associated text, such as link titles and code block language references. Such attributes may contain multiple text sub-elements (e.g. some regular text, an HTML entity, and then some more regular text). Thus, anMD_ATTRIBUTE
value indetails
consists of a list of 2-tuples:(text_type, text)
wheretext_type
is anmd4c.TextType
(see "Enums" below) andtext
is the actual text as astr
.Note that attributes may not always be provided. For example, an indented code block will not contain
info
orlang
attributes. In those cases, the value will beNone
. - Non-task
text_callback
must also accept two parameters, but they are different:
type
- Anmd4c.TextType
representing the type of text element. See the "Enums" section for more info.text
- The actual text, as astr
.
Callbacks need not return anything specific; their return values are ignored.
To cancel parsing, callbacks can raise md4c.StopParsing
. This will be caught
by the parse()
method and immediately halt parsing quietly. All other
exceptions raised by callbacks will abort parsing and will be propagated back
to the caller of parse()
.
Class HTMLRenderer
import md4c
html_renderer = md4c.HTMLRenderer(parser_flags, renderer_flags)
Initialize a new HTMLRenderer
. Parameters:
parser_flags
- Anint
made up of some combination of the parser option flags or'd together, e.g.md4c.MD_FLAG_TABLES | md4c.MD_FLAG_STRIKETHROUGH
. For the default options, use0
, which will parse according to the base CommonMark standard. See the "Module-Wide Constants" section below for a full list of parser option flags.renderer_flags
- Anint
made up of some combination of the HTML renderer option flags or'd together. These are also listed in the "Module-Wide Constants" section below.
Parse Method
import md4c
html_renderer = md4c.HTMLRenderer(...)
html_renderer.parse(input)
Parse markdown text and return a str
with rendered HTML. Parameters:
input
- Astr
orbytes
containing the Markdown document to parse. If abytes
, it must be UTF-8 encoded.
This method will raise md4c.ParseError
in the event of a problem during
parsing, such as running out of memory. This does not signal invalid syntax, as
there is no such thing in Markdown.
Module-Wide Constants
The MD4C library provides various option flags for parsers and renderers as named constants. These are made available as module-level constants in PyMD4C. Also, each constant has a correspondent optional keyword argument that can be used instanciating parser and renderers.
Parser Option Flags
Basic option flags:
md4c flag | keyword argument | description |
---|---|---|
md4c.MD_FLAG_COLLAPSEWHITESPACE |
collapse_whitespace |
In normal text, collapse non-trivial whitespace into a single space. |
md4c.MD_FLAG_COLLAPSEWHITESPACE |
permissive_atx_headers |
Do not requite a space in ATX headers (e.g. ###Header ). |
md4c.MD_FLAG_PERMISSIVEURLAUTOLINKS |
permissive_url_autolinks |
Convert URLs to links even without < and > . |
md4c.MD_FLAG_PERMISSIVEEMAILAUTOLINKS |
permissive_email_autolinks |
Convert email addresses to links even without < , > , and mailto: . |
md4c.MD_FLAG_NOINDENTEDCODEBLOCKS |
no_indented_code_blocks |
Disable indented code blocks (only allow fenced code blocks). |
md4c.MD_FLAG_NOHTMLBLOCKS |
no_html_blocks |
Disable raw HTML blocks. |
md4c.MD_FLAG_NOHTMLSPANS |
no_html_spans |
Disable raw HTML inlines. |
md4c.MD_FLAG_TABLES |
tables |
Enable tables extension. |
md4c.MD_FLAG_STRIKETHROUGH |
strikethrough |
Enable strikethrough extension. |
md4c.MD_FLAG_PERMISSIVEWWWAUTOLINKS |
permissive_www_autolinks |
Enable www autolinks (even without any scheme prefix, as long as they begin with www. ). |
md4c.MD_FLAG_TASKLISTS |
tasklists |
Enable task lists extension. |
md4c.MD_FLAG_LATEXMATHSPANS |
latex_math_spans |
Enable $ and $$ containing LaTeX equations. |
md4c.MD_FLAG_WIKILINKS |
wikilinks |
Enable wiki links extension. |
md4c.MD_FLAG_UNDERLINE |
underline |
Enable underline extension (and disable _ for regular emphasis). |
Combination option flags:
md4c flag | keyword argument | description |
---|---|---|
md4c.MD_FLAG_PERMISSIVEAUTOLINKS |
permissive_auto_links |
Enables all varieties of autolinks: MD_FLAG_PERMISSIVEURLAUTOLINKS , MD_FLAG_PERMISSIVEEMAILAUTOLINKS , and MD_FLAG_PERMISSIVEWWWAUTOLINKS . |
md4c.MD_FLAG_NOHTML |
no_html |
Disables all raw HTML tags: MD_FLAG_NOHTMLBLOCKS and MD_FLAG_NOHTMLSPANS . |
Dialect option flags (note that not all features of a dialect may be supported, but these flags will cause MD4C to parse as many features of the dialect as it supports):
md4c flag | keyword argument | description |
---|---|---|
md4c.MD_DIALECT_GITHUB |
dialect_github |
Parse GitHub-Flavored Markdown, which enables the following flags:
|
HTML Renderer Option Flags
md4c flag | keyword argument | description |
---|---|---|
md4c.MD_HTML_FLAG_DEBUG |
debug |
For development use, send MD4C debug output to stderr. |
md4c.MD_HTML_FLAG_VERBATIM_ENTITIES |
verbatim_entities |
Do not replace HTML entities with the actual character (e.g. © with ©). |
md4c.MD_HTML_FLAG_SKIP_UTF8_BOM |
skip_utf8_bom |
Omit BOM from start of UTF-8 input. |
md4c.MD_HTML_FLAG_XHTML |
xhtml |
Generate XHTML instead of HTML. |
Enums
The MD4C library uses various enums to provide data to callbacks. PyMD4C uses
IntEnum
s to encapsulate these.
See md4c.h
from the MD4C project for more information on these enums
and associated types.
Block Types - class BlockType
md4c.BlockType.DOC
- Documentmd4c.BlockType.QUOTE
- Block quotemd4c.BlockType.UL
- Unordered listmd4c.BlockType.OL
- Ordered listmd4c.BlockType.LI
- List itemmd4c.BlockType.HR
- Horizontal rulemd4c.BlockType.H
- Headingmd4c.BlockType.CODE
- Code blockmd4c.BlockType.HTML
- Raw HTML blockmd4c.BlockType.P
- Paragraphmd4c.BlockType.TABLE
- Tablemd4c.BlockType.THEAD
- Table header rowmd4c.BlockType.TBODY
- Table bodymd4c.BlockType.TR
- Table rowmd4c.BlockType.TH
- Table header cellmd4c.BlockType.TD
- Table cell
Span Types - class SpanType
md4c.SpanType.EM
- Emphasismd4c.SpanType.STRONG
- Strongmd4c.SpanType.A
- Linkmd4c.SpanType.IMG
- Imagemd4c.SpanType.CODE
- Inline codemd4c.SpanType.DEL
- Strikethroughmd4c.SpanType.LATEXMATH
- Inline mathmd4c.SpanType.LATEXMATH_DISPLAY
- Display mathmd4c.SpanType.WIKILINK
- Wiki linkmd4c.SpanType.U
- Underline
Text Types - class TextType
md4c.TextType.NORMAL
- Normal textmd4c.TextType.NULLCHAR
- NULL charactermd4c.TextType.BR
- Line breakmd4c.TextType.SOFTBR
- Soft line breakmd4c.TextType.ENTITY
- HTML Entitymd4c.TextType.CODE
- Text inside a code block or inline codemd4c.TextType.HTML
- Raw HTML (inside an HTML block or simply inline HTML)md4c.TextType.LATEXMATH
- Text inside an equation
Table Alignments - class Align
md4c.Align.DEFAULT
md4c.Align.LEFT
md4c.Align.CENTER
md4c.Align.RIGHT
Exceptions
-
md4c.ParseError
- Raised by one of theparse()
methods when there is an error during parsing, such as running out of memory. There is no such thing as invalid syntax in Markdown, so this really only signals some sort of system error. -
md4c.StopParsing
- A callback can raise this to stop parsing early.GenericParser
'sparse()
method will catch it and abort quietly.
License
This project is licensed under the MIT license. See the LICENSE.md
file for
details.
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 Distributions
Hashes for pymd4c-0.4.6.0b1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e53b0df52aeaca1791cbda2fff1a23bc9af3972c68116ff1de97efc96a185029 |
|
MD5 | 4a5f5cfb7272a50a29452424743ab044 |
|
BLAKE2b-256 | 6912b5b9fa11ec4f7319b6e31691c75ee26170d2b0bfb14eb85b107f71323ec8 |
Hashes for pymd4c-0.4.6.0b1-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8f2c0448dccfabe2206ce1467f62810bd23a820fd59ba7a383c2bc1856ec191 |
|
MD5 | b8163bcf4f65bb1adf0a2bb306cbefd9 |
|
BLAKE2b-256 | 1db4be12e0a53af6e8e0e83f17634cfab994dad515c10ef5b64e1d7b256c29c4 |
Hashes for pymd4c-0.4.6.0b1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bc54ad6b39ba0d1e8b241960ee1781798557ce9121ade863d7d71cad47acfc1 |
|
MD5 | 071b0dfee7eb3ec6cdecfe719b67147c |
|
BLAKE2b-256 | c291f48370cd9e26e48d1cc158dc6aadcd017240e98a80c4fbadc47ff035f11e |
Hashes for pymd4c-0.4.6.0b1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8f2cc9fe85a298864016b4bdc50a2790803377520fe150b9bff91588430438d |
|
MD5 | 7b45467f3ef53b4bf91135a9bf2fc5ce |
|
BLAKE2b-256 | a748a68431b37ecda792504c35ece73968ea460d9a8e26154fcbd02bec5f82c4 |
Hashes for pymd4c-0.4.6.0b1-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed8e727bffd18659035f47a7bcb22a789e6dfb8ccc9ef2a9e9bdcba6d899fc09 |
|
MD5 | ff682e7b66d546cc50417c9fb1ab9abc |
|
BLAKE2b-256 | 017cd6d25694d3bfa24936f9d7e19beb52666e176c4fe105fa2a51776da6286d |
Hashes for pymd4c-0.4.6.0b1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 745fe2ef22aca86d3694520e5a4e6a63cf79232a7cbc81b0d052d3ff3fb6c3af |
|
MD5 | ee58572ef906a92adaebee79b558b0b6 |
|
BLAKE2b-256 | 26b69737ee04169aa6fca399382fe3cb1d17ba226a38ac2df20a843f7b214d1f |
Hashes for pymd4c-0.4.6.0b1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b440d431b6289ef439ce4bbef064290861e022f7d93ab0f206b5d33e41b50919 |
|
MD5 | 83510d02cbac1e1cf0e8d9e392af2ace |
|
BLAKE2b-256 | 59e39d9a1598f386bba33790ffc734f5648a2525b12611d467215af3d24b0292 |
Hashes for pymd4c-0.4.6.0b1-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 141a85e3f8178c9b7fd9234bae8112b8aef533499703197ba76dabc34dd71952 |
|
MD5 | 4280970664abd91150088717cac4352c |
|
BLAKE2b-256 | 8fd2123e0db0188fe980556c489dcab912d503d92ce53a6c180c7a626c6aaed0 |
Hashes for pymd4c-0.4.6.0b1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2835b8700a47d0a56db3090e13960c87819982101a3c80394108f2a819a2011d |
|
MD5 | bc15384bedde6bd367710c7b4bed98b4 |
|
BLAKE2b-256 | a262debbf9e64b28ed1f89b0d0d22fd29088cf4aa2546c6715bb8ef1bae93cc5 |
Hashes for pymd4c-0.4.6.0b1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bca1ec9ce129eefd702c3f8fa4bc6bc7dd9814b0ffd24dedb237b641a12316d |
|
MD5 | 4947754fe2769ed6af48874fda80754a |
|
BLAKE2b-256 | e97f0c739ada81d1823e05f062761be0f0b114828abcaab7783cbfcf941003f2 |
Hashes for pymd4c-0.4.6.0b1-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c0138392c0a3b0b66c9f0c47a0abf74c1fbd223b5231112c1843fa4fb0002d8 |
|
MD5 | 2a685a76d536d4386b06faa97d339f20 |
|
BLAKE2b-256 | 203330b8dfb2ba6ceaa5ec19864a6d7cf3c0cb091ea15ee7b3b68e066dfa93fa |
Hashes for pymd4c-0.4.6.0b1-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6f7553b1b562e4cb5fe1ff854af034a91cd44fc9db398ee7f5edd08777cfa74 |
|
MD5 | e1da08701356a07dfb1340e1039ba008 |
|
BLAKE2b-256 | 8aa35802caf23e11d1785930b445cf353a12c5e85581974c94ea9bdfce2392d6 |