Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions build/image_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
import os


def scan_file(path: str) -> int:
def scan_file(path: str, verbose: bool = True) -> list:
"""Scans a file for all `image` shortcodes.

Args:
path (str): Path to file.
verbose (bool, optional): If True, prints the shortcodes found. Defaults to True.

Returns:
(int) Number of shortcodes found.
(list) A list of all image shortcodes found.
"""

img_list = []
img_names = []

with open(path, encoding="utf_8") as md_file:
text = md_file.read()
Expand All @@ -26,16 +28,18 @@ def scan_file(path: str) -> int:
text, lambda t: t.tag == "image"
):
img_list.append((img, pos_info))
img_names.append(img.named_params["filename"])

if len(img_list) > 0:
if len(img_list) > 0 and verbose:
print(f"File '{path}':")

for img in img_list:
print(
f" Line {img[1].line}: '{img[0].named_params['filename']}'"
)

return len(img_list)
# return len(img_list)
return img_names


parser = argparse.ArgumentParser(
Expand All @@ -44,20 +48,40 @@ def scan_file(path: str) -> int:
)

parser.add_argument("pathname", help="Path of the folder to scan")
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")

args = parser.parse_args()

print(f"Scanning '{args.pathname}'")

num_found = 0
all_images_found = []

for root, dirs, files in os.walk(args.pathname):
for file in files:
if file.endswith(".md"):
fullpath = os.path.join(root, file)
num_found += scan_file(fullpath)
images_found = scan_file(fullpath, verbose=args.find_unused is None)
num_found += len(images_found)
all_images_found.extend(images_found)

if num_found == 0:
print(f"No image shortcodes found in '{args.pathname}'")
else:
print(f"Found {num_found} image shortcodes.")

if args.find_unused and num_found > 0:
unique_images = list(set(all_images_found))
print(f"Found {len(unique_images)} unique images in '{args.pathname}'.")
print(f"Checking for images not found in '{args.pathname}' that are in '{args.find_unused[0]}'")

unused_images = []

for root, dirs, files in os.walk(args.find_unused[0]):
for file in files:
if (file.endswith(".png") or file.endswith(".jpg") or file.endswith(".webp")) and not any(file in img for img in unique_images):
img_filepath = os.path.join(root, file)
print(f" Image '{img_filepath}' not found in '{args.pathname}'")
unused_images.append(img_filepath)

print(f"Found {len(unused_images)} unused images.")