From 170c0f3080e747b9249c6794bf11453e8753e16c Mon Sep 17 00:00:00 2001 From: Emilia Date: Tue, 21 Nov 2023 23:44:01 -0500 Subject: [PATCH] Automation for image files in html --- image-fill.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 image-fill.py diff --git a/image-fill.py b/image-fill.py new file mode 100644 index 0000000..8cc7e28 --- /dev/null +++ b/image-fill.py @@ -0,0 +1,55 @@ +# This script automatically inserts img elements into +# the html file for the pictures page. + +import glob +from pathlib import Path +import argparse +from fileinput import FileInput +from sys import exit + +parser = argparse.ArgumentParser( + prog="ImgInserter", + description="Fills img elements into the html file for the pictures page" +) + +parser.add_argument("html_file", + help="HTML file to be modified") +parser.add_argument("img_dir", + help="""Directory where images are stored; + must be in the same directory as the HTML file""") + +args = parser.parse_args() + +# Find the path to the HTML file +html_path = glob.glob(args.html_file) +if len(html_path) == 0: + print("HTML file provided cannot be found") + exit(1) +html_path = Path(html_path[0]) # Shadow original html_path, now a Path + +# Find the image directory +image_dir = glob.glob(args.img_dir) +if len(image_dir) == 0: + print("Image directory cannot be found") + exit(1) +image_dir = Path(image_dir[0]) # Shadow original image_dir, now a Path + +# Find all images in the image directory +images = list(image_dir.rglob("*.png")) + \ + list(image_dir.rglob("*.jpg")) + \ + list(image_dir.rglob("*.jpeg")) + +# Generate the img tag for each file +imgs = [] +for i in images: + src = str(i) + imgs.append(f'') + +with FileInput(html_path, inplace=True) as html: + for line in html: + if "Images go below" in line: # Insert imgs here + print(line, end='') + for img in imgs: + print(img) + else: + print(line, end='')