`
mobeiheart
  • 浏览: 5469 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

markdown模块学习笔记

阅读更多
Markdown是个将普通文本转换成HTML4或XHTML标记语言的模块,在很多web应用上都能有很大的用处。
下载和安装于其他一般模块无异,稍旧版本需要依赖ElementTree模块。

command模式下使用markdown

导入markdown模块

>>> from markdown import markdown


输出段落

>>> markdown("hello")
u'hello'


输出标题

>>> markdown("#This is the headline")
u'This is the headline'
>>> markdown("##Subtitle")
u'Subtitle'
>>> h1="""
another h1
=====""" #any mumber of '=' will be ok
>>> markdown(h1)
u'<h1>another h1</h1>'
>>>h2="""
another h2
----""" #any mumber of '-' will be ok
>>> markdown(h2)
u'<h2>another h2</h2>'


输出超链接/图片链接
>>> markdown("[click here](http://xxoo.ws 'link')")
u'<p><a href="http://xxoo.ws" title="link">click here</a></p>'
>>> markdown("![img link](http://xxoo.ws/logo.png 'logo')")
u'<p><img alt="img link" src="http://xxoo.ws/logo.png" title="logo" /></p>'


输出引用
>>> quotetext="""
>blockquote
>line 1
>line 2
"""
>>> markdown(quotetext)
u'<blockquote>\n<p>blockquote\nline 1\nline 2</p>\n</blockquote>'


输出列表
>>> lists="""
* one
* two
* three
"""
>>> markdown(lists)
u'<ul>\n<li>one</li>\n<li>two</li>\n<li>three</li>\n</ul>'


'*'可用'+''-''1.''2.'代替,注意符号与内容之间的空格

>>> lists="""
1. one
1. two
1. three
"""
>>> markdown(lists)
u'<ol>\n<li>one</li>\n<li>two</li>\n<li>three</li>\n</ol>'


输出代码块
>>> code="""
code:

    code here
"""
>>> markdown(code)
u'<p>code:</p>\n<pre><code>code here\n</code></pre>'


代码块内容与前面顶内容有一行的间距,而且行头有4个空格或一个制表符

编码Email链接
>>> markdown("<address@example.com>")
u'<p><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#97;&#100;&#100;&#114;&#101;&#115;&#115;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;">&#97;&#100;&#100;&#114;&#101;&#115;&#115;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;</a></p>'



参考链接:
http://daringfireball.net/projects/markdown/syntax#html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics