Skip to content

Instantly share code, notes, and snippets.

@qykong
Created February 28, 2018 03:30
Show Gist options
  • Save qykong/e51a2f6a2be8d34687941fd9370094d6 to your computer and use it in GitHub Desktop.
Save qykong/e51a2f6a2be8d34687941fd9370094d6 to your computer and use it in GitHub Desktop.
from urllib import parse
import re
import argparse
def return_md_image(latex_string):
return '![' + latex_string + '](https://render.githubusercontent.com/render/math?' + parse.urlencode({'math': latex_string.replace('$', '')}) + '&mode=inline)'
def convert_md(input_file, outputfile):
with open(input_file, 'r') as ifile:
content = ''.join(ifile.readlines())
p = re.compile(' (\$.*?\$)')
latexes = p.findall(content)
for ls in latexes:
content = content.replace(ls, return_md_image(ls))
with open(outputfile, 'w') as f:
f.write(content)
parser = argparse.ArgumentParser()
parser.add_argument('--input', help = 'input file path')
parser.add_argument('--output', help = 'output file path')
args = parser.parse_args()
convert_md(args.input, args.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment