Skip to content

Instantly share code, notes, and snippets.

@adammichaelwood
Created October 9, 2017 21:59
Show Gist options
  • Save adammichaelwood/530c9a6add9a4b25e47882f1888d4bed to your computer and use it in GitHub Desktop.
Save adammichaelwood/530c9a6add9a4b25e47882f1888d4bed to your computer and use it in GitHub Desktop.
Adds image filename extensions to Sphinx image directives that previously used the wildcard syntax
import os, re
# make a dict of names:extensions
img_exts = dict()
for _,_,f in os.walk("img"):
for name in f:
n, x = name.split(".")
img_exts[n] = x
rst_ext = re.compile('\.rst\Z')
img_ref = re.compile('(img\/[a-z0-9\-]*\/([a-z0-9\-]*)\.)\*')
def replace_extension(m):
extension = img_exts[m.group(2)]
return "".join([m.group(1), extension])
for item in os.listdir("."):
if rst_ext.search(item):
with open(item, 'r') as file:
filedata = file.read()
filedata = img_ref.sub(replace_extension, filedata)
# Write the file out again
with open(item, 'w') as file:
file.write(filedata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment