MarkDown 数学公式转知乎格式。
版权声明:本文为 DLonng 原创文章,可以随意转载,但必须在明确位置注明出处!
感谢知乎(@阿飞)的 python 代码 math2zhihu.py
:
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)
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_pre = file_name.split(".")[0]
output_file_name = file_name_pre + "_zhihu.md"
replace(file_name, output_file_name)
使用方法:
python math2zhihu.py 待转换的文档.md
脑子是个好东西,可惜我没有😂!
本文原创首发于微信公号「登龙」,分享机器学习、算法编程、Python、机器人技术等原创文章,扫码即可关注!
DLonng at 03/23/20