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.

285 lines
8.3 KiB

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