标签:cap 编辑 float 还需要 lin proc 有道 erp ror
有很多编辑器可以直接将markdown转换成html,为什么还要自己写呢?因为我想写完markdown之后,即可以保存在笔记软件中(比如有道),又可以放到github进行版本管理,还可以发布到博客(比如博客园)。这些如果都操作一遍,是很繁琐的,所以必须交给脚本去做。
使用mistune(源码很有学习价值)。需要自己引入pygments模块渲染代码块,官网有参考例子。
import mistune
import sys
import codecs
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import html
class HighlightRenderer(mistune.Renderer):
def block_code(self, code, lang):
if not lang:
return ‘\n<pre><code>%s</code></pre>\n‘ % mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter()
return highlight(code, lexer, formatter)
def main(argv):
name = argv[0]
input_file = codecs.open(name, mode=‘r‘, encoding=‘utf-8‘)
text = input_file.read()
renderer = HighlightRenderer()
markdown = mistune.Markdown(renderer=renderer)
html = markdown(text)
html_name = ‘%s.html‘ % (name[:-3])
output_file = codecs.open(
html_name, ‘w‘, encoding=‘utf-8‘, errors=‘xmlcharrefreplace‘)
output_file.write(html)
if __name__ == "__main__":
main(sys.argv[1:])
上面代码还不能使代码着色,因为没有指定css,还需要在生成的html头中加入css,不同的css文件可以在http://richleland.github.io/pygments-css/找到。
<style type = "text/css">
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #60a0b0; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #007020; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #007020 } /* Comment.Preproc */
.highlight .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */
.highlight .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #808080 } /* Generic.Output */
.highlight .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
.highlight .kc { color: #007020; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #007020 } /* Keyword.Pseudo */
.highlight .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #902000 } /* Keyword.Type */
.highlight .m { color: #40a070 } /* Literal.Number */
.highlight .s { color: #4070a0 } /* Literal.String */
.highlight .na { color: #4070a0 } /* Name.Attribute */
.highlight .nb { color: #007020 } /* Name.Builtin */
.highlight .nc { color: #0e84b5; font-weight: bold } /* Name.Class */
.highlight .no { color: #60add5 } /* Name.Constant */
.highlight .nd { color: #555555; font-weight: bold } /* Name.Decorator */
.highlight .ni { color: #d55537; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #007020 } /* Name.Exception */
.highlight .nf { color: #06287e } /* Name.Function */
.highlight .nl { color: #002070; font-weight: bold } /* Name.Label */
.highlight .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #062873; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #bb60d5 } /* Name.Variable */
.highlight .ow { color: #007020; font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #40a070 } /* Literal.Number.Float */
.highlight .mh { color: #40a070 } /* Literal.Number.Hex */
.highlight .mi { color: #40a070 } /* Literal.Number.Integer */
.highlight .mo { color: #40a070 } /* Literal.Number.Oct */
.highlight .sb { color: #4070a0 } /* Literal.String.Backtick */
.highlight .sc { color: #4070a0 } /* Literal.String.Char */
.highlight .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #4070a0 } /* Literal.String.Double */
.highlight .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #4070a0 } /* Literal.String.Heredoc */
.highlight .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */
.highlight .sx { color: #c65d09 } /* Literal.String.Other */
.highlight .sr { color: #235388 } /* Literal.String.Regex */
.highlight .s1 { color: #4070a0 } /* Literal.String.Single */
.highlight .ss { color: #517918 } /* Literal.String.Symbol */
.highlight .bp { color: #007020 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #bb60d5 } /* Name.Variable.Class */
.highlight .vg { color: #bb60d5 } /* Name.Variable.Global */
.highlight .vi { color: #bb60d5 } /* Name.Variable.Instance */
.highlight .il { color: #40a070 } /* Literal.Number.Integer.Long */
</style>
所以完整的代码应该为:
import mistune
import sys
import codecs
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import html
class HighlightRenderer(mistune.Renderer):
def block_code(self, code, lang):
if not lang:
return ‘\n<pre><code>%s</code></pre>\n‘ % mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter()
return highlight(code, lexer, formatter)
def main(argv):
md_name = argv[0]
with codecs.open(md_name, mode=‘r‘, encoding=‘utf-8‘) as mdfile:
with codecs.open("friendly.css",mode = ‘r‘,encoding = ‘utf-8‘) as cssfile:
md_text = mdfile.read()
css_text = cssfile.read()
renderer = HighlightRenderer()
markdown = mistune.Markdown(renderer=renderer)
html_text = markdown(md_text)
html_name = ‘%s.html‘ % (md_name[:-3])
with codecs.open(html_name, ‘w‘, encoding=‘utf-8‘, errors=‘xmlcharrefreplace‘) as output_file:
output_file.write(css_text + html_text)
if __name__ == "__main__":
if len(sys.argv) == 2:
main(sys.argv[1:])
else:
print("Error:please specify markdown file path")
friendly.css文件中存放之前的css文件。
同样使用markdown2的代码如下:
import markdown2
import codecs
import sys
def main(argv):
md_name = argv[0]
with codecs.open(md_name, mode=‘r‘, encoding=‘utf-8‘) as mdfile:
with codecs.open("friendly.css", mode=‘r‘, encoding=‘utf-8‘) as cssfile:
md_text = mdfile.read()
css_text = cssfile.read()
extras = [‘code-friendly‘, ‘fenced-code-blocks‘, ‘footnotes‘]
html_text = markdown2.markdown(md_text, extras=extras)
html_name = ‘%s.html‘ % (md_name[:-3])
with codecs.open(html_name, ‘w‘, encoding=‘utf-8‘, errors=‘xmlcharrefreplace‘) as output_file:
output_file.write(css_text + html_text)
if __name__ == "__main__":
if len(sys.argv) == 2:
main(sys.argv[1:])
else:
print("Error:please specify markdown file path")
标签:cap 编辑 float 还需要 lin proc 有道 erp ror
原文地址:http://www.cnblogs.com/WeyneChen/p/6339965.html