python : convert markdown to html
程序员文章站
2022-03-08 14:57:21
...
先要安装 pip install markdown
转换脚本 md2html.py
转换脚本 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()
上一篇: 再不用markdown就落后啦