import re
import os
from pathlib import Path
def convert_jekyll_link(match):
link_text = match.group(1)
url_part = match.group(2)
url = url_part.lstrip('/')
return f"[{link_text}]({url})"
def fix_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
pattern = r'\[([^\]]+)\]\(\{\{\s*[\'"]?([^\'"]+)[\'"]?\s*\|\s*relative_url\s*\}\}(?:#([^\]]+))?\)'
def replace_link(match):
link_text = match.group(1)
url_part = match.group(2)
anchor = match.group(3) if match.lastindex >= 3 else None
url = url_part.lstrip('/')
if not url.endswith('.md') and not url.startswith('http') and not url.startswith('#'):
url = f"{url}.md"
if anchor:
url = f"{url}#{anchor}"
return f"[{link_text}]({url})"
new_content = re.sub(pattern, replace_link, content)
if new_content != content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
return False
except Exception as e:
print(f"Error processing {file_path}: {e}")
return False
def main():
docs_dir = Path("docs")
if not docs_dir.exists():
print("Error: docs directory not found")
return
md_files = list(docs_dir.rglob("*.md"))
print(f"Found {len(md_files)} markdown files")
print("\nConverting Jekyll links to Markdown...")
fixed_count = 0
for md_file in md_files:
if fix_file(md_file):
try:
rel_path = md_file.relative_to(Path.cwd())
except ValueError:
rel_path = md_file
print(f" Fixed: {rel_path}")
fixed_count += 1
print(f"\nFixed {fixed_count} files")
if __name__ == "__main__":
main()