### Upgrade Guide: Plugins and Directives Source: https://mistune.lepture.com/en/latest/upgrade Information regarding Mistune plugins and directives during the v2 to v3 upgrade. ```APIDOC ## Upgrade Guide: Plugins and Directives ### Description Guidance on Mistune plugins and directives for the v2 to v3 upgrade. For plugins, refer to the advanced guide and built-in source code. For directives, v3 introduces a new fenced style. ### Method N/A (Documentation of changes, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Upgrade Guide: AstRenderer Changes Source: https://mistune.lepture.com/en/latest/upgrade Explains the removal of `AstRenderer` in Mistune v3 and how to achieve AST rendering. ```APIDOC ## Upgrade Guide: AstRenderer Changes ### Description In Mistune v3, the explicit `AstRenderer` class has been removed. AST rendering can now be achieved by passing `None` or `'ast'` to the `create_markdown` function. ### Method N/A (Documentation of changes, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python import mistune # To render AST in v3 md = mistune.create_markdown(renderer='ast') # or renderer=None markdown_text = '...markdown text...' markdown_output = md(markdown_text) ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Enable Math Plugin in Mistune Source: https://mistune.lepture.com/en/latest/plugins Illustrates how to enable the 'math' plugin for handling block and inline math syntax using
' + escape(text) + ''
# Usage:
markdown = mistune.create_markdown(renderer=MyRenderer())
print(markdown('hi `$a^2=4$`'))
```
### Available Methods for HTMLRenderer
This lists the methods available on `HTMLRenderer` and its plugins:
* **Inline Level:** `text`, `link`, `image`, `emphasis`, `strong`, `codespan`, `linebreak`, `softbreak`, `inline_html`
* **Block Level:** `paragraph`, `heading`, `blank_line`, `thematic_break`, `block_text`, `block_code`, `block_quote`, `block_html`, `block_error`, `list`, `list_item`
* **Plugin Provided:** `strikethrough`, `mark`, `insert`, `subscript`, `abbr`, `ruby`, `task_list_item`, `table`, `table_head`, `table_body`, `table_row`, `table_cell`, `footnote_ref`, `footnotes`, `footnote_item`, `def_list`, `def_list_head`, `def_list_item`, `block_math`, `inline_math`
```
--------------------------------
### Mistune CLI Help
Source: https://mistune.lepture.com/en/latest/cli
Displays the help message for the Mistune command-line tool, outlining available options and their purposes. This includes options for specifying input message or file, plugins, escape, hardwrap, output file, and renderer.
```bash
$ python -m mistune -h
```
--------------------------------
### Parsing Reference-Style Links in Python
Source: https://mistune.lepture.com/en/latest/api
This snippet demonstrates the Markdown syntax for reference-style links. It shows how to define a link text and its corresponding reference, followed by the definition of the reference with its URL and an optional title.
```python
a [link][example]
[example]: https://example.com "Optional title"
```
--------------------------------
### Generate Abstract Syntax Tree (AST) with Mistune
Source: https://mistune.lepture.com/en/latest/guide
Creates a Markdown instance configured to generate an Abstract Syntax Tree (AST) instead of HTML. This is achieved by setting `renderer=None` or `renderer='ast'`.
```python
import mistune
markdown = mistune.create_markdown(renderer=None)
text = 'hello **world**'
print(markdown(text))
```
```python
import mistune
markdown = mistune.create_markdown(renderer='ast')
```
--------------------------------
### Definition Lists Plugin
Source: https://mistune.lepture.com/en/latest/plugins
The def_list plugin enables the creation of HTML definition lists using the `term
: definition` syntax. This plugin is NOT enabled by default in `mistune.html()`.
```APIDOC
## Definition Lists Plugin
### Description
Enables HTML definition lists.
### Usage
**NOT ENABLED** by default in `mistune.html()`.
To enable the def_list plugin with your own markdown instance:
```python
import mistune
# Using create_markdown
markdown = mistune.create_markdown(plugins=['def_list'])
# Using Markdown constructor with explicit plugin
from mistune.plugins.def_list import def_list
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[def_list])
```
```
--------------------------------
### Enable Definition Lists Plugin in Mistune
Source: https://mistune.lepture.com/en/latest/plugins
Describes how to enable the def_list plugin in Mistune for creating HTML definition lists. This functionality is not enabled by default in `mistune.html()`. Two Python code snippets show how to set it up.
```python
markdown = mistune.create_markdown(plugins=['def_list'])
```
```python
from mistune.plugins.def_list import def_list
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[def_list])
```
--------------------------------
### Unix PIPE Markdown Conversion
Source: https://mistune.lepture.com/en/latest/cli
Demonstrates using the Mistune command-line tool with Unix pipes. Standard input from 'echo' is piped to mistune for conversion to HTML.
```bash
echo "foo **bar**" | python -m mistune
```
--------------------------------
### Enable Table Plugin in Mistune
Source: https://mistune.lepture.com/en/latest/plugins
Shows how to enable the table plugin in Mistune, which is active by default in `mistune.html()`. Includes two Python snippets for initializing a Markdown object with table support.
```python
markdown = mistune.create_markdown(plugins=['table'])
```
```python
from mistune.plugins.table import table
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[table])
```
--------------------------------
### Customize Mistune renderer for syntax highlighting
Source: https://mistune.lepture.com/en/latest/guide
Implements a custom renderer `HighlightRenderer` that extends `mistune.HTMLRenderer`. It overrides the `block_code` method to use Pygments for syntax highlighting of code blocks.
```python
import mistune
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import html
class HighlightRenderer(mistune.HTMLRenderer):
def block_code(self, code, info=None):
if info:
lexer = get_lexer_by_name(info, stripall=True)
formatter = html.HtmlFormatter()
return highlight(code, lexer, formatter)
return '' + mistune.escape(code) + ''
markdown = mistune.create_markdown(renderer=HighlightRenderer())
print(markdown('```python\nassert 1 == 1\n```'))
```
--------------------------------
### Markdown Renderer
Source: https://mistune.lepture.com/en/latest/renderers
Explains how to use the `MarkdownRenderer` to reformat Markdown text and how to customize it for plugins.
```APIDOC
## Markdown Renderer
### Description
The `MarkdownRenderer` is used to reformat Markdown text. The basic `MarkdownRenderer` only supports standard Markdown syntax. For plugins, you need to customize it.
### Basic Usage
```python
from mistune.renderers.markdown import MarkdownRenderer
format_markdown = mistune.create_markdown(renderer=MarkdownRenderer())
print(format_markdown('your_markdown_text'))
```
### Customizing with Plugins
To add support for plugins like strikethrough, subclass `MarkdownRenderer` and implement the corresponding methods.
```python
from mistune.renderers.markdown import MarkdownRenderer
class MyRenderer(MarkdownRenderer):
def strikethrough(self, token, state):
return '~~' + self.render_children(token, state) + '~~
# Usage with strikethrough plugin
format_markdown = mistune.create_markdown(renderer=MyRenderer(), plugins=['strikethrough'])
print(format_markdown('~~This is strikethrough~~'))
```
### Default Methods for MarkdownRenderer
* **Inline Level:** `text`, `link`, `image`, `emphasis`, `strong`, `codespan`, `linebreak`, `softbreak`, `inline_html`
* **Block Level:** `paragraph`, `heading`, `blank_line`, `thematic_break`, `block_text`, `block_code`, `block_quote`, `block_html`, `block_error`, `list`
```
--------------------------------
### Enable Mark Plugin in Mistune
Source: https://mistune.lepture.com/en/latest/plugins
Demonstrates how to enable the 'mark' plugin for inserting tags. This plugin is not enabled by default. Two methods are shown: using create_markdown with a plugin list, and creating a custom Markdown instance with an HTMLRenderer and the mark plugin.
```python
markdown = mistune.create_markdown(plugins=['mark'])
```
```python
from mistune.plugins.formatting import mark
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[mark])
```
--------------------------------
### Convert Markdown to HTML via File
Source: https://mistune.lepture.com/en/latest/cli
Converts a markdown file (e.g., README.md) to HTML using the Mistune command-line tool. This is the default conversion behavior.
```bash
$ python -m mistune -f README.md
```
--------------------------------
### Add strikethrough plugin to Mistune
Source: https://mistune.lepture.com/en/latest/guide
Creates a Markdown instance with the 'strikethrough' plugin enabled. This allows the use of `~~text~~` for strikethrough formatting.
```python
import mistune
markdown = mistune.create_markdown(plugins=['strikethrough'])
markdown('~~s~~')
```
--------------------------------
### Mistune Math Plugin
Source: https://mistune.lepture.com/en/latest/api
Provides support for mathematical notation using LaTeX-like syntax, with block math delimited by `$$` and inline math by `$`. This plugin requires a Markdown instance. Variants exist for use within quotes and lists.
```python
import mistune
markdown = mistune.create_markdown(plugins=['math'])
text = "Block math is surrounded by $$:\n\n$$ f(a)=f(b) $$ \n\nInline math is surrounded by `$`, such as $f(a)=f(b)$"
html = markdown(text)
print(html)
```
--------------------------------
### Customize HTMLRenderer for Inline Math in Mistune
Source: https://mistune.lepture.com/en/latest/renderers
Demonstrates how to create a custom HTMLRenderer in Mistune to handle inline math syntax, extending the default behavior for specific patterns like '$a^2=4$'. It shows subclassing HTMLRenderer and overriding the 'codespan' method.
```python
from mistune import HTMLRenderer
import mistune
from mistune.util import escape
class MyRenderer(HTMLRenderer):
def codespan(self, text):
if text.startswith('$') and text.endswith('$'):
return '' + escape(text) + ''
return '' + escape(text) + ''
# use customized renderer
markdown = mistune.create_markdown(renderer=MyRenderer())
print(markdown('hi `$a^2=4$`'))
```
--------------------------------
### Table Plugin
Source: https://mistune.lepture.com/en/latest/plugins
The table plugin enables the creation of markdown tables using pipe-delimited syntax. It is enabled by default in `mistune.html()`.
```APIDOC
## Table Plugin
### Description
Enables markdown table syntax.
### Usage
Enabled by default in `mistune.html()`.
To create a markdown instance with the table plugin:
```python
import mistune
# Using create_markdown
markdown = mistune.create_markdown(plugins=['table'])
# Using Markdown constructor with explicit plugin
from mistune.plugins.table import table
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[table])
```
```
--------------------------------
### Enable Strikethrough Plugin in Mistune
Source: https://mistune.lepture.com/en/latest/plugins
Demonstrates how to enable the strikethrough plugin in Mistune, which is enabled by default in `mistune.html()`. Shows two methods for creating a custom Markdown instance with the plugin.
```python
markdown = mistune.create_markdown(plugins=['strikethrough'])
```
```python
from mistune.plugins.formatting import strikethrough
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[strikethrough])
```
--------------------------------
### Enable Spoiler Plugin with create_markdown
Source: https://mistune.lepture.com/en/latest/plugins
Shows how to enable the spoiler plugin when creating a markdown instance using the `mistune.create_markdown` function by passing 'spoiler' in the plugins list.
```python
markdown = mistune.create_markdown(plugins=['spoiler'])
```
--------------------------------
### Enable Spoiler Plugin with HTMLRenderer
Source: https://mistune.lepture.com/en/latest/plugins
Demonstrates an alternative method to enable the spoiler plugin by explicitly importing the plugin and passing it to the `mistune.Markdown` constructor along with an `HTMLRenderer`.
```python
from mistune.plugins.spoiler import spoiler
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[spoiler])
```
--------------------------------
### Block Spoiler Syntax and Conversion
Source: https://mistune.lepture.com/en/latest/plugins
Demonstrates the syntax for a block-level spoiler using the '>!' marker and its corresponding HTML conversion into a `here is the spoiler content
it will be hidden
this is the hidden text
``` -------------------------------- ### Mistune Task Lists Plugin Source: https://mistune.lepture.com/en/latest/api Enables task list support in markdown, following GitHub Flavored Markdown specifications. It allows for creating lists with checkboxes for 'to-do' items. This plugin requires a Markdown instance. ```python import mistune markdown = mistune.create_markdown(plugins=['task_lists']) text = "- [ ] unchecked task\n- [x] checked task" html = markdown(text) print(html) ``` -------------------------------- ### Create a non-escaped Mistune Markdown instance Source: https://mistune.lepture.com/en/latest/guide Creates a Markdown instance with HTML escaping disabled by setting `escape=False`. This allows raw HTML tags to be rendered directly. ```python import mistune markdown = mistune.create_markdown(escape=False) markdown('