python的markdown擴(kuò)展,功能較為豐富,里面甚至集成了一些 rST-style 的命令。極大的擴(kuò)展了文章的表現(xiàn)力。
也有第三方擴(kuò)展
[^@#
這是對(duì)于模仿 php Markdown Extra 的 python-markdown 擴(kuò)展的一個(gè)合輯
支持下面這些擴(kuò)展:
Abbreviations 縮寫(xiě)Attribute Lists 屬性列表Definition Lists 定義列表Fenced Code Blocks 代碼塊Footnotes 腳注Tables 表格Smart Strong 加粗The HTML specification is maintained by the W3C.
*[HTML]: Hyper Text Markup Language *[W3C]: World Wide Web Consortium
擴(kuò)展了定義縮寫(xiě)的能力。被定義的縮寫(xiě)會(huì)被包含在 標(biāo)簽里。
The HTML specification is maintained by the W3C.*[HTML]: Hyper Text Markup Language*[W3C]: World Wide Web Consortium轉(zhuǎn)換成
<p>The <abbr title="Hyper Text Markup Language">HTML</abbr> specification is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>想法:
不止可以用作縮寫(xiě),還可以直接作為一些提示信息,免去了鏈接,腳注,或者加括號(hào)注釋的麻煩。
屬性列表增加了定義多中HTML元素的屬性的方式。
格式例子:
{: #someid .someclass somekey='some value' }用#開(kāi)頭的是元素id,用.開(kāi)始的是分配給元素的class的列表,后面的鍵值等式,將被賦到元素上。
要知道,當(dāng).語(yǔ)法被加到類(lèi)里,使用鍵值對(duì)會(huì)重寫(xiě)之前定義的屬性:
{: #id1 .class1 id=id2 class="class2 class3" .class4 }被轉(zhuǎn)換成:
id="id2" class="class2 class3 class4"屬性列表要放到對(duì)應(yīng)的塊的最后一行。
This is a paragraph.{: #an_id .a_class }會(huì)有:
<p id="an_id" class="a_class">This is a paragraph.</p>要求是在同一行:
A setext style header {: #setext}=================================### A hash style header ### {: #hash }轉(zhuǎn)換為:
<h1 id="setext">A setext style header</h1><h3 id="hash">A hash style header</h3>屬性列表應(yīng)該被在內(nèi)鏈屬性后,無(wú)間隔,定義:
[link](http://example.com){: class="foo bar" title="Some title!" }轉(zhuǎn)換為:
<p><a class="foo bar" title="Some title!">link</a></p>個(gè)人想法:
感覺(jué)使用價(jià)值不大。更加偏向于html本身的元素的設(shè)置。
可以創(chuàng)建定義列表。
Apple: Pomaceous fruit of plants of the genus Malus in the family Rosaceae.Orange: The fruit of an evergreen tree of the genus Citrus.轉(zhuǎn)換為:
<dl><dt>Apple</dt><dd>Pomaceous fruit of plants of the genus Malus in the family Rosaceae.</dd><dt>Orange</dt><dd>The fruit of an evergreen tree of the genus Citrus.</dd></dl>個(gè)人看法:
感覺(jué)很適合用來(lái)表示一些詞匯的解釋與定義。
用符號(hào)表示的代碼塊,克服了一些縮進(jìn)代碼塊的局限。
不支持內(nèi)嵌。即,只支持用在文本最左側(cè)的級(jí)別。
This is a paragraph introducing:~~~~~~~~~~~~~~~~~~~~(至少三個(gè),上下最好要一樣多)a one-line code block~~~~~~~~~~~~~~~~~~~~(至少三個(gè),不然隔不斷)顯示:
This is a paragraph introducing:
~ a one-line code block ~
指定語(yǔ)言:
~~~~{.python}# python code~~~~~~~~.html<p>HTML Document</p>~~~~轉(zhuǎn)換為:
<PRe><code class="python"># python code</code></pre><pre><code class="html"><p>HTML Document</p></code></pre>效果:
~~~~{.python}
~~~~
~~~~.html
HTML Document
~~~~
其中,更建議使用同樣被支持的GITHUB代碼塊格式 ```
個(gè)人看法:
還是用 ```好,方便。
如果想要讓代碼塊使用 CodeHilite 擴(kuò)展來(lái)高亮,得有 Pygments ,之后就會(huì)被適當(dāng)?shù)母吡赁D(zhuǎn)換。
和其中的冒號(hào)語(yǔ)法相似的是,可以強(qiáng)調(diào)某些行。
要使用:
~~~~{.python hl_lines="1 3"}# This line is emphasized# This line isn't# This line is emphasized~~~~或者:
```python hl_lines="1 3"# This line is emphasized# This line isn't# This line is emphasized```Footnotes1 have a label[^@#$%] and the footnote’s content.
Paragraph two of the definition.> A blockquote with> multiple lines. a code blockA final paragraph.在方括號(hào)里只有第一個(gè)脫字符有特殊含義,而且,括號(hào)內(nèi)可以使用任意字符,包含空格。前后對(duì)應(yīng)就好。
常規(guī):
Footnotes2 have a label[^@#$%] and the footnote's content.3: This is a footnote content.[^@#$%]: A footnote on the label: "@#$%".特殊的:從第二行的開(kāi)頭就相當(dāng)于文本中的最左側(cè),markdown標(biāo)記是有效的。就像下面的,引用,代碼塊,都是有顯示的。
4: The first paragraph of the definition. Paragraph two of the definition. > A blockquote with > multiple lines. a code block A final paragraph.特殊的一些配置:
The following options are provided to configure the output(輸出):
PLACE_MARKER: A text string used to mark the position where the footnotes(腳注) are rendered(致使).默認(rèn)的是 ///Footnotes (去掉括號(hào)) Go Here///
If the place marker text is not found in the document, the footnote definitions(定義) are placed at the end of the resulting HTML document.
默認(rèn)的是 ///Footnotes (去掉括號(hào)) Go Here/// 這句話后面就有一句,直接把所在的一個(gè)單元全部替換了。
Defaults to ///Footnotes Go Here///.
UNIQUE_IDS: Whether to avoid collisions(碰撞) across multiple calls to reset(). Defaults to False.
BACKLINK_TEXT: The text string that links from the footnote definition back to the position in the document. Defaults to ?.這個(gè)設(shè)置的是跳回原文的那個(gè)小圖標(biāo)文本。
個(gè)人想法:
上面的這些設(shè)置感覺(jué)很有用。
例子:
First Header | Second Header------------- | -------------Content Cell | Content CellContent Cell | Content Cell轉(zhuǎn)換為:
<table> <thead> <tr> <th>First Header</th> <th>Second Header</th> </tr> </thead> <tbody> <tr> <td>Content Cell</td> <td>Content Cell</td> </tr> <tr> <td>Content Cell</td> <td>Content Cell</td> </tr> </tbody></table>特別的:
| Item | Value || --------- | -----:|| Computer | $1600 || Phone | $12 || Pipe | $1 || Function name | Description || ------------- | ------------------------------ || `help()` | Display the help window. || `destroy()` | **Destroy your computer!** || Item | Value |
|---|---|
| Computer | $1600 |
| Phone | $12 |
| Pipe | $1 |
| Function name | Description |
|---|---|
help() | Display the help window. |
destroy() | Destroy your computer! |
Text with double__underscore__Words. Strong still works. this__works__too.
處理雙下劃線。
Text with double__underscore__words.__Strong__ still works.__this__works__too__.->
<p>Text with double__underscore__words.</p><p><strong>Strong</strong> still works.</p><p><strong>this__works__too</strong>.</p>由上可見(jiàn),緊湊文本內(nèi)的成對(duì)雙下劃線是不起作用的。多個(gè)雙下劃線,起到加粗作用的只有最外面的一對(duì)。
個(gè)人看法: 類(lèi)似的還有成對(duì)單下劃線。感覺(jué)和成對(duì)的星號(hào)用途類(lèi)似。
!!! note You should note that the title will be automatically capitalized.
!!! danger “Don’t try this at home” …
!!! important “” This is a admonition box without a title.
添加了 rST-style 的警告標(biāo)記
其中,type 會(huì)被用作CSS的類(lèi)名,作為默認(rèn)的標(biāo)題,必須是個(gè)單詞。你可以自己隨便寫(xiě)。
轉(zhuǎn)換成:
<div class="admonition note"><p class="admonition-title">Note</p><p>You should note that the title will be automatically capitalized.</p></div><div class="admonition danger"><p class="admonition-title">Don't try this at home</p><p>...</p></div><div class="admonition important"><p>This is a admonition box without a title.</p></div>個(gè)人看法:
很實(shí)用。
使用Pygments高亮python-markdown文本中的代碼塊。
輸入:
#!/usr/bin/python # Code goes here ...會(huì)顯示:
#!/usr/bin/python# Code goes here ...輸入:
#!python # Code goes here ...會(huì)顯示:
# Code goes here ...特別的——輸入:
:::python # Code goes here ...顯示:
# Code goes here ...輸入:
:::python hl_lines="1 3"# This line is emphasized# This line isn't# This line is emphasizedhl_lines is named for Pygments’ option meaning “highlighted lines”. When no language is defined, the Pygments highlighting engine will try to guess the language (unless guess_lang is set to False).
The following options are provided to configure the output:
linenums: 行號(hào) Use line numbers. Possible values are True for yes, False for no and None for auto. Defaults to None.
Using True will force every code block to have line numbers, even when using colons (:::) for language identification.
Using False will turn off all line numbers, even when using shebangs (#!) for language identification.
guess_lang: 語(yǔ)言自動(dòng)檢測(cè) Automatic language detection. Defaults to True.
Using False will prevent Pygments from guessing the language, and thus highlighting blocks only when you explicitly set the language.
css_class: Set CSS class name for the wrapper
tag. Defaults to codehilite.pygments_style: Pygments HTML Formatter Style (ColorScheme). Defaults to default.
This is useful only when noclasses is set to True, otherwise the CSS styles must be provided by the end user.
noclasses: Use inline styles instead of CSS classes. Defaults to False.
use_pygments: 使用 Defaults to True. Set to False to disable the use of Pygments. If a language is defined for a code block, it will be assigned to the tag as a class in the manner suggested by the HTML5 spec (alternate output will not be entertained) and might be used by a javaScript library in the browser to highlight the code block.
個(gè)人看法:
主要還是關(guān)于代碼高亮的。
即 #的功能。
The following options are provided to configure the output(輸出):
level: Base level for headers.
Default: 1
The level setting allows you to automatically(自動(dòng)地) adjust(調(diào)整) the header levels to fit within the hierarchy(層級(jí)) of your HTML templates(模板). For example, suppose the markdown text for a page should not contain any headers higher than level 3 (<h3>). The following will accomplish(完成) that:
forceid: Force all headers to have an id.
Default: True
The forceid setting turns on or off the automatically generated(形成) ids for headers that do not have one explicitly(明確地) defined(定義) (using the Attribute List extension).
separator: Word separator(分離器). Character which replaces white space in id.
Default: -
slugify: Callable to generate anchors(錨).
Default: markdown.extensions.headerid.slugify
If you would like to use a different algorithm(算法) to define the ids, you can pass in a callable which takes two arguments:
value: The string to slugify.separator: The Word Separator.The HeaderId extension(延長(zhǎng)) also supports the Meta-Data extension. Please see the documentation(文件材料) for that extension for specifics(特性). The supported meta-data keywords are:
例如:
header_level: 2header_forceid: Off# A Header轉(zhuǎn)換為:
<h2>A Header</h2>可以定義文檔的元數(shù)據(jù)。
元數(shù)據(jù)包含一系列的關(guān)鍵字和值,定義在文檔開(kāi)頭。像這樣:
Title: My DocumentSummary: A brief description of my document.Authors: Waylan Limberg John DoeDate: October 2, 2007blank-value: base_url: http://example.comThis is the first paragraph of the document.關(guān)鍵字不分大小寫(xiě),可包含字母,數(shù)字,下劃線,連接符,必須接有冒號(hào),值可以是任何,包括空白,但是要與冒號(hào)在一行,接在后面。
可以多行排列值,但是新起的一行要有至少四個(gè)空格。
文檔的第一個(gè)空行會(huì)結(jié)束所有的元數(shù)據(jù)部分,所以文檔第一行不可是空。
當(dāng)然也可以使用YAML格式來(lái)標(biāo)記元數(shù)據(jù)的開(kāi)始與結(jié)束。 開(kāi)頭第一行必須是---,結(jié)尾是空行,或者---,或者...
所有的元數(shù)據(jù)在處理時(shí),會(huì)被優(yōu)先從文檔中剝離。
元數(shù)據(jù)可以被訪問(wèn):
>>> md = markdown.Markdown(extensions = ['markdown.extensions.meta'])>>> html = md.convert(text)>>> # Meta-data has been stripped from output>>> print html<p>This is the first paragraph of the document.</p>>>> # View meta-data>>> print md.Meta{'title' : ['My Document'],'summary' : ['A brief description of my document.'],'authors' : ['Waylan Limberg', 'John Doe'],'date' : ['October 2, 2007'],'blank-value' : [''],'base_url' : ['http://example.com']}The following extensions are currently known to work with the Meta-Data extension. The keywords they are known to support are also listed.
HeaderId header_levelheader_forceidWikiLinks wiki_base_urlwiki_end_urlwiki_html_class個(gè)人看法:
用Pelican寫(xiě)博文時(shí),開(kāi)頭就用的是元數(shù)據(jù)。記得在用jekyll的框架時(shí),也是有。
換行。
Line 1Line 2->
<p>Line 1<br />Line 2</p>Line 1 Line 2
個(gè)人看法:
這個(gè)擴(kuò)展應(yīng)該就是把平時(shí)多按的回車(chē)給省了。
Ordered item 2
Unordered item 1Unordered item 2關(guān)于有序無(wú)序列表的一個(gè)擴(kuò)展。
不允許列表類(lèi)型混用。
默認(rèn)的markdown行為中
1. Ordered item 12. Ordered item 2- Unordered item 1- Unordered item 2輸出結(jié)果是:
1. Ordered item 12. Ordered item 23. Unordered item 14. Unordered item 2但是這個(gè)擴(kuò)展就改變了:
<ol> <li>Ordered item 1</li> <li>Ordered item 2</li></ol><ul> <li>Unordered item 1</li> <li>Unordered item 2</li></ul>有序無(wú)需全都有。互不影響。
使用一種列表時(shí),前面一定要有空行。
該擴(kuò)展,將ascii中的引號(hào),連接號(hào),省略號(hào)轉(zhuǎn)換為對(duì)應(yīng)的HTML等價(jià)表示。
| ASCII symbol | Replacements | HTML Entities | Substitution Keys ||---|---|---|---||' | ‘ ’ | ‘ ’ | 'left-single-quote', 'right-single-quote'||" | “ ” | “ ” | 'left-double-quote', 'right-double-quote'||<< >> | ? ? | « » | 'left-angle-quote', 'right-angle-quote'||... | … | … | 'ellipsis'||-- | – | – | 'ndash'||--- | — | — | 'mdash'|| ASCII symbol | Replacements | HTML Entities | Substitution Keys |
|---|---|---|---|
| ‘ | ‘ ’ | ‘ ’ | ‘left-single-quote’, ‘right-single-quote’ |
| “ | “ ” | “ ” | ‘left-double-quote’, ‘right-double-quote’ |
| << >> | ? ? | ? ? | ‘left-angle-quote’, ‘right-angle-quote’ |
| … | … | … | ‘ellipsis’ |
| – | – | – | ‘ndash’ |
| — | — | ‘mdash’ |
Note This extension(延長(zhǎng)) re-implements the Python SmartyPants library by integrating(完整) it into the markdown(標(biāo)低價(jià)) parser(解析). While this does not provide any additional(附加的) features(特色), it does offer a few advantages. Notably(值得注意的), it will not try to work on highlighted(突出的) code blocks (using the CodeHilite Extension(延長(zhǎng))) like the third party library has been known to do.
The following options are provided to configure the output(輸出):
| Option | Default value | Description |
|---|---|---|
| smart_dashes | True | whether to convert(轉(zhuǎn)變) dashes |
| smart_quotes | True | whether to convert(轉(zhuǎn)變) straight quotes |
| smart_angled_quotes | False | whether to convert angled(成角的) quotes |
| smart_ellipses | True | whether to convert ellipses(橢圓形) |
| substitutions | {} | overwrite(重寫(xiě)) default substitutions(代替) |
個(gè)人看法:
注意在pelican的nest模板中,不知名的原因,第一行數(shù)據(jù),不受第二行格式的限制。 但是正常的預(yù)覽時(shí),是會(huì)有限制的。
即 [TOC]
Officially Supported ExtensionsPython-Markdown Extra縮寫(xiě)屬性列表對(duì)于塊級(jí)元素對(duì)于headers元素對(duì)于內(nèi)鏈定義列表代碼塊python code腳注表格加粗Admonition 警告CodeHiliteHeaderIdMeta-DataNew-Line-to-Break ExtensionSane ListsSmartyPantsTable of ContentsWikiLinks
會(huì)把各級(jí)標(biāo)題集合到一起,構(gòu)建成一個(gè)目錄的形式,可以快速訪問(wèn)對(duì)應(yīng)的標(biāo)題。不論標(biāo)簽的位置在文檔的何處,只要獨(dú)立成行,前后空白,就可以生成整個(gè)文章的標(biāo)題列表。
[TOC]# Header 1## Header 2->
<div class="toc"> <ul> <li><a href="#header-1">Header 1</a></li> <ul> <li><a href="#header-2">Header 2</a></li> </ul> </ul></div><h1 id="header-1">Header 1</h1><h1 id="header-2">Header 2</h1>The following options are provided to configure the output(輸出):
marker: Text to find and replace with the Table of Contents. Defaults to [TOC].
Set to an empty string to disable searching for a marker, which may save some time, especially on long documents.
title: Title to insert in the Table of Contents’ <div>. Defaults to None.
anchorlink: Set to True to cause all headers to link to themselves. Default is False.
permalink: Set to True or a string to generate(形成) permanent(永久的) links at the end of each header. Useful with Sphinx style sheets.
When set to True the paragraph symbol(象征) (? or “?”) is used as the link text. When set to a string, the provided string is used as the link text.
baselevel: Base level for headers. Defaults to 1.
The baselevel setting allows the header levels to be automatically(自動(dòng)地) adjusted(調(diào)整) to fit within the hierarchy(層級(jí)) of your HTML templates(模板). For example, suppose the Markdown text for a page should not contain any headers higher than level 3 (<h3>). The following will accomplish(完成) that:
slugify: Callable to generate(形成) anchors(錨).
Default: markdown.extensions.headerid.slugify
In order to use a different algorithm(算法) to define(定義) the id attributes(屬性), define and pass in a callable which takes the following two arguments:
* value: The string to slugify.* separator: The Word Separator.The callable must return a string appropriate(適當(dāng)?shù)? for use in HTML id attributes.
separator: Word separator. Character which replaces white space in id. Defaults to “-”.
個(gè)人看法:
permalink 這個(gè)設(shè)置有點(diǎn)意思,就像是一點(diǎn)擊,顯示的最上面一行就是這個(gè)標(biāo)題。
添加對(duì)于 WikiLinks 的支持。 [[內(nèi)容可以被轉(zhuǎn)換為鏈接]]
[[wikilink]]
內(nèi)容可以是任何大小寫(xiě)字母,數(shù)字,連接號(hào),下劃線和空格。
[[Bracketed]][[Wiki Link]]->
<a href="/Bracketed/" class="wikilink">Bracketed</a><a href="/Wiki_Link/" class="wikilink">Wiki Link</a>The default behavior is to point each link to the document root of the current domain and close with a trailing(后面的) slash. Additionally, each link is assigned to the HTML class wikilink.
The following options are provided to change the default behavior:
base_url: String to append(附加) to beginning of URL.
Default: ‘/’
end_url: String to append to end of URL.
Default: ‘/’
html_class: CSS class. Leave blank for none.
Default: ‘wikilink’
build_url: Callable which formats the URL from its parts.
使用這些配置的例子:
假如指子目錄 /wiki/ 并以 with .html 結(jié)尾。
[[WikiLink]]轉(zhuǎn)換為
<a href="/wiki/WikiLink.html" class="wikilink">WikiLink</a>支持改變或者移除類(lèi)屬型
>>> html = markdown.markdown(text,... extensions=[WikiLinkExtension(html_class='myclass')]... )->
<a href="/WikiLink/" class="myclass">WikiLink</a>也可以使用元數(shù)據(jù):
The supported meta-data keywords are:
wiki_base_urlwiki_end_urlwiki_html_classWhen used, the meta-data will override the settings provided through the extension_configs interface.
This document:wiki_base_url: http://example.com/wiki_end_url: .htmlwiki_html_class:A [[WikiLink]] in the first paragraph.would result in the following output (notice the blank wiki_html_class):個(gè)人看法:
<p>A <a >WikiLink</a> in the first paragraph.</p>個(gè)人看法:
感覺(jué)這個(gè)不好用。還是最基本的語(yǔ)法鏈接格式好一些。[]()
[^@#
|
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注