How to Use Mistune ================== Mistune is super easy to use. Here is how you can convert Markdown formatted text into HTML:: import mistune mistune.html(YOUR_MARKDOWN_TEXT) The ``.html()`` methods has enabled all the features you might want by default: * No escape of HTML tags * With **strikethrough** plugin * With **table** plugin * With **footnote** plugin Customize Mistune ----------------- Mistune provides a function to create Markdown instance easily:: import mistune markdown = mistune.create_markdown() This method will create a "escaped" Markdown instance without any plugins, which means:: markdown('
<div>hello</div>
' Non escaped version:: markdown = mistune.create_markdown(escape=False) markdown('~~s~~
' markdown = mistune.create_markdown(plugins=['strikethrough']) markdown('~~s~~') # ==> 's
' + mistune.escape(code) + ''
markdown = mistune.create_markdown(renderer=HighlightRenderer())
print(markdown('```python\nassert 1 == 1\n```'))
In this way, we can use Pygments to highlight the fenced code. Learn more
at :ref:`renderers`.
.. _abstract-syntax-tree:
Abstract syntax tree
--------------------
Mistune can produce AST by default without any renderer::
markdown = mistune.create_markdown(renderer=None)
This ``markdown`` function will generate a list of tokens instead of HTML::
text = 'hello **world**'
markdown(text)
# ==>
[
{
'type': 'paragraph',
'children': [
{'type': 'text', 'raw': 'hello '},
{'type': 'strong', 'children': [{'type': 'text', 'raw': 'world'}]}
]
}
]
It is also possible to pass ``renderer='ast'`` to create the markdown instance::
markdown = mistune.create_markdown(renderer='ast')
For details on how to parse these tokens, see :ref:`parsing-ast-tokens`.