The fastest markdown parser in pure Python
Project description
The fastest markdown parser in pure Python with renderer features, inspired by marked.
Features
Pure Python. Tested in Python 2.6+, Python 3.3+ and PyPy.
Very Fast. It is the fastest in all pure Python markdown parsers.
More Features. Table, footnotes, autolink, fenced code etc.
View the benchmark results.
Installation
Installing mistune with pip:
$ pip install mistune
Mistune can be faster, if you compile with cython:
$ pip install cython mistune
Basic Usage
A simple API that render a markdown formatted text:
import mistune
mistune.markdown('I am using **mistune markdown parser**')
# output: <p>I am using <strong>mistune markdown parser</strong></p>
If you care about performance, it is better to re-use the Markdown instance:
import mistune
markdown = mistune.Markdown()
markdown('I am using **mistune markdown parser**')
Mistune has enabled all features by default. You don’t have to configure anything. But there are options for you to change the parser behaviors.
Options
Here is a list of all options that will affect the rendering results, configure them with mistune.Renderer:
renderer = mistune.Renderer(escape=True, hard_wrap=True)
# use this renderer instance
markdown = mistune.Markdown(renderer=renderer)
markdown(text)
escape: if set to False, all raw html tags will not be escaped.
hard_wrap: if set to True, it will has GFM line breaks feature.
use_xhtml: if set to True, all tags will be in xhtml, for example: <hr />.
parse_block_html: parse text only in block level html.
parse_inline_html: parse text only in inline level html.
When using the default renderer, you can use one of the following shortcuts:
mistune.markdown(text, escape=True, hard_wrap=True) markdown = mistune.Markdown(escape=True, hard_wrap=True) markdown(text)
Renderer
Like misaka/sundown, you can influence the rendering by custom renderers. All you need to do is subclassing a Renderer class.
Here is an example of code highlighting:
import mistune
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
class HighlightRenderer(mistune.Renderer):
def block_code(self, code, lang):
if not lang:
return '\n<pre><code>%s</code></pre>\n' % \
mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter()
return highlight(code, lexer, formatter)
renderer = HighlightRenderer()
markdown = mistune.Markdown(renderer=renderer)
print(markdown('```python\nassert 1 == 1\n```'))
Find more renderers in mistune-contrib.
Block Level
Here is a list of block level renderer API:
block_code(code, language=None) block_quote(text) block_html(html) header(text, level, raw=None) hrule() list(body, ordered=True) list_item(text) paragraph(text) table(header, body) table_row(content) table_cell(content, **flags)
The flags tells you whether it is header with flags['header']. And it also tells you the align with flags['align'].
Span Level
Here is a list of span level renderer API:
autolink(link, is_email=False) codespan(text) double_emphasis(text) emphasis(text) image(src, title, alt_text) linebreak() newline() link(link, title, content) strikethrough(text) text(text) inline_html(text)
Footnotes
Here is a list of renderers related to footnotes:
footnote_ref(key, index) footnote_item(key, text) footnotes(text)
Lexers
Sometimes you want to add your own rules to Markdown, such as GitHub Wiki links. You can’t achieve this goal with renderers. You will need to deal with the lexers, it would be a little difficult for the first time.
We will take an example for GitHub Wiki links: [[Page 2|Page 2]]. It is an inline grammar, which requires custom InlineGrammar and InlineLexer:
import copy
from mistune import Renderer, InlineGrammar, InlineLexer
class WikiLinkRenderer(Renderer):
def wiki_link(self, alt, link):
return '<a href="%s">%s</a>' % (link, alt)
class WikiLinkInlineLexer(InlineLexer):
def enable_wiki_link(self):
# add wiki_link rules
self.rules.wiki_link = re.compile(
r'\[\[' # [[
r'([\s\S]+?\|[\s\S]+?)' # Page 2|Page 2
r'\]\](?!\])' # ]]
)
# Add wiki_link parser to default rules
# you can insert it some place you like
# but place matters, maybe 3 is not good
self.default_rules.insert(3, 'wiki_link')
def output_wiki_link(self, m):
text = m.group(1)
alt, link = text.split('|')
# you can create an custom render
# you can also return the html if you like
return self.renderer.wiki_link(alt, link)
You should pass the inline lexer to Markdown parser:
renderer = WikiLinkRenderer()
inline = WikiLinkInlineLexer(renderer)
# enable the feature
inline.enable_wiki_link()
markdown = Markdown(renderer, inline=inline)
markdown('[[Link Text|Wiki Link]]')
It is the same with block level lexer. It would take a while to understand the whole mechanism. But you won’t do the trick a lot.
Contribution & Extensions
Mistune itself doesn’t accept any extension. It will always be a simple one file script.
If you want to add features, you can head over to mistune-contrib.
Here are some extensions already in mistune-contrib:
Math/MathJax features
Highlight Code Renderer
TOC table of content features
MultiMarkdown Metadata parser
Get inspired with the contrib repository.
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
File details
Details for the file mistune-0.7.tar.gz
.
File metadata
- Download URL: mistune-0.7.tar.gz
- Upload date:
- Size: 48.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1daa2e55f5de63ecde7c446c4677c0447006752f78ad2c9c1c3c3452d395f89f |
|
MD5 | 16949bb59e8a74672b0bb24c52fb94cc |
|
BLAKE2b-256 | 42b72847eccf4e93d8962de4763d6d08dd6b6d41b8f85416f6194a7f50f3b45a |
File details
Details for the file mistune-0.7-py2.py3-none-any.whl
.
File metadata
- Download URL: mistune-0.7-py2.py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d592f8687a1262a1e5ff3fe4ae7da1800e613889f10ee4192ce38dab758a61b |
|
MD5 | 2097b84066fb0d01f4c465be438f710e |
|
BLAKE2b-256 | aab72fee307defc5d8318973ede16144f37f6e75a4925fed6e95dbd6137dbe56 |
File details
Details for the file mistune-0.7-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
.
File metadata
- Download URL: mistune-0.7-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
- Upload date:
- Size: 422.6 kB
- Tags: CPython 3.4m, macOS 10.10+ intel, macOS 10.10+ x86-64, macOS 10.6+ intel, macOS 10.9+ intel, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d09ac3dd83258045a2a32c1695ce25dd0f008c03587fabf8bb0e5cf958465ca8 |
|
MD5 | 1335608bbfab49c77ac00aa8ba809f27 |
|
BLAKE2b-256 | 4608a62d246503d13ad74711d9b5ec7048990d8eedaf4acda7d3dbd3dd340f60 |
File details
Details for the file mistune-0.7-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
.
File metadata
- Download URL: mistune-0.7-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
- Upload date:
- Size: 421.5 kB
- Tags: CPython 3.3m, macOS 10.10+ intel, macOS 10.10+ x86-64, macOS 10.6+ intel, macOS 10.9+ intel, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbc799f38ddc8a7d9f7c012ed976103c748c8368af30993782a2d03f6e947619 |
|
MD5 | f7d86494293cbc1f73d0cf4e7bbb4cfa |
|
BLAKE2b-256 | e3989becaf906a4cc447990d35499474ca50c49246139115b3b2043dac83103c |
File details
Details for the file mistune-0.7-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
.
File metadata
- Download URL: mistune-0.7-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
- Upload date:
- Size: 412.0 kB
- Tags: CPython 2.7, macOS 10.10+ intel, macOS 10.10+ x86-64, macOS 10.6+ intel, macOS 10.9+ intel, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6397007ebd659c75007424189e33b2c846ae56a9e159d8ba11685a4dafba4c0 |
|
MD5 | 3fc557ee166bd1ad60340ede39943ad7 |
|
BLAKE2b-256 | 5e7765a7f8d22e0090f08aeb875f884aa2607ba407f79dc3c9d19ee86c9b38a7 |