import os import re TARGET_DIR = "src/content/docs/szmgr" pattern = re.compile(r"pass:\[<s>(.*?)</s>\]") files = [f for f in os.listdir(TARGET_DIR) if f.endswith(".md")] for f in files: print(f"\t- {f}") lines = [] with open(os.path.join(TARGET_DIR, f), "r") as file: lines = file.readlines() for i in range(len(lines)): l = lines[i] matches = list(pattern.finditer(l)) if matches: # Reverse so replacements don't mess up span positions for match in reversed(matches): topic = match.group(1).strip() print(f"Replacing {topic}") start, end = match.span() l = l[:start] + f" ~{topic}~ " + l[end:] lines[i] = l with open(os.path.join(TARGET_DIR, f), "w") as file: file.writelines(lines)