• markdown公式转为知乎格式


        在知乎中写技术类文章,经常会用到markdown知乎文章可以导入markdown格式,但是不支持Latex公式。知乎大神提供了替代方案: https://zhuanlan.zhihu.com/p/69142198

    替换为: <img src="https://www.zhihu.com/equation?tex=1" alt="1" class="ee_img tr_noresize" eeimg="1">

    查找目标:$ *(.*?) *$

    替换为: <img src="https://www.zhihu.com/equation?tex=1" alt="1" class="ee_img tr_noresize" eeimg="1">

        为实现自动化,用python将其简易实现,代码如下:

    import re
    import sys
    def replace(file_name, output_file_name):
        try:
            pattern1 = r"$$
    *([sS]*?)
    *$$"
            new_pattern1 = r'
    <img src="https://www.zhihu.com/equation?tex=1" alt="1" class="ee_img tr_noresize" eeimg="1">
    '
            pattern2 = r"$
    *(.*?)
    *$"
            new_pattern2 =r'
    <img src="https://www.zhihu.com/equation?tex=1" alt="1" class="ee_img tr_noresize" eeimg="1">
    '
            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 发布!

  • 相关阅读:
    Linux删除文件相关命令
    Bing语句
    VS2013配置Winpcap
    node10-mongoose
    node09-cookie
    node08-express
    node07-http
    node06-path
    node05-fs
    node04-buffer
  • 原文地址:https://www.cnblogs.com/moonwanderer/p/11692332.html
Copyright © 2020-2023  润新知