skip to navigation
skip to content

Not Logged In

mistune 0.6

The fastest markdown parser in pure Python

The fastest markdown parser in pure Python, inspired by marked.


  • 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.


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.


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' % \
        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)
header(text, level, raw=None)
list(body, ordered=True)
table(header, body)
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)
image(src, title, alt_text)
link(link, title, content)


Here is a list of all options that will affect the rendering results:

renderer = mistune.Renderer(escape=True)
md = mistune.Markdown(renderer=renderer)
  • 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.

When using the default renderer, you can use one of the following shorthands:

mistune.markdown(text, escape=True)

md = mistune.Markdown(escape=True)


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 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 =
        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.


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.

File Type Py Version Uploaded on Size
mistune-0.6-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 (md5) Python Wheel cp27 2015-06-17 388KB
mistune-0.6-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 (md5) Python Wheel cp33 2015-06-17 397KB
mistune-0.6-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 (md5) Python Wheel cp34 2015-06-17 399KB
mistune-0.6-py2.py3-none-any.whl (md5) Python Wheel py2.py3 2015-06-17 14KB
mistune-0.6.tar.gz (md5) Source 2015-06-17 45KB
  • Downloads (All Versions):
  • 1911 downloads in the last day
  • 12863 downloads in the last week
  • 49482 downloads in the last month