-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreorganizeimages.py
56 lines (52 loc) · 2.55 KB
/
reorganizeimages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import re
import os
from pathlib import Path
from shutil import copyfile
import urllib.parse
import fileinput
imagesFolder = r'assets/'
modulesFolder = r'modules/'
for dirpath, dirs, files in os.walk(modulesFolder):
# print(dirpath)
for f in files:
fname = os.path.join(dirpath,f)
if fname.endswith('.adoc'):
print(fname)
parentFolder = os.path.dirname(os.path.dirname(fname))
# we don't need to create an images folder in the modules folder
if not parentFolder.endswith('modules'):
# see if the images folder already exists and create it if not
try:
newImagesFolder = os.path.join(parentFolder, "images")
os.mkdir(newImagesFolder)
except:
# print(newImagesFolder, "already exists")
pass
# actual text replacement and image moving
with fileinput.input(fname, inplace=True) as f:
for line in f:
image = re.search(r'image::(.*)\[(.*)]', line)
iimage = re.search(r'image:(.*)\[(.*)]', line)
if image:
# get the relative path
imagePath = image.group(1)
imageName = re.search(r'/assets/(.*\..*)', imagePath)
if imageName:
imageName = imageName.group(1)
line = (re.sub(r'image::(.*)\[(.*)]', r'image::'+imageName+r'[\2]', line))
elif iimage:
imagePath = iimage.group(1)
imageName = re.search(r'/assets/(.*\..*)', imagePath)
if imageName:
imageName = imageName.group(1)
line = (re.sub(r'image:(.*)\[(.*)]', r' image:'+imageName+r'[\2] ', line))
if image or iimage:
if imageName:
# find the image in the 'assets' folder
sourceImage = os.path.join("assets", imageName)
# strip special characters
sourceImage = urllib.parse.unquote(sourceImage)
destinationImage = os.path.join(newImagesFolder, imageName)
destinationImage = urllib.parse.unquote(destinationImage)
copyfile(sourceImage, destinationImage)
print('{}'.format(line), end='')