Created
October 9, 2017 21:59
-
-
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
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, 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