`
belldeep
  • 浏览: 39161 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

python : convert markdown to html

阅读更多
先要安装 pip install markdown
转换脚本 md2html.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 使用方法 python md2html.py  filename
import os, sys
import markdown
import codecs

head = """<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
code {
  color: inherit;
  background-color: rgba(0, 0, 0, 0.05);
}
</style>
</head>
<body>
"""

foot = """
</body>
</html>
"""

if len(sys.argv) ==2:
    fname = sys.argv[1]
else:
    print 'usage: md2html.py fname '
    sys.exit(1)

f1 = '%s.md' % (fname)
if not os.path.exists(f1):
    print 'Error: %s not found\n' % f1
    sys.exit(1)

fp1 = codecs.open(f1, mode="r", encoding="utf-8")
text = fp1.read()
html = markdown.markdown(text)
fp1.close()

f2 = '%s.html' % (fname)
fp2 = codecs.open(f2, "w",encoding="utf-8",errors="xmlcharrefreplace")
fp2.write(head+html+foot)
fp2.close()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics