Skip to main content

The fastest markdown parser in pure Python

Project description

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mistune-0.4.tar.gz (13.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mistune-0.4-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (350.9 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

mistune-0.4-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (350.6 kB view details)

Uploaded CPython 3.3mmacOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

mistune-0.4-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (363.4 kB view details)

Uploaded CPython 2.7macOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

mistune-0.4-cp26-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (363.4 kB view details)

Uploaded CPython 2.6macOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

File details

Details for the file mistune-0.4.tar.gz.

File metadata

  • Download URL: mistune-0.4.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mistune-0.4.tar.gz
Algorithm Hash digest
SHA256 76fa521141b3a65c97094ef7e7a5c2745cf2c6c374419c4029f422998152e677
MD5 bee1d80e51cfb4e17660b99233dbc850
BLAKE2b-256 12a2229025f7b9921eac983a378c9feabb4a8de8161b48a2022a9ad8bf42c041

See more details on using hashes here.

File details

Details for the file mistune-0.4-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mistune-0.4-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 644e24710392917e8b7b65fdf04b48b09f806a0d1d05b177d7a45793676d7b3b
MD5 7bf4f1138a5ad7dfa6acb83c970ca639
BLAKE2b-256 08653c36bb91e4c4b943cbd6e7deb011ba04f107f1077c4f113a0518748338ef

See more details on using hashes here.

File details

Details for the file mistune-0.4-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mistune-0.4-cp33-cp33m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cab723cccf9c3f0853d35207ec422ea38451ac9c28a9d0588a44f89acae3b163
MD5 fae6214c5163c67b93d899ee32d26e7a
BLAKE2b-256 1477ff211880431aa59e28d2630435baa1598a032bbc5266775070ecff896b82

See more details on using hashes here.

File details

Details for the file mistune-0.4-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mistune-0.4-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3072f8971999413e436affbbceab6f97cc48c2093739425d5fe73cea2735c622
MD5 e7156d3e8223cdb2ceda40cc64c5ca47
BLAKE2b-256 e9616b4958b1e6644a138686564b79617b051f5ee185181e323f6ca8e4e94be4

See more details on using hashes here.

File details

Details for the file mistune-0.4-cp26-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mistune-0.4-cp26-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6ad427ad9cb23e42e28993ef8b91ba8122fa76943e29603737528d6a96e31cde
MD5 933cd697f878847fa3b1af8ff1711006
BLAKE2b-256 975c3ac9d656784290a5c492131811b59c2022b991dfe90a443fc556dd33ba51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page