From a25de5a1905b7d0a77341c9d3afef9959a5eae63 Mon Sep 17 00:00:00 2001 From: TheJoeCoder <joe@radialbog9.uk> Date: Wed, 27 Nov 2024 19:39:46 +0000 Subject: [PATCH] Change urlencode to quote (didn't read the docs) and fix replace bug --- updatecontent.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/updatecontent.py b/updatecontent.py index 75f65de..7024eb7 100644 --- a/updatecontent.py +++ b/updatecontent.py @@ -3,10 +3,10 @@ import os import logging import shutil import re -from urllib.parse import urlencode +from urllib.parse import quote # MD_IMAGE_REGEX = r'!\[(?<altText>.*)\]\s*\((?<imageURL>.+)\)|img\s*src="(?<imageURL1>[^"]*)"\s*alt="(?<altText1>[^"]*)" \/>|img\s*alt="(?<altText2>[^"]*)"\s*src="(?<imageURL2>[^"]*)" \/>' -OBSIDIAN_IMAGE_REGEX = r'!\[\[(.+\.(png|jpe?g|avif|bmp|gif|svg|webp))(?:\|[0-9]+(?:x[0-9]+)?)?\]\]' +OBSIDIAN_IMAGE_REGEX = r'!\[\[((.+\.(png|jpe?g|avif|bmp|gif|svg|webp))(?:\|[0-9]+(?:x[0-9]+)?)?)\]\]' # Initialise logging logging.basicConfig(level=logging.INFO) @@ -56,8 +56,12 @@ for filename in os.listdir(posts_target_dir): images = re.findall(OBSIDIAN_IMAGE_REGEX, content) for image in images: + # image[0] is the full match (e.g. filename.png|100x100) + # image[1] is the image name (e.g. filename.png) + # image[2] is the image extension (e.g. png) + # Get the image name - image_name = image[0] + image_name = image[1] image_path = os.path.join(images_source_dir, image_name) logger.debug(f"Processing image {image_name}") @@ -73,9 +77,9 @@ for filename in os.listdir(posts_target_dir): shutil.copy(image_path, target_image_path) # Replace the image path in the content - image_url_name = urlencode(image_name) + image_url_name = quote(image_name) md_link = f"" - content = content.replace(f"![[{image_name}]]", md_link) + content = content.replace(f"![[{image[0]}]]", md_link) # Write the updated content back to the file with open(filepath, "w") as file: -- GitLab