免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5677 | 回复: 0
打印 上一主题 下一主题

python-markdown用法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-22 14:09 |只看该作者 |倒序浏览
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
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

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
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]$ cat notes_py.html
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

后记:
markdown工具在很多程序中用到,比如说wordpress, pybb等,特别适合wiki, blog,forum等。
参考:
http://blog.rogerz.cn/archives/401
http://www.freewisdom.org/projects/python-markdown/
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/68938/showart_2034448.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP