-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplace_md_image.py
executable file
·127 lines (116 loc) · 4.45 KB
/
replace_md_image.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# usage: python3 replace_md_image.py markdownFilesDir
# function: update the ![](http) with ![](filename.assets/picname)
# this will download the file automatically.
import re
import os
import sys
import io
from shutil import copy
import requests
# the images in Markdown: ![img]()
# group 1 is the url or filename
global imgPattern
imgPattern = r'!\[.*\]\((.*)\)'
backupName = 'markdownBackup'
# find all the files in filenameList with .md extension
def getMarkdownFileList(directoryName):
res = []
for filename in os.listdir(directoryName):
fileWithPath = os.path.join(directoryName, filename)
if os.path.isdir(fileWithPath) and filename != backupName:
res.extend(getMarkdownFileList(fileWithPath))
elif os.path.isfile(fileWithPath) and fileWithPath.endswith('.md'):
res.append(fileWithPath)
return res
# copy src to dest
# return the dest with the filename
def copyFile(src: str, dest: str):
try:
copy(src, dest)
except IOError:
print(f"fail to copy {src} to {dest}")
return None
except:
print(f"fail to copy {src} to {dest}")
return None
return os.path.join(dest, os.path.basename(src))
# download from url to dir
# return the downloaded filename without path
def download(url: str, dir: str) -> str:
r = requests.get(url)
# get original filename
dest = os.path.join(dir, url.split('/')[-1])
# remove parameters
endPos = dest.rfind('?')
if endPos == -1:
endPos = len(dest)
dest = dest[0:endPos]
with open(dest, "wb") as f:
f.write(r.content)
return dest
# download from url and substitude with local filename
def processMarkdownFile(markdownFileList):
for markdownFilename in markdownFileList:
# markdownFilename.assets directory
dirName = os.path.dirname(markdownFilename)
dirName = os.path.join(dirName, os.path.splitext(markdownFilename)[0])
dirName += '.assets'
# open with utf-8
with io.open(markdownFilename, 'r', encoding='utf-8') as f:
imageDir = os.path.splitext(os.path.basename(markdownFilename))[0] + '.assets'
numReplace = 0
content = f.read()
matches = re.compile(imgPattern).findall(content)
if matches == None or len(matches) == 0:
continue
for match in matches:
if match == None or len(match) == 0:
continue
newPath = None
# only download url started with http, this will download https too.
print(match)
if match[0:4] != 'http':
continue
if not os.path.exists(dirName):
os.makedirs(dirName)
newPath = download(match, dirName)
relativePath = os.path.join(imageDir, os.path.basename(newPath))
# substitude url with local path
content = content.replace(match, relativePath)
numReplace += 1
if content and numReplace > 0:
io.open(markdownFilename, 'w', encoding='utf-8').write(content)
print(f'{os.path.basename(markdownFilename)}: {numReplace} URL(s) substituded.')
elif numReplace == 0:
print(f'{os.path.basename(markdownFilename)} has nothing to be substituded.')
# backup files before substitution
def backup(mdFilenames) -> bool:
for mdFilename in mdFilenames:
dest = os.path.join(os.path.dirname(mdFilename), backupName)
print(f'backup {mdFilename} to {dest}')
if not os.path.exists(dest):
os.makedirs(dest)
if not copyFile(mdFilename, dest):
return False
return True
if __name__ == '__main__':
argc = len(sys.argv)
if argc != 2:
print('usage: python3 replace_md_image.py <markdownFilesDir>')
exit(1)
if not os.path.exists(sys.argv[1]):
print(f'{sys.argv[1]} does not exist.')
exit(1)
os.chdir(sys.argv[1])
mdFilenames = getMarkdownFileList(os.getcwd())
print(mdFilenames)
print('-----------------backup------------------')
if not backup(mdFilenames):
print('-------------backup failed---------------')
exit(1)
print('--------------backup succeed-------------')
print('\n')
print('----------------replace------------------')
processMarkdownFile(mdFilenames)
print('------------replace succeed--------------')