neuron-zk-generator/src/lib/transform_links.py

46 lines
1.3 KiB
Python
Raw Normal View History

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
def process_line(line):
2025-01-17 17:51:40 +00:00
link_rgx = r"\[.*?\]\((.*?)\)"
links = re.findall(link_rgx, line)
if links:
for link in links:
stripped_path = re.search(r"[^/\\]+$", link)
if stripped_path:
stripped_path = stripped_path.group()
# Handle internal links
if ".md" in stripped_path:
line = line.replace(f"({link})", f"({stripped_path})")
# Handle image links
else:
new_img_path = f"static/{stripped_path}"
line = line.replace(f"({link})", f"({new_img_path})")
return line
2024-10-19 13:03:04 +01:00
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:
new_lines.append(process_line(line))
2025-01-17 17:51:40 +00:00
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
)
)