python-markdown用法

markdown工具,可以将txt转化成html格式。这一类工具的作用是将按一定格式写成的可读性强的文本文件
转化为结构化的标准xhtml或html。markdown最初用perl写成,后来移植到python,java,php.这里主要
介绍下python-markdown的用法。

python-markdown模块使用有两个模式,一种是做为独立的命令行,另外一种是做为python的模块使用。
1. 命令行模式
在Fedora下,命令行名称是markdown:

[ray@localhost markdown]$ markdown 
Usage: markdown INPUTFILE [options]

Options:
  -h, --help            show this help message and exit
  -f OUTPUT_FILE, --file=OUTPUT_FILE
                        write output to OUTPUT_FILE
  -e ENCODING, --encoding=ENCODING
                        encoding for input and output files
  -q, --quiet           suppress all messages
  -v, --verbose         print info messages
  -s SAFE_MODE, --safe=SAFE_MODE
                        same mode ('replace', 'remove' or 'escape'  user's
                        HTML tag)
  --noisy               print debug messages
  -x EXTENSION, --extension=EXTENSION
                        load extension EXTENSION

使用起来很简单, 直接运行markdown input.txt > output.html即可。例如:
[ray@localhost markdown]$ cat notes.txt 
Who am I?
===========
This is Ray Chen speaking.

Where am I from?
====================
I'm from SH, China. I love this place.

What's next?
=================
  * nothing
  * sleep
  * study
  * reading book


[ray@localhost markdown]$ markdown notes.txt > notes.html 
[ray@localhost markdown]$ cat notes.html 
<h1>Who am I?</h1>
<p>This is Ray Chen speaking.
</p>

<h1>Where am I from?</h1>
<p>I'm from SH, China. I love this place.
</p>

<h1>What's next?</h1>
<ul>
 <li>
     nothing
 </li>

 <li>
     sleep
 </li>

 <li>
     study
 </li>

 <li>
     reading book
 </li>

2. 模块用法
[ray@localhost markdown]$ cat test_markdown.py 
#!/usr/bin/python

import markdown
import codecs

# Open input file in read, utf-8 mode
input_file = codecs.open("notes.txt", mode="r", encoding="utf8")
text = input_file.read()
html = markdown.markdown(text)

# print
print html

# Write string html to disk
output_file = codecs.open("notes_py.html", mode="w", encoding="utf8")
output_file.write(html)

[ray@localhost markdown]$ python test_markdown.py 
<h1>Who am I?</h1>
<p>This is Ray Chen speaking.
</p>

<h1>Where am I from?</h1>
<p>I'm from SH, China. I love this place.
</p>

<h1>What's next?</h1>
<ul>
 <li>
     nothing
 </li>

 <li>
     sleep
 </li>

 <li>
     study
 </li>

 <li>
     reading book
 </li>
</ul>
[ray@localhost markdown]$ cat notes_py.html 
<h1>Who am I?</h1>
<p>This is Ray Chen speaking.
</p>

<h1>Where am I from?</h1>
<p>I'm from SH, China. I love this place.
</p>

<h1>What's next?</h1>
<ul>
 <li>
     nothing
 </li>

 <li>
     sleep
 </li>

 <li>
     study
 </li>

 <li>
     reading book
 </li>


后记:
markdown工具在很多程序中用到,比如说wordpress, pybb等,特别适合wiki, blog,forum等。

参考:
http://blog.rogerz.cn/archives/401
http://www.freewisdom.org/projects/python-markdown/



转自:http://blog.chinaunix.net/uid-7414207-id-2056022.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值