generated from Enveloppe/mkdocs-publisher-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_unused_media.py
46 lines (36 loc) · 1.56 KB
/
find_unused_media.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
from pathlib import Path
import os
import argparse
def find_unused_media(img_path: Path, dry_run: bool = False):
docs_dir = Path(Path.cwd(), 'docs')
#list images of assets
assets_dir = Path(docs_dir, img_path)
images = [file for file in assets_dir.rglob('*') if file.is_file() and file.suffix in ['.png', '.jpg', '.jpeg', '.gif', '.svg']]
md_files = [file for file in docs_dir.rglob('*.md') if file.is_file()]
# Search for images in markdown files
used_images = []
for md_file in md_files:
for image in images:
with open(md_file, 'r', encoding='utf-8') as f:
if image.name in f.read():
used_images.append(image)
# compare the two lists
unused_images = [image for image in images if image not in used_images]
# delete unused images
if unused_images:
print(f'Found {len(unused_images)} unused images in {assets_dir}. Deleting...')
for image in unused_images:
if not dry_run:
print(image)
os.remove(image)
else:
print(f'Would delete {image}')
else:
print(f'Found no unused images in {assets_dir}.')
if __name__ == '__main__':
# use argparse to get the path to the assets folder
parser = argparse.ArgumentParser()
parser.add_argument('img_path', type=str, help='Path to the assets folder')
parser.add_argument('--dry-run', action='store_true', help='Do not delete unused images')
args = parser.parse_args()
find_unused_media(Path(args.img_path), args.dry_run)