7
7
import os
8
8
9
9
10
- def scan_file (path : str ) -> int :
10
+ def scan_file (path : str , verbose : bool = True ) -> list :
11
11
"""Scans a file for all `image` shortcodes.
12
12
13
13
Args:
14
14
path (str): Path to file.
15
+ verbose (bool, optional): If True, prints the shortcodes found. Defaults to True.
15
16
16
17
Returns:
17
- (int) Number of shortcodes found.
18
+ (list) A list of all image shortcodes found.
18
19
"""
19
20
20
21
img_list = []
22
+ img_names = []
21
23
22
24
with open (path , encoding = "utf_8" ) as md_file :
23
25
text = md_file .read ()
@@ -26,16 +28,18 @@ def scan_file(path: str) -> int:
26
28
text , lambda t : t .tag == "image"
27
29
):
28
30
img_list .append ((img , pos_info ))
31
+ img_names .append (img .named_params ["filename" ])
29
32
30
- if len (img_list ) > 0 :
33
+ if len (img_list ) > 0 and verbose :
31
34
print (f"File '{ path } ':" )
32
35
33
36
for img in img_list :
34
37
print (
35
38
f" Line { img [1 ].line } : '{ img [0 ].named_params ['filename' ]} '"
36
39
)
37
40
38
- return len (img_list )
41
+ # return len(img_list)
42
+ return img_names
39
43
40
44
41
45
parser = argparse .ArgumentParser (
@@ -44,20 +48,40 @@ def scan_file(path: str) -> int:
44
48
)
45
49
46
50
parser .add_argument ("pathname" , help = "Path of the folder to scan" )
51
+ parser .add_argument ("--find-unused" , nargs = 1 , help = "Prints out all images in IMAGEFOLDER that are not found in pathname. Set pathname to the top content folder to scan all content for images." , metavar = "IMAGEFOLDER" )
47
52
48
53
args = parser .parse_args ()
49
54
50
55
print (f"Scanning '{ args .pathname } '" )
51
56
52
57
num_found = 0
58
+ all_images_found = []
53
59
54
60
for root , dirs , files in os .walk (args .pathname ):
55
61
for file in files :
56
62
if file .endswith (".md" ):
57
63
fullpath = os .path .join (root , file )
58
- num_found += scan_file (fullpath )
64
+ images_found = scan_file (fullpath , verbose = args .find_unused is None )
65
+ num_found += len (images_found )
66
+ all_images_found .extend (images_found )
59
67
60
68
if num_found == 0 :
61
69
print (f"No image shortcodes found in '{ args .pathname } '" )
62
70
else :
63
71
print (f"Found { num_found } image shortcodes." )
72
+
73
+ if args .find_unused and num_found > 0 :
74
+ unique_images = list (set (all_images_found ))
75
+ print (f"Found { len (unique_images )} unique images in '{ args .pathname } '." )
76
+ print (f"Checking for images not found in '{ args .pathname } ' that are in '{ args .find_unused [0 ]} '" )
77
+
78
+ unused_images = []
79
+
80
+ for root , dirs , files in os .walk (args .find_unused [0 ]):
81
+ for file in files :
82
+ if (file .endswith (".png" ) or file .endswith (".jpg" ) or file .endswith (".webp" )) and not any (file in img for img in unique_images ):
83
+ img_filepath = os .path .join (root , file )
84
+ print (f" Image '{ img_filepath } ' not found in '{ args .pathname } '" )
85
+ unused_images .append (img_filepath )
86
+
87
+ print (f"Found { len (unused_images )} unused images." )
0 commit comments