Skip to content

Instantly share code, notes, and snippets.

@AkashiSN
Last active Dec 29, 2017
Embed
What would you like to do?
Markdownの$$で囲まれたTex数式を画像に置き換えてGitHubなどで表示できるようにするスクリプト
#!/usr/bin/env python3
import sys
pattern = '<img src="https://latex.codecogs.com/png.latex?{}" />'
with open(sys.argv[1] ,"r") as f:
new_lines = ""
while True:
new_line = ""
line = f.readline()
while line.find("$$\n") >= 0:
line = f.readline()
math = line.replace(" ","&space;")[:-1]
tag = pattern.format(math)
new_line = "\n" + tag + "\n\n"
new_lines += new_line
f.readline()
line = f.readline()
new_line = ""
s = line.find("$$")
if s < 0:
new_line = line
e = -2
while s >= 0:
new_line += line[e+2:s]
e = line.find("$$",s+2)
math = line[s+2:e]
math = math.replace(" ","&space;")
if s != 0:
math = '\inline&space;' + math
tag = pattern.format(math)
new_line += tag
s = line.find("$$",e+2)
if s < 0:
new_line += line[e+2::]
new_lines += new_line
if not line:
break
with open(sys.argv[1],"w") as f:
f.write(new_lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment