Last active
November 2, 2016 14:39
-
-
Save erikbern/5e81bf7d68e9deab9c55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os, sys, re | |
patterns = [ | |
# <img src='http://s.wordpress.com/latex.php?latex=%5Cmathcal%7BO%7D%28n%29&bg=T&fg=000000&s=0' alt='\mathcal{O}(n)' title='\mathcal{O}(n)' class='latex' /> | |
(r'<img.*?title=\'(.*?)\' class=\'latex\' />', | |
'$$ \\1 $$ ', | |
0), | |
# [<img class=" size-full wp-image-1722 aligncenter" src="http://erikbern.com/wp-content/uploads/2016/01/avg.png" alt="avg" width="512" height="512" />](http://erikbern.com/wp-content/uploads/2016/01/avg.png) | |
(r'\[(<img.*?)\]\(.*?\)', | |
'\\1', | |
0), | |
# <img class="alignnone size-full wp-image-600" title="RNN 1" src="http://45.55.214.245/wp-content/uploads/2014/06/RNN-1.png" alt="" width="741" height="187" /> | |
(r'<img.*?src="(.*?)".*?/>', | |
'', | |
0), | |
# <figcaption class="wp-caption-text">blah blah</figcaption></figure> | |
(r'<figcaption.*?>(.*?)</figcaption></figure>', | |
'*\\1*', | |
re.DOTALL), | |
# gists | |
(r'<div class="oembed-gist">.*?href="https://gist.github.com/(.*?)".*?</div>', | |
'{% gist \\1 %}', | |
0), | |
# encoding issue | |
(r'\xc2\xa0', | |
' ', | |
0), | |
# italics | |
(r' _([^_]*?) _', | |
' _\\1_ ', | |
0), | |
# emphasis | |
(r' \*\*([^*]*?) \*\*', | |
' **\\1** ', | |
0), | |
# links | |
(r'http://45.55.214.245', | |
'', | |
0), | |
# pipes (for mathjax | |
(r'\|', | |
'\mid', | |
0) | |
] | |
for root, subfolders, files in os.walk(sys.argv[1]): | |
for fn in files: | |
fn = os.path.join(root, fn) | |
output = [] | |
with open(fn) as f: | |
s = f.read() | |
for pattern, replacement, flags in patterns: | |
s2 = re.sub(pattern, replacement, s, 0, flags) | |
if s2 != s: | |
print fn, pattern | |
s = s2 | |
f = open(fn, 'w') | |
f.write(s) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment