The fastest markdown parser in pure Python
Project description
The fastest markdown parser in pure Python, 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
If pip is not available, try easy_install:
$ easy_install mistune
Cython Feature
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 **markdown**') # output: <p>I am using <strong>markdown</strong></p>
Mistune has all features by default. You don’t have to configure anything.
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 MyRenderer(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 = MyRenderer() md = mistune.Markdown(renderer=renderer) print(md.render('Some Markdown text.'))
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) tag(html) strikethrough(text) text(text)
Options
Here is a list of all options that will affect the rendering results:
mistune.markdown(text, escape=True) md = mistune.Markdown(escape=True) md.render(text)
escape: if set to True, all raw html tags will 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_html: parse text in block level html.
Lexers
Sometimes you want to add your own rules to Markdown, such as GitHub Wiki links. You can’t archive 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 MyRenderer(Renderer): def wiki_link(self, alt, link): return '<a href="%s">%s</a>' % (link, alt) class MyInlineGrammar(InlineGrammar): # it would take a while for creating the right regex wiki_link = re.compile( r'\[\[' # [[ r'([\s\S]+?\|[\s\S]+?)' # Page 2|Page 2 r'\]\](?!\])' # ]] ) class MyInlineLexer(InlineLexer): default_rules = copy.copy(InlineLexer.default_rules) # Add wiki_link parser to default rules # you can insert it any place you like default_rules.insert(3, 'wiki_link') def __init__(self, renderer, rules=None, **kwargs): if rules is None: # use the inline grammar rules = MyInlineGrammar() super(MyInlineLexer, self).__init__(renderer, rules, **kwargs) 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 = MyRenderer() inline = MyInlineLexer(renderer) 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
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.
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.5.tar.gz
.
File metadata
- Download URL: mistune-0.5.tar.gz
- Upload date:
- Size: 184.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d53d868cfd10cf757160e88adb5760fce95f7026a243f15a02b7c604238e5869 |
|
MD5 | 36fefdd9b2466cac646b11aab2c13c74 |
|
BLAKE2b-256 | 7f54056588bc6885df533dabb3bb7e65d082a4de6dda2bee408278112809c0ec |
File details
Details for the file mistune-0.5-cp34-cp34m-macosx_10_6_x86_64.whl
.
File metadata
- Download URL: mistune-0.5-cp34-cp34m-macosx_10_6_x86_64.whl
- Upload date:
- Size: 378.9 kB
- Tags: CPython 3.4m, macOS 10.6+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30e216580712adb33ddbbfbd0e95554cc04536c94157b649b9d7e2cf04d5e9b2 |
|
MD5 | 12e9e74d653a642da4eba12b8d644aa2 |
|
BLAKE2b-256 | d73ca1fdc01c052572e4d049fe98a6a1579197f23f9e5f5bf0facca147828b84 |
File details
Details for the file mistune-0.5-cp33-cp33m-macosx_10_6_x86_64.whl
.
File metadata
- Download URL: mistune-0.5-cp33-cp33m-macosx_10_6_x86_64.whl
- Upload date:
- Size: 379.0 kB
- Tags: CPython 3.3m, macOS 10.6+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0635f992678d6fbb0be47d5d42e4f7f4e2a483e2a3e3412bc4696634abc33176 |
|
MD5 | a9dedd4239840511059e15d4a96f4425 |
|
BLAKE2b-256 | 0c3482f7f7bc7ccc941e1157093c0405fb6935aeb721f1c82fd0572a030f0776 |
File details
Details for the file mistune-0.5-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
.
File metadata
- Download URL: mistune-0.5-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
- Upload date:
- Size: 401.1 kB
- Tags: CPython 2.7, macOS 10.6+ intel, macOS 10.9+ intel, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5a581972e18b69cb8a3b294ac2ca2974578c2b072fdd3e3fe145851ededefa4 |
|
MD5 | 46878d65a8c99f610aa76d3e0481f84b |
|
BLAKE2b-256 | ab33202a84d059fe9f0a668ae41582cd91fd3de4d6e66c4d164f3f309932cb68 |
File details
Details for the file mistune-0.5-cp26-none-macosx_10_6_x86_64.whl
.
File metadata
- Download URL: mistune-0.5-cp26-none-macosx_10_6_x86_64.whl
- Upload date:
- Size: 401.1 kB
- Tags: CPython 2.6, macOS 10.6+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9d5e976ede213bce7893b7e33d135db7516d89ddaec377dca80e0e0d4632035 |
|
MD5 | 518cfb8cbae8c26a4f28c1fd4b2d809c |
|
BLAKE2b-256 | 565b57c4afe2a3071158bf02c78bd7bf2b9db1c8b4472ac2be7287ba79a465b5 |