1、無論是淺拷貝還是深拷貝,拷貝對象後是否會開闢新記憶體,取決於被拷貝對象的數據類型是否可變,一般來講,可變的數據類型會開闢新記憶體,不可變數據類型反之不會開闢新記憶體,進行記憶體地址的引用(-5-256以外的大整數池會開闢記憶體,但我本地進行測試比較記憶體還是一樣的,有問題) 2、要在單層、嵌套型對象中逐一比 ...
@
目錄小試牛刀
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 |
表格對齊
Align formatted table:
簡易寫法
Left Header | Center Header | Right Header
:----------- | :-------------: | ------------:
Conent Cell | Content Cell | Content Cell
複雜寫法
| Left Header | Center Header | Right Header |
| :---------- | :-------------: | ------------: |
| Conent Cell | Content Cell | Content Cell |
顯示:
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
Left Header | Center Header | Right Header |
---|---|---|
Conent Cell | Content Cell | Content Cell |
鏈接(url)
語法
允許使用預設的鏈接創建url
For instance, https://typlog.com/
顯示:
For instance , https://typlog.com/
任務列表
語法
- [x] item 1
- [ ] item 2
顯示:
描述列表
語法
First term
: First definition
: Second definitionSecond term
: Third definition
顯示:
- First term
- First definition
- Second definition
- Second term
- Third definition
縮寫(abber)
語法
The HTML specification is maintained by the W3C.
*[HTML]: Hyper Text Markup Language
*[W3C]: World Wide Web Consortium
顯示:
The HTML specification is maintained by the W3C.
解析器
使用解析器
你可以使用自己的渲染器來自定義HTML的輸出.創建一個mistune.HTMLRenderer的子類:
import mistune
from mistune import escape
class MyRenderer(mistune.HTMLRenderer):
def codespan(self, text):
if text.startswith('$') and text.endswith('$'):
return '<span class="math">' + escape(text) + '</span>'
return '<code>' + escape(text) + '</code>'
# 使用自定義解析器
markdown = mistune.create_markdown(renderer=MyRenderer())
print(markdown('hi `$a^2=4$`'))
可用的解析器功能列表
- 1.內聯級 inline level
- text(self, text)
- link(self, link, text=None, title=None)
- image(self, src, alt="", title=None)
- emphasis(self, text)
- strong(self, text)
- codespan(self, text)
- linebreak(self)
- newline(self)
- inline_html(self, html)
- 2.塊級 block level
- paragraph(self, text)
- heading(self, text, level)
- heading(self, text, level, tid) # when TOC directive is enabled
- thematic_break(self)
- block_text(self, text)
- block_code(self, code, info=None)
- block_quote(self, text)
- block_html(self, html)
- block_error(self, html)
- list(self, text, ordered, level, start=None)
- list_item(self, text, level)
- 3.由刪除插件提供 provided by strikethrough plugin
- strikethrough(self, text)
- 4.由表格插件提供 provide by table plugin
- table(self, text)
- table_head(self, text)
- table_body(self, text)
- table_row(self, text)
- table_cell(self, text, align=None, is_head=False)
- 5.由註膠插件提供 provided by footnotes plugin
- footnote_ref(self, key, index)
- footnotes(self, text)
- footnote_item(self, text, key, index)
- 6.確定最終呈現內容(定義輸出) Finalize rendered content (define output)
- finalize(self, data)
自定義渲染器
Midtune 支持開發者自定義渲染器功能,例如,創建一個代碼高亮渲染器
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, lang=None):
if lang:
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter()
return highlight(code, lexer, formatter)
return '<pre><code>' + mistune.escape(code) + '</code></pre>'
markdown = mistune.create_markdown(renderer=HighlightRenderer())
print(markdown('```python\nassert 1 == 1\n```'))
創建插件
Mistune有一些內置插件,您可以查看Mistune/plugins中的源代碼,瞭解如何編寫插件。讓我們以GitHub Wiki鏈接為例:
一個mistune插件示例:
# 為Wiki鏈接定義正則表達式 define regex for Wiki links
import mistune
WIKI_PATTERN = (
r'\[\[' # [[
r'([\s\S]+?\|[\s\S]+?)' # Page 2|Page 2
r'\]\](?!\])' # ]]
)
# 定義如何解析匹配項 define how to parse matched item
def parse_wiki(inline, m, state):
# ``inline`` is ``md.inline``, see below
# "m"是匹配的正則表達式項 ``m`` is matched regex item
text = m.group(1)
title, link = text.split('|')
return 'wiki', link, title
# 定義如何渲染HTML define how to render HTML
def render_html_wiki(link, title):
return f'<a href="{link}">{title}</a>'
def plugin_wiki(md):
# 這是一個內聯語法,因此我們將wiki規則註冊到md.inline中
# this is an inline grammar, so we register wiki rule into md.inline
# 語法: md.inline.register_rule(name, 正則表達式, 函數[解析匹配項])
# 註意名字要一直匹配
md.inline.register_rule('wiki', WIKI_PATTERN, parse_wiki)
# 將wiki規則添加到活動規則中
# add wiki rule into active rules
md.inline.rules.append('wiki')
# 添加HTML渲染器 add HTML renderer
if md.renderer.NAME == 'html':
md.renderer.register('wiki', render_html_wiki)
# 使用這個插件 use this plugin
markdown = mistune.create_markdown(plugins=[plugin_wiki])
資源
名稱 | 鏈接 |
---|---|
官方說明 | https://mistune.readthedocs.io/en/v2.0.4/guide.html |
mistune GitHub主頁 | https://github.com/lepture/mistune |
mistune 作者寫的其他插件 | https://github.com/lepture/mistune-contrib |