Skip to main content

Mistune

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

https://travis-ci.org/lepture/mistune.png?branch=master https://ci.appveyor.com/api/projects/status/8ai8tfwp75oela17 https://coveralls.io/repos/lepture/mistune/badge.png?branch=master

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()
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

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_features = copy.copy(InlineLexer.default_features)

    # Add wiki_link parser to default features
    # you can insert it any place you like
    default_features.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:

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.

Metadata

Release files for mistune 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mistune 0.4.1
File Size Uploaded
mistune-0.4.1.tar.gz 145.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for mistune 0.4.1
File
mistune-0.4.1-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl CPython 3.4 CPython 3.4 pymalloc macOS 10.9+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386), macOS 10.9+ x86-64 Details
mistune-0.4.1-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl CPython 3.3 CPython 3.3 pymalloc macOS 10.9+ x86-64, macOS 10.9+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386) Details
mistune-0.4.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl CPython 2.7 none macOS 10.9+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386), macOS 10.9+ x86-64 Details
mistune-0.4.1-cp26-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl CPython 2.6 none macOS 10.9+ x86-64, macOS 10.9+ Intel (x86-64, i386), macOS 10.6+ Intel (x86-64, i386) Details

Total release size: 1.7 MB

Release files / mistune-0.4.1.tar.gz

Download URL mistune-0.4.1.tar.gz
Size 145.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a1943a64445676bbde20432a95219d30da650b377c99fa427dee35d2869305fc
BLAKE2b-256 checksum
How to use checksums
dc738b3a73641b7a4112ec1f8613e92905542dd155f88192f1d6508d7ad6e868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / mistune-0.4.1-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl

Download URL mistune-0.4.1-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Size 388.2 kB
Tags CPython 3.4 CPython 3.4 pymalloc macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7fdccb526d7127fd873cf59b0a017ece8c4a1315161710bee2e03f229c692a11
BLAKE2b-256 checksum
How to use checksums
0dd392c50ffc8390b9865053458026da2c44a11ba613b3a94213ef31ab7d191a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / mistune-0.4.1-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl

Download URL mistune-0.4.1-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Size 388.6 kB
Tags CPython 3.3 CPython 3.3 pymalloc macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
4dbf5f5979c2caacb7dce5f1f7e2b1d231bf8c27a7fba76414b91c63d1f1a59c
BLAKE2b-256 checksum
How to use checksums
95f527ab08cc18cdc3460493ad8b6e7efdbf9201b156d71b74f065745cdd3d1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / mistune-0.4.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl

Download URL mistune-0.4.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Size 387.6 kB
Tags CPython 2.7 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
dc53437c7562c9c674322ec86bdea34e1275a34cd0e0675fa247e17775947c51
BLAKE2b-256 checksum
How to use checksums
28b7fc6e66ca4b9fe8cf3bc5d1b83dcfc3036406095e5ce1348ec73916b49708
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / mistune-0.4.1-cp26-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl

Download URL mistune-0.4.1-cp26-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Size 387.6 kB
Tags CPython 2.6 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
3452227c4ad865ff22caa3ae1a47959c2214663b7fe561427dd0ab58eb876a1d
BLAKE2b-256 checksum
How to use checksums
c022f4d5cd6d12fa88faa768226b33fed168af9e0fb46ed6251becaf4bed8104
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

3.3.4

2 release files

3.3.3

2 release files

3.3.2

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8

2 release files

0.7.4

4 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

5 release files

0.7

5 release files

0.6

5 release files

0.5.1

5 release files

0.5

5 release files

This release

0.4.1 This release

5 release files

0.4

5 release files

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page