|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import yaml |
| 6 | +import sys |
| 7 | + |
| 8 | +# Constants |
| 9 | +REPO_URL = "https://github.com/PhasicFlow/phasicFlow" |
| 10 | +REPO_PATH = os.path.join(os.environ.get("GITHUB_WORKSPACE", ""), "repo") |
| 11 | +WIKI_PATH = os.path.join(os.environ.get("GITHUB_WORKSPACE", ""), "wiki") |
| 12 | +MAPPING_FILE = os.path.join(REPO_PATH, "doc/mdDocs/markdownList.yml") |
| 13 | + |
| 14 | +def load_mapping(): |
| 15 | + """Load the markdown to wiki page mapping file.""" |
| 16 | + try: |
| 17 | + with open(MAPPING_FILE, 'r') as f: |
| 18 | + data = yaml.safe_load(f) |
| 19 | + return data.get('mappings', []) |
| 20 | + except Exception as e: |
| 21 | + print(f"Error loading mapping file: {e}") |
| 22 | + return [] |
| 23 | + |
| 24 | +def convert_relative_links(content, source_path): |
| 25 | + """Convert relative links in markdown content to absolute URLs.""" |
| 26 | + # Find markdown links with regex pattern [text](url) |
| 27 | + md_pattern = r'\[([^\]]+)\]\(([^)]+)\)' |
| 28 | + |
| 29 | + # Find HTML img tags |
| 30 | + img_pattern = r'<img\s+src=[\'"]([^\'"]+)[\'"]' |
| 31 | + |
| 32 | + def replace_link(match): |
| 33 | + link_text = match.group(1) |
| 34 | + link_url = match.group(2) |
| 35 | + |
| 36 | + # Skip if already absolute URL or anchor |
| 37 | + if link_url.startswith(('http://', 'https://', '#', 'mailto:')): |
| 38 | + return match.group(0) |
| 39 | + |
| 40 | + # Get the directory of the source file |
| 41 | + source_dir = os.path.dirname(source_path) |
| 42 | + |
| 43 | + # Create absolute path from repository root |
| 44 | + if link_url.startswith('/'): |
| 45 | + # If link starts with /, it's already relative to repo root |
| 46 | + abs_path = link_url |
| 47 | + else: |
| 48 | + # Otherwise, it's relative to the file location |
| 49 | + abs_path = os.path.normpath(os.path.join(source_dir, link_url)) |
| 50 | + if not abs_path.startswith('/'): |
| 51 | + abs_path = '/' + abs_path |
| 52 | + |
| 53 | + # Convert to GitHub URL |
| 54 | + github_url = f"{REPO_URL}/blob/main{abs_path}" |
| 55 | + return f"[{link_text}]({github_url})" |
| 56 | + |
| 57 | + def replace_img_src(match): |
| 58 | + img_src = match.group(1) |
| 59 | + |
| 60 | + # Skip if already absolute URL |
| 61 | + if img_src.startswith(('http://', 'https://')): |
| 62 | + return match.group(0) |
| 63 | + |
| 64 | + # Get the directory of the source file |
| 65 | + source_dir = os.path.dirname(source_path) |
| 66 | + |
| 67 | + # Create absolute path from repository root |
| 68 | + if img_src.startswith('/'): |
| 69 | + # If link starts with /, it's already relative to repo root |
| 70 | + abs_path = img_src |
| 71 | + else: |
| 72 | + # Otherwise, it's relative to the file location |
| 73 | + abs_path = os.path.normpath(os.path.join(source_dir, img_src)) |
| 74 | + if not abs_path.startswith('/'): |
| 75 | + abs_path = '/' + abs_path |
| 76 | + |
| 77 | + # Convert to GitHub URL (use raw URL for images) |
| 78 | + github_url = f"{REPO_URL}/raw/main{abs_path}" |
| 79 | + return f'<img src="{github_url}"' |
| 80 | + |
| 81 | + # Replace all markdown links |
| 82 | + content = re.sub(md_pattern, replace_link, content) |
| 83 | + |
| 84 | + # Replace all img src tags |
| 85 | + content = re.sub(img_pattern, replace_img_src, content) |
| 86 | + |
| 87 | + return content |
| 88 | + |
| 89 | +def process_file(source_file, target_wiki_page): |
| 90 | + """Process a markdown file and copy its contents to a wiki page.""" |
| 91 | + source_path = os.path.join(REPO_PATH, source_file) |
| 92 | + target_path = os.path.join(WIKI_PATH, f"{target_wiki_page}.md") |
| 93 | + |
| 94 | + print(f"Processing {source_path} -> {target_path}") |
| 95 | + |
| 96 | + try: |
| 97 | + # Check if source exists |
| 98 | + if not os.path.exists(source_path): |
| 99 | + print(f"Source file not found: {source_path}") |
| 100 | + return False |
| 101 | + |
| 102 | + # Read source content |
| 103 | + with open(source_path, 'r') as f: |
| 104 | + content = f.read() |
| 105 | + |
| 106 | + # Convert relative links |
| 107 | + content = convert_relative_links(content, source_file) |
| 108 | + |
| 109 | + # Write to wiki page |
| 110 | + with open(target_path, 'w') as f: |
| 111 | + f.write(content) |
| 112 | + |
| 113 | + return True |
| 114 | + |
| 115 | + except Exception as e: |
| 116 | + print(f"Error processing {source_file}: {e}") |
| 117 | + return False |
| 118 | + |
| 119 | +def main(): |
| 120 | + # Check if wiki directory exists |
| 121 | + if not os.path.exists(WIKI_PATH): |
| 122 | + print(f"Wiki path not found: {WIKI_PATH}") |
| 123 | + sys.exit(1) |
| 124 | + |
| 125 | + # Load mapping |
| 126 | + mappings = load_mapping() |
| 127 | + if not mappings: |
| 128 | + print("No mappings found in the mapping file") |
| 129 | + sys.exit(1) |
| 130 | + |
| 131 | + print(f"Found {len(mappings)} mappings to process") |
| 132 | + |
| 133 | + # Process each mapping |
| 134 | + success_count = 0 |
| 135 | + for mapping in mappings: |
| 136 | + source = mapping.get('source') |
| 137 | + target = mapping.get('target') |
| 138 | + |
| 139 | + if not source or not target: |
| 140 | + print(f"Invalid mapping: {mapping}") |
| 141 | + continue |
| 142 | + |
| 143 | + if process_file(source, target): |
| 144 | + success_count += 1 |
| 145 | + |
| 146 | + print(f"Successfully processed {success_count} of {len(mappings)} files") |
| 147 | + |
| 148 | + # Exit with error if any file failed |
| 149 | + if success_count < len(mappings): |
| 150 | + sys.exit(1) |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments