mdconverter.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:CustomContextMenu 作者: bigsinger 项目源码 文件源码
def convert_md_file(argv):
    is_convert_html = True
    if len(sys.argv) >= 3:
        is_convert_html = False

    md_filename = argv[0]
    css_filename = os.path.join(getthispath(), 'default.css')   # ??CSS????http://richleland.github.io/pygments-css/
    html_filename = '%s.html' % (md_filename[:-3])
    pdf_filename = '.'.join([md_filename.rsplit('.')[0], 'pdf'])
    css_text = None
    md_text = None

    with codecs.open(css_filename, mode='r', encoding='utf-8') as f:
        css_text = '<style type = "text/css">\n' + f.read() + "\n</style>\n"

    with codecs.open(md_filename, mode='r', encoding='utf-8') as f:
        md_text = f.read()

    # ?????????mistune
    if False:
        html_text = md2html_by_markdown2(md_text)
    else:
        global CODE_LANG
        CODE_LANG = 'java'
        markdown = mistune.Markdown(renderer=HighlightRenderer())
        html_text = markdown(md_text)

    if is_convert_html:
        # save to html file
        #?windows?encoding?????gbk??????utf-8???????
        with codecs.open(html_filename, 'w', encoding='gbk', errors='xmlcharrefreplace') as f:
            f.write(css_text + html_text)
        if os.path.exists(html_filename):
            os.popen(html_filename)
    else:
        # save to pdf file
        HTML(string=html_text).write_pdf(pdf_filename, stylesheets=[css_filename])
        if os.path.exists(pdf_filename):
            os.popen(pdf_filename)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号