#!/usr/bin/env python3 import os import re import yaml import sys # Constants REPO_URL = "https://github.com/PhasicFlow/phasicFlow" REPO_PATH = os.path.join(os.environ.get("GITHUB_WORKSPACE", ""), "repo") WIKI_PATH = os.path.join(os.environ.get("GITHUB_WORKSPACE", ""), "wiki") MAPPING_FILE = os.path.join(REPO_PATH, ".github/workflows/markdownList.yml") def load_mapping(): """Load the markdown to wiki page mapping file.""" try: with open(MAPPING_FILE, 'r') as f: data = yaml.safe_load(f) return data.get('mappings', []) except Exception as e: print(f"Error loading mapping file: {e}") return [] def convert_relative_links(content, source_path): """Convert relative links in markdown content to absolute URLs.""" # Find markdown links with regex pattern [text](url) md_pattern = r'\[([^\]]+)\]\(([^)]+)\)' # Find HTML img tags img_pattern = r' {target_path}") try: # Check if source exists if not os.path.exists(source_path): print(f"Source file not found: {source_path}") return False # Read source content with open(source_path, 'r') as f: content = f.read() # Convert relative links content = convert_relative_links(content, source_file) # Write to wiki page with open(target_path, 'w') as f: f.write(content) return True except Exception as e: print(f"Error processing {source_file}: {e}") return False def main(): # Check if wiki directory exists if not os.path.exists(WIKI_PATH): print(f"Wiki path not found: {WIKI_PATH}") sys.exit(1) # Load mapping mappings = load_mapping() if not mappings: print("No mappings found in the mapping file") sys.exit(1) print(f"Found {len(mappings)} mappings to process") # Process each mapping success_count = 0 for mapping in mappings: source = mapping.get('source') target = mapping.get('target') if not source or not target: print(f"Invalid mapping: {mapping}") continue if process_file(source, target): success_count += 1 print(f"Successfully processed {success_count} of {len(mappings)} files") # Exit with error if any file failed if success_count < len(mappings): sys.exit(1) if __name__ == "__main__": main()