You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.2 KiB

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