Created
November 22, 2017 13:37
-
-
Save christopherkullenberg/37e63eab2dbc83503fcbac93a35c4ad9 to your computer and use it in GitHub Desktop.
Latex links to markdown links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Input: file with LaTeX links, ex. "\href{http://example.com}{example}" | |
Output: file with mardown links, ex. [example](http://example.com) | |
''' | |
import re | |
outfile = open('filewithmarkdownlinks.md', 'w') #make up a new filename | |
texfile = open('filewithlatexlinks.md', encoding='utf-8') # the file with latex links in it | |
textext = texfile.readlines() | |
for t in textext: | |
findlink = re.findall(r'(\\href.*?(?=\}))(\}\{.*?(?=\}))', t, re.IGNORECASE) | |
if findlink: | |
for f in findlink: | |
mdlink = f[1].replace('}{', '[') + ']' + f[0].replace('\href{', '(') + ')' | |
print("original" + t) | |
print(mdlink) | |
outfile.write(t.replace(f[0] + f[1], mdlink).replace('}','')) | |
else: | |
outfile.write(t) | |
outfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment