Skip to content

Instantly share code, notes, and snippets.

@AkashiSN
Last active December 29, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkashiSN/03429beb9eefe910cf8a662b5a3f101b to your computer and use it in GitHub Desktop.
Save AkashiSN/03429beb9eefe910cf8a662b5a3f101b to your computer and use it in GitHub Desktop.
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