markdown公式轉爲知乎格式

    在知乎中寫技術類文章,常常會用到markdown知乎文章能夠導入markdown格式,可是不支持Latex公式。知乎大神提供了替代方案: https://zhuanlan.zhihu.com/p/69142198python

替換爲:\n<img src="https://www.zhihu.com/equation?tex=\1" alt="\1" class="ee_img tr_noresize" eeimg="1">\nmarkdown

查找目標:\$\n*(.*?)\n*\$code

替換爲:\n<img src="https://www.zhihu.com/equation?tex=\1" alt="\1" class="ee_img tr_noresize" eeimg="1">\nutf-8

    爲實現自動化,用python將其簡易實現,代碼以下:get

import re
import sys
def replace(file_name, output_file_name):
    try:
        pattern1 = r"\$\$\n*([\s\S]*?)\n*\$\$"
        new_pattern1 = r'\n<img src="https://www.zhihu.com/equation?tex=\1" alt="\1" class="ee_img tr_noresize" eeimg="1">\n'
        pattern2 = r"\$\n*(.*?)\n*\$"
        new_pattern2 =r'\n<img src="https://www.zhihu.com/equation?tex=\1" alt="\1" class="ee_img tr_noresize" eeimg="1">\n'
        f = open(file_name, 'r')
        f_output = open(output_file_name, 'w')
        all_lines = f.read()
        new_lines1 = re.sub(pattern1, new_pattern1, all_lines)
        new_lines2 = re.sub(pattern2, new_pattern2, new_lines1)
        f_output.write(new_lines2)
        # for line in all_lines:
        #     new_line1 = re.sub(pattern1, new_pattern1, line)
        #     new_line2 = re.sub(pattern2, new_pattern2, new_line1)
        #     f_output.write(new_line2)
        f.close()
        f_output.close()
    except Exception, e:
        print(e)


if __name__ == '__main__':

    if len(sys.argv) < 2:
        print("need file name")
        sys.exit(1)
    file_name = sys.argv[1]
    # file_name = "極大似然小結.md".decode('utf-8')
    file_name_pre = file_name.split(".")[0]
    output_file_name = file_name_pre + "_zhihu.md"
    replace(file_name, output_file_name)

    由此完成自動化配置。博客

本文由博客一文多發平臺 OpenWrite 發佈!it

相關文章
相關標籤/搜索