Refer to Python-Markdown Documentation
- International (Unicode) Support 支持国际化
- Output Format:
HTML5
andXHTML
support 支持HTML5
,XHTML
输出格式 - Extention Support 支持扩展
- Command Line Interface 命令行
python3 -m pip install markdown
import markdown
text = 'markdown file content'
output = markdown.markdown(text)
assert isinstance(output, str)
# Determine output format, "xhtml" by default
# 定义输出格式,默认"xhtml"
output = markdown.markdown(text, output_format='html5')
# Determine length of tabs, 4 by default
# 定义Tab缩进, 默认4
output = markdown.markdown(text, output_format='html5', tab_length=4)
# Input from Markdown file, output to a HTML-5 file
# 文件输入输出
input_file = 'example.md'
output_file = 'example.html'
markdown.markdownFromFile(input_file, output_file)
import markdown
text = 'markdown file content'
output = markdown.markdown(text, extensions=['extra'])
output = markdown.markdown(text, extensions=['extra'], extension_configs = {
'extra': {
'option': 'value',
}
})
Custom Extension 自定义插件
import markdown
from markdown.extensions import Extension
class MyExtClass(Extension):
pass
# define your extension here...
text = 'markdown file content'
output = markdown.markdown(text, extensions=[MyExtClass(option='value')])
import markdown
# Process multiple texts
# 批量处理多个文本
md = markdown.Markdown(output_format='html5', tab_length=4)
md.convert(text1)
md.reset().convert(text2)
# Process multiple files
# 批量处理多个文件
md = markdown.Markdown(output_format='html5', tab_length=4)
md.convertFile(input_file1, output_file1)
md.reset().convertFile(input_file1, output_file1)
Extension 扩展 | Entry Point 参数调用名 |
---|---|
Extra | extra |
Abbreviations | abbr |
Attribute Lists | attr_list |
Definition Lists | def_list |
Fenced Code Blocks | fenced_code |
Footnotes | footnotes |
Tables | tables |
Admonition | admonition |
CodeHilite | codehilite |
Meta-Data | meta |
New Line to Break | nl2br |
Sane Lists | sane_lists |
SmartyPants | smarty |
Table of Contents | toc |
WikiLinks | wikilinks |
This is *true* markdown text.
<div markdown="1">
This is *true* markdown text.
</div>
Result:
<p>This is <em>true</em> markdown text.</p>
<div>
<p>This is <em>true</em> markdown text.</p>
</div>
The HTML specification
is maintained by the W3C.
*[HTML]: Hyper Text Markup Language
*[W3C]: World Wide Web Consortium
will be rendered as:
<p>The <abbr title="Hyper Text Markup Language">HTML</abbr> specification
is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>
{: #id1 .class1 .class2 key='value' }
A word which starts with a hash (#
) will set the id of an element.
A word which starts with a dot (.
) will be added to the list of class