????在知乎中寫(xiě)技術(shù)類(lèi)文章宾袜,經(jīng)常會(huì)用到markdown知乎文章可以導(dǎo)入markdown格式聘鳞,但是不支持Latex公式说搅。知乎大神提供了替代方案: https://zhuanlan.zhihu.com/p/69142198
替換為:
\n<img src="https://www.zhihu.com/equation?tex=\1" alt="\1" class="ee_img tr_noresize" eeimg="1">\n
查找目標(biāo):
\$\n*(.*?)\n*\$
替換為:
\n<img src="https://www.zhihu.com/equation?tex=\1" alt="\1" class="ee_img tr_noresize" eeimg="1">\n
????為實(shí)現(xiàn)自動(dòng)化鹊碍,用python將其簡(jiǎn)易實(shí)現(xiàn)穆役,代碼如下:
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 = "極大似然小結(jié).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)
????由此完成自動(dòng)化配置。
本文由博客一文多發(fā)平臺(tái) OpenWrite 發(fā)布旁蔼!