Skip to content

Instantly share code, notes, and snippets.

@erikbern
Last active November 2, 2016 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikbern/5e81bf7d68e9deab9c55 to your computer and use it in GitHub Desktop.
Save erikbern/5e81bf7d68e9deab9c55 to your computer and use it in GitHub Desktop.
import os, sys, re
patterns = [
# <img src='http://s.wordpress.com/latex.php?latex=%5Cmathcal%7BO%7D%28n%29&#038;bg=T&#038;fg=000000&#038;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="(.*?)".*?/>',
'![image](\\1)',
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