• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

mistune.egg-info/H03-May-2022-284208

tests/H11-Oct-2018-2,8371,817

CHANGES.rstH A D11-Oct-20184.8 KiB209133

LICENSEH A D14-Sep-20171.4 KiB157

MANIFEST.inH A D14-Sep-2017152 65

MakefileH A D14-Sep-2017703 4430

PKG-INFOH A D11-Oct-201810.3 KiB284208

README.rstH A D11-Oct-20187.2 KiB258183

mistune.pyH A D11-Oct-201835.7 KiB1,185955

setup.cfgH A D11-Oct-2018111 139

setup.pyH A D11-Oct-20181.4 KiB4538

README.rst

1Mistune
2=======
3
4The fastest markdown parser in pure Python with renderer features,
5inspired by marked_.
6
7.. image:: https://img.shields.io/badge/donate-lepture-green.svg
8   :target: https://lepture.com/donate
9   :alt: Donate lepture
10.. image:: https://img.shields.io/pypi/wheel/mistune.svg?style=flat
11   :target: https://pypi.python.org/pypi/mistune/
12   :alt: Wheel Status
13.. image:: https://anaconda.org/conda-forge/mistune/badges/version.svg
14   :target: https://anaconda.org/conda-forge/mistune
15   :alt: Conda Version
16.. image:: https://img.shields.io/pypi/v/mistune.svg
17   :target: https://pypi.python.org/pypi/mistune/
18   :alt: Latest Version
19.. image:: https://travis-ci.org/lepture/mistune.svg?branch=master
20   :target: https://travis-ci.org/lepture/mistune
21   :alt: Travis CI Status
22.. image:: https://coveralls.io/repos/lepture/mistune/badge.svg?branch=master
23   :target: https://coveralls.io/r/lepture/mistune
24   :alt: Coverage Status
25.. image:: https://ci.appveyor.com/api/projects/status/8ai8tfwp75oela17?svg=true
26   :target: https://ci.appveyor.com/project/lepture/mistune
27   :alt: App Veyor CI Status
28
29.. _marked: https://github.com/chjj/marked
30
31
32Features
33--------
34
35* **Pure Python**. Tested in Python 2.7, Python 3.5+ and PyPy.
36* **Very Fast**. It is the fastest in all **pure Python** markdown parsers.
37* **More Features**. Table, footnotes, autolink, fenced code etc.
38
39View the `benchmark results <https://github.com/lepture/mistune/issues/1>`_.
40
41Installation
42------------
43
44Installing mistune with pip::
45
46    $ pip install mistune
47
48
49Mistune can be faster, if you compile with cython::
50
51    $ pip install cython mistune
52
53
54Basic Usage
55-----------
56
57A simple API that render a markdown formatted text:
58
59.. code:: python
60
61    import mistune
62
63    mistune.markdown('I am using **mistune markdown parser**')
64    # output: <p>I am using <strong>mistune markdown parser</strong></p>
65
66If you care about performance, it is better to re-use the Markdown instance:
67
68.. code:: python
69
70    import mistune
71
72    markdown = mistune.Markdown()
73    markdown('I am using **mistune markdown parser**')
74
75Mistune has enabled all features by default. You don't have to configure
76anything. But there are options for you to change the parser behaviors.
77
78
79Options
80-------
81
82Here is a list of all options that will affect the rendering results,
83configure them with ``mistune.Renderer``:
84
85.. code:: python
86
87    renderer = mistune.Renderer(escape=True, hard_wrap=True)
88    # use this renderer instance
89    markdown = mistune.Markdown(renderer=renderer)
90    markdown(text)
91
92* **escape**: if set to *False*, all raw html tags will not be escaped.
93* **hard_wrap**: if set to *True*, it will has GFM line breaks feature.
94  All new lines will be replaced with ``<br>`` tag
95* **use_xhtml**: if set to *True*, all tags will be in xhtml, for example: ``<hr />``.
96* **parse_block_html**: parse text only in block level html.
97* **parse_inline_html**: parse text only in inline level html.
98
99When using the default renderer, you can use one of the following shortcuts::
100
101    mistune.markdown(text, escape=True, hard_wrap=True)
102
103    markdown = mistune.Markdown(escape=True, hard_wrap=True)
104    markdown(text)
105
106
107Renderer
108--------
109
110Like misaka/sundown, you can influence the rendering by custom renderers.
111All you need to do is subclassing a `Renderer` class.
112
113Here is an example of code highlighting:
114
115.. code:: python
116
117    import mistune
118    from pygments import highlight
119    from pygments.lexers import get_lexer_by_name
120    from pygments.formatters import html
121
122    class HighlightRenderer(mistune.Renderer):
123        def block_code(self, code, lang):
124            if not lang:
125                return '\n<pre><code>%s</code></pre>\n' % \
126                    mistune.escape(code)
127            lexer = get_lexer_by_name(lang, stripall=True)
128            formatter = html.HtmlFormatter()
129            return highlight(code, lexer, formatter)
130
131    renderer = HighlightRenderer()
132    markdown = mistune.Markdown(renderer=renderer)
133    print(markdown('```python\nassert 1 == 1\n```'))
134
135Find more renderers in `mistune-contrib`_.
136
137Block Level
138~~~~~~~~~~~
139
140Here is a list of block level renderer API::
141
142    block_code(code, language=None)
143    block_quote(text)
144    block_html(html)
145    header(text, level, raw=None)
146    hrule()
147    list(body, ordered=True)
148    list_item(text)
149    paragraph(text)
150    table(header, body)
151    table_row(content)
152    table_cell(content, **flags)
153
154The *flags* tells you whether it is header with ``flags['header']``. And it
155also tells you the align with ``flags['align']``.
156
157
158Span Level
159~~~~~~~~~~
160
161Here is a list of span level renderer API::
162
163    autolink(link, is_email=False)
164    codespan(text)
165    double_emphasis(text)
166    emphasis(text)
167    image(src, title, alt_text)
168    linebreak()
169    newline()
170    link(link, title, content)
171    strikethrough(text)
172    text(text)
173    inline_html(text)
174
175Footnotes
176~~~~~~~~~
177
178Here is a list of renderers related to footnotes::
179
180    footnote_ref(key, index)
181    footnote_item(key, text)
182    footnotes(text)
183
184Lexers
185------
186
187Sometimes you want to add your own rules to Markdown, such as GitHub Wiki
188links. You can't achieve this goal with renderers. You will need to deal
189with the lexers, it would be a little difficult for the first time.
190
191We will take an example for GitHub Wiki links: ``[[Page 2|Page 2]]``.
192It is an inline grammar, which requires custom ``InlineGrammar`` and
193``InlineLexer``:
194
195.. code:: python
196
197    import copy,re
198    from mistune import Renderer, InlineGrammar, InlineLexer
199
200    class WikiLinkRenderer(Renderer):
201        def wiki_link(self, alt, link):
202            return '<a href="%s">%s</a>' % (link, alt)
203
204    class WikiLinkInlineLexer(InlineLexer):
205        def enable_wiki_link(self):
206            # add wiki_link rules
207            self.rules.wiki_link = re.compile(
208                r'\[\['                   # [[
209                r'([\s\S]+?\|[\s\S]+?)'   # Page 2|Page 2
210                r'\]\](?!\])'             # ]]
211            )
212
213            # Add wiki_link parser to default rules
214            # you can insert it some place you like
215            # but place matters, maybe 3 is not good
216            self.default_rules.insert(3, 'wiki_link')
217
218        def output_wiki_link(self, m):
219            text = m.group(1)
220            alt, link = text.split('|')
221            # you can create an custom render
222            # you can also return the html if you like
223            return self.renderer.wiki_link(alt, link)
224
225You should pass the inline lexer to ``Markdown`` parser:
226
227.. code:: python
228
229    renderer = WikiLinkRenderer()
230    inline = WikiLinkInlineLexer(renderer)
231    # enable the feature
232    inline.enable_wiki_link()
233    markdown = Markdown(renderer, inline=inline)
234    markdown('[[Link Text|Wiki Link]]')
235
236It is the same with block level lexer. It would take a while to understand
237the whole mechanism. But you won't do the trick a lot.
238
239
240Contribution & Extensions
241-------------------------
242
243Mistune itself doesn't accept any extension. It will always be a simple one
244file script.
245
246If you want to add features, you can head over to `mistune-contrib`_.
247
248Here are some extensions already in `mistune-contrib`_:
249
250* Math/MathJax features
251* Highlight Code Renderer
252* TOC table of content features
253* MultiMarkdown Metadata parser
254
255Get inspired with the contrib repository.
256
257.. _`mistune-contrib`: https://github.com/lepture/mistune-contrib
258