Created
April 29, 2019 20:39
-
-
Save FxKu/0d747ebbf5101f777be7e55b0447b6c1 to your computer and use it in GitHub Desktop.
Fix broken image 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
#!/usr/bin/env python3 | |
import os | |
from itertools import islice | |
def searchAndAppendImgRef(img_name, img_chunk): | |
# simply search all files in source folder | |
for root, dirs, files in os.walk('/home/flydix/Dokumente/Repo/3dcitydb-docs/source'): | |
for file_name in files: | |
if file_name.endswith('.rst'): | |
with open(os.path.join(root, file_name), 'r+') as f: | |
if img_name in f.read(): | |
print ('found %s in %s' %(img_name, file_name)) | |
f.write('\n') | |
f.writelines(img_chunk) | |
return True | |
else: | |
f.close() | |
# bad luck | |
print ('Not found ' + img_name) | |
return False | |
if __name__ == '__main__': | |
with open('/home/flydix/Dokumente/Repo/3dcitydb-docs/img-links.rst', 'r') as img_list: | |
while True: | |
# 3 lines for each image | |
img_chunk = list(islice(img_list, 3)) | |
if not img_chunk: | |
img_list.close() | |
break | |
# search for |image| ref in all files and append chunk if found | |
searchAndAppendImgRef('|' + img_chunk[0].split('|')[1] + '|', img_chunk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment