@
目录
小试牛刀
import mistune
from mistune.directives import DirectiveToc,DirectiveInclude
from mistune.plugins import plugin_footnotes,\
plugin_strikethrough,plugin_table,plugin_url,\
plugin_task_lists,plugin_def_list,plugin_abbr
renderer = mistune.HTMLRenderer()
markdown = mistune.create_markdown(renderer,escape=False,plugins=[DirectiveToc(),
DirectiveInclude(),# toc支持
plugin_footnotes, # 注脚
plugin_strikethrough, # 删除线
plugin_table, # 表格
plugin_url, # 链接
plugin_task_lists , # 任务列表
plugin_def_list, # 自定义列表
plugin_abbr, # 缩写
]
)
mdText = '''
@[toc]
# H1 title
~~here is the content~~
<https://typlog.com/>
https://typlog.com/
[链接](https://typlog.com/)
content in paragraph with footnote[^1] markup.
[^1]: footnote explain
## H2 title
- [x] item 1
- [ ] item 2
First term
: First definition
: Second definition
Second term
: Third definition
# H1 title
The HTML specification is maintained by the W3C.
*[HTML]: Hyper Text Markup Language
*[W3C]: World Wide Web Consortium
.. include:: study.md
'''
md_HTML = markdown(mdText)
with open("a.html","w+",encoding="utf-8") as f:
f.write(md_HTML)
上述代码你跑成功了吗?是不是还有许多不解的地方?没关系下面有你想要的
开始使用mistune
mistune简单使用
import mistune
mistune.html(YOUR_MARKDOWN_TEXT)
mistune高级用法(自定义mistune)
import mistune
markdown = mistune.create_markdown()
markdown('YOUR_MARKDOWN_TEXT')
参数
参数 | 释义 | 默认值 | 备注 |
---|---|---|---|
escape | HTML转义 | TRUE | 默认情况下将html文本转义,如:[1] |
plugins | 启用的插件功能 | None | 导入插件后添加到plugins中启用插件,他的传入值为列表,如:[2] |
hard_wrap | 将每一新行分成<br> |
False | 启用后md文件中的每一行都会解析成单独一行 |
renderer | 默认选项有AST解析器mistune.AstRenderer()和HTML解析器mistune.HTMLRenderer() , 也可以自定义[3] |
mistune中插件
插件使用方法(以 删除线(strikethrough) 为例)
mistune.html() 默认支持strikethrough. 创建自己的markdown实例:
markdown = mistune.create_markdown(plugins=['strikethrough'])
其他创建你自己的markdown实例的方法:
from mistune.plugins import plugin_strikethrough
renderer = mistune.HTMLRenderer()
markdown = mistune.Markdown(renderer, plugins=[plugin_strikethrough])
插件包名
内置插件
序号 | 插件目录 | 引用 |
---|---|---|
1. | 删除线(strikethrough) | from mistune.plugins import plugin_strikethrough |
2 | 注脚(footnotes) | from mistune.plugins import plugin_footnotes |
3 | 表格(table) | from mistune.plugins import plugin_table |
4 | 链接(url) | from mistune.plugins import plugin_url |
5 | 任务列表(task_lists) | from mistune.plugins import plugin_task_lists |
6 | 描述列表(def_list) | from mistune.plugins import plugin_def_list |
7 | 缩写(abbr) | from mistune.plugins import plugin_abbr |
删除线(strikethrough)
语法:
~~here is the content~~
显示:
here is the content
注脚(footnotes)
语法:
content in paragraph with footnote[^1] markup. [^1]: footnote explain
显示:
content in paragraph with footnote[4] markup.
表格(table)
语法:
简易式表格 :
Simple formatted table:
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
复杂的表格:
Complex formatted table:
| First Header | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |
表格对