Pythonには、Daring Fireball: Markdownを実装したMarkdownというパッケージがあります。
使い方は以下のようになります。
importmarkdownprintmarkdown.markdown("input")
extensionsで拡張できます。
importmarkdownextensions=["fenced_code","tables"]printmarkdown.markdown("input",extensions)
Extensions — Python Markdown
ここに書いてあるExtensionsはデフォルトで利用可能です。このドキュメントには指定するのに'markdown.extensions.footnotes'と書いていますが、単に'footnotes'だけでも使えます。
おすすめ設定(GFM的に使える)
delタグとinsタグを使うために以下のパッケージをインストールします。
$ pip install mdx_del_ins
del_insがExtensionで使えますので以下のように設定します。
importmarkdownextensions=['extra','admonition','codehilite(css_class=highlight)','nl2br','sane_lists','toc','del_ins']printmarkdown.markdown("input",extensions)
GFMライクに使えます。
Pelicanで使用する場合には、pelicanconf.pyに以下のように書きます。
MD_EXTENSIONS = ['extra', 'admonition', 'codehilite(css_class=highlight)','nl2br', 'sane_lists', 'toc', 'del_ins']
この記事もこの設定で書いています。
標準のExtensionリファレンス
Extensions — Python Markdown
以下は上記のドキュメントを簡単にまとめました。興味のある方どうぞ。
目次
色々含まれているので1つずつ見てみます。
Abbreviations (abbr)
Abbreviations Extension — Python Markdown
略語tag(\<abbr>)を追加できます。以下に例を示します。
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.
(正直余り使わないし、調べるまで知りませんでした)
Attribute Lists (attr_list)
Attribute Lists Extension — Python Markdown
classやidなどのattributeを追加できます。便利そう。
This is a paragraph.
{: #an_id .a_class }
A setext style header {: #setext}
=================================
### A hash style header ### {: #hash }
[link](http://example.com){: class="foo bar" title="Some title!" }
blockに対して, inlineに対して両方使えます。
cssセレクタぽく書けます。ダブルクォーテーション("")で囲むことで複数指定もできます。
{: #someid .someclass somekey='some value' }
{: #id1 .class1 id=id2 class="class2 class3" .class4 }
Definition Lists (def_list)
Definition Lists Extension — Python Markdown
dl>dt>ddを書けます。
Apple:PomaceousfruitofplantsofthegenusMalusinthefamilyRosaceae.Orange:ThefruitofanevergreentreeofthegenusCitrus.
- Apple
- Pomaceous fruit of plants of the genus Malus in
the family Rosaceae. - Orange
- The fruit of an evergreen tree of the genus Citrus.
Fenced Code Blocks (fenced_code)
Fenced Code Blocks Extension — Python Markdown
```で書こうとコードブロックになります。デフォルトの機能にしてほしいくらいです。
このExtensionは指定行だけハイライトするということもできるみたいです。以下のように指定します。
こんな感じになります。
$catsample.py# cat result# code block [python]importantigravity
Footnotes Extension — Python Markdown
脚注です。
Footnotes[^1] have a label[^@#$%] and the footnote's content.
[^1]: This is a footnote content.
[^@#$%]: A footnote on the label: "@#$%".
Footnotes have a label and the footnote's content.
脚注内に複数行を入れることもできます。
[^1]:
The first paragraph of the definition.
Paragraph two of the definition.
> A blockquote with> multiple lines.
a code block
A final paragraph.
Tables (tables)
Tables Extension — Python Markdown
テーブルが使えます。
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
First Header | Second Header |
---|
Content Cell | Content Cell |
Content Cell | Content Cell |
Smart_Strong (smart_strong)
Smart Strong Extension — Python Markdown
賢く強調してくれます。
>>>importmarkdown>>>markdown.markdown('Text with double__underscore__words.', \extensions=['markdown.extensions.smart_strong'])u'<p>Text with double__underscore__words.</p>'>>>markdown.markdown('__Strong__ still works.', \extensions=['markdown.extensions.smart_strong'])u'<p><strong>Strong</strong> still works.</p>'>>>markdown.markdown('__this__works__too__.', \extensions=['markdown.extensions.smart_strong'])u'<p><strong>this__works__too</strong>.</p>'
Admonition (admonition)
Admonition Extension — Python Markdown
reStructuredTextにあるAdminisions(reStructuredText Directives)のMarkdown版を使えます。
!!! note
You should note that the title will be automatically capitalized.
!!! warning "WARNING"
...
!!! important ""
This is a admonition box without a title.
Note
You should note that the title will be automatically capitalized.
This is a admonition box without a title.
CSSをいい感じに書いておくと良いです。
CodeHilite (codehilite)
CodeHilite Extension — Python Markdown
css_classを指定しないとうまくハイライトされませんでした。
extensions=['codehilite(css_class=highlight)']
HeaderId Extension — Python Markdown
headerにheader_<数字>というIDをつけてくれます。
Meta-Data Extension — Python Markdown
以下の様にメタデータを定義できるようになります。
Title:MyDocumentSummary:Abriefdescriptionofmydocument.Authors:WaylanLimbergJohnDoeDate:October2,2007blank-value:base_url:http://example.comThisisthefirstparagraphofthedocument.
New-Line-to-Break Extension (nl2br)
New Line to Break Extension — Python Markdown
以下の例に示すように、改行が新しい行になるようにパースされます。
>>>importmarkdown>>>text="""... Line 1... Line 2... """>>>html=markdown.markdown(text,extensions=['markdown.extensions.nl2br'])>>>printhtml<p>Line1<br/>Line2</p>
Sane Lists (sane_lists)
Sane Lists Extension — Python Markdown
listを賢くパースしてくれます。
- Ordered item 1
- Ordered item 2
- Unordered item 1
- Unordered item 2
SmartyPants (smarty)
SmartyPants Extension — Python Markdown
文字参照を置き換えてくれます。詳しくは以下を参照。
Table of Contents (toc)
Table of Contents Extension — Python Markdown
目次を表示します。
WikiLinks
WikiLinks Extension — Python Markdown
内部参照が使えるようになります。
パースすると以下のようになります。
<ahref="/Bracketed/"class="wikilink">Bracketed</a>