2024-10-19 13:03:04 +01:00
|
|
|
import os
|
|
|
|
import re
|
2024-11-11 15:49:01 +00:00
|
|
|
|
2024-10-19 13:03:04 +01:00
|
|
|
from termcolor import colored
|
|
|
|
|
|
|
|
|
2025-01-16 07:05:31 +00:00
|
|
|
def process_line(line):
|
|
|
|
image_rgx = r"!\[.*?\]\((.*?)\)"
|
|
|
|
internal_link_rgx = r"\[.*?\]\((.*?)\)"
|
|
|
|
img_links = re.findall(image_rgx, line)
|
|
|
|
internal_links = re.findall(internal_link_rgx, line)
|
|
|
|
|
|
|
|
if img_links:
|
|
|
|
for img_link in img_links:
|
|
|
|
stripped_img_ref = re.search(r"[^/\\]+$", img_link)
|
|
|
|
if stripped_img_ref:
|
|
|
|
stripped_img_ref = stripped_img_ref.group()
|
|
|
|
new_img_ref = f"static/{stripped_img_ref}"
|
|
|
|
line = line.replace(f"({img_link})", f"({new_img_ref})")
|
|
|
|
|
|
|
|
if internal_links:
|
|
|
|
for internal_link in internal_links:
|
|
|
|
if internal_link.endswith('.md') and ('/' in internal_link):
|
|
|
|
stripped_path = (re.search(r"[^/\\]+$", internal_link))
|
|
|
|
if stripped_path:
|
|
|
|
stripped_path = stripped_path.group()
|
|
|
|
line = line.replace(f"({internal_link})", f"({stripped_path})")
|
2024-10-19 13:03:04 +01:00
|
|
|
return line
|
|
|
|
|
|
|
|
|
|
|
|
def transform_links(target_dir):
|
2024-11-11 15:49:01 +00:00
|
|
|
print(colored("INFO: Updating links...", "light_blue"))
|
2024-10-19 13:03:04 +01:00
|
|
|
for filename in os.listdir(target_dir):
|
|
|
|
if filename.endswith(".md"):
|
|
|
|
file_path = os.path.join(target_dir, filename)
|
|
|
|
with open(file_path, "r") as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
new_lines = []
|
|
|
|
for line in lines:
|
2025-01-16 07:05:31 +00:00
|
|
|
new_lines.append(process_line(line))
|
|
|
|
if len(new_lines):
|
2024-10-19 13:03:04 +01:00
|
|
|
with open(file_path, "w") as f:
|
|
|
|
f.writelines(new_lines)
|
|
|
|
print(
|
|
|
|
colored(
|
2024-11-11 15:49:01 +00:00
|
|
|
"SUCCESS Links updated",
|
|
|
|
"light_green",
|
2024-10-19 13:03:04 +01:00
|
|
|
)
|
|
|
|
)
|