1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
import os import re import argparse import sys
def parse_repo_string(repo_string): """ Parses the repository string "user/repo@branch" into (user, repo, branch).
Args: repo_string: The repository string in the format 'user/repo@branch'.
Returns: A tuple containing (user, repo, branch).
Raises: ValueError: If the string format is invalid. """ match = re.match(r"([^/]+)/([^@]+)@(.+)", repo_string) if match: user, repo, branch = match.groups() if user and repo and branch: return user, repo, branch else: raise ValueError("User, repository name, and branch cannot be empty.") else: raise ValueError("Repository string must be in the format 'user/repo@branch'.")
def replace_links_in_file(filepath, repo_user, repo_name, branch, custom_domain): """ Reads a file, replaces specific GitHub/jsDelivr links, and writes back if changes were made.
Args: filepath: Path to the Markdown file. repo_user: GitHub username. repo_name: GitHub repository name. branch: GitHub repository branch. custom_domain: The custom domain to use for replacement.
Returns: The number of replacements made in this file. """ replacements_made = 0 try: with open(filepath, 'r', encoding='utf-8') as f_read: content = f_read.read() original_content = content
escaped_user = re.escape(repo_user) escaped_repo = re.escape(repo_name) escaped_branch = re.escape(branch)
jsdelivr_pattern = re.compile( rf"https?://cdn\.jsdelivr\.net/gh/{escaped_user}/{escaped_repo}@{escaped_branch}/([^\s\"\'\)<>]+)" )
github_raw_pattern = re.compile( rf"https?://raw\.githubusercontent\.com/{escaped_user}/{escaped_repo}/{escaped_branch}/([^\s\"\'\)<>]+)" )
replacement_url_base = f"https://{custom_domain}"
def replacer(match): nonlocal replacements_made path = match.group(1) replacements_made += 1 return f"{replacement_url_base}/{path.lstrip('/')}"
content = jsdelivr_pattern.sub(replacer, content) content = github_raw_pattern.sub(replacer, content)
if content != original_content: with open(filepath, 'w', encoding='utf-8') as f_write: f_write.write(content) print(f" ✅ Replaced {replacements_made} links.") else: pass
except FileNotFoundError: print(f" ❌ Error: File not found at {filepath}.") except IOError as e: print(f" ❌ Error reading/writing file {filepath}: {e}") except Exception as e: print(f" ❌ An unexpected error occurred processing file {filepath}: {e}")
return replacements_made
def main(): """ Main function to parse arguments and orchestrate the link replacement process. """ parser = argparse.ArgumentParser( description="Recursively scan Markdown files and replace GitHub/jsDelivr links with a custom domain.", formatter_class=argparse.RawDescriptionHelpFormatter )
parser.add_argument( "repo", help="Target GitHub repository in the format 'user/repo@branch' (e.g., DullJZ/MyPicture@master)" ) parser.add_argument( "domain", help="Custom domain name to use for replacement (e.g., ohmyimage.pp.ua)" )
parser.add_argument( "-d", "--directory", default=".", help="The root directory to start scanning for Markdown files (default: current directory)" ) parser.add_argument( "-e", "--extensions", default=".md", help="Comma-separated list of file extensions to process (default: .md)" )
args = parser.parse_args()
try: repo_user, repo_name, branch = parse_repo_string(args.repo) except ValueError as e: print(f"Error: Invalid repository format provided ('{args.repo}'). {e}", file=sys.stderr) sys.exit(1)
custom_domain = args.domain start_dir = args.directory file_extensions = tuple(ext.strip().lower() for ext in args.extensions.split(',') if ext.strip()) if not file_extensions: print(f"Error: No valid file extensions specified.", file=sys.stderr) sys.exit(1)
total_files_processed = 0 total_replacements = 0
print(f"🚀 Starting link replacement process...") print(f" Repository: {args.repo}") print(f" Custom Domain: {custom_domain}") print(f" Scanning Directory: {os.path.abspath(start_dir)}") print(f" File Extensions: {', '.join(file_extensions)}") print("-" * 40)
for root, _, files in os.walk(start_dir): for filename in files: if filename.lower().endswith(file_extensions): filepath = os.path.join(root, filename) relative_path = os.path.relpath(filepath, start_dir) print(f"Processing: {relative_path}") total_files_processed += 1 try: count = replace_links_in_file(filepath, repo_user, repo_name, branch, custom_domain) total_replacements += count except Exception as e: print(f" ❌ Failed to process {relative_path}: {e}")
print("-" * 40) print(f"🏁 Scan complete.") if total_files_processed == 0: print(f" No files with extensions ({', '.join(file_extensions)}) found in '{start_dir}'.") else: print(f" Processed {total_files_processed} file(s).") print(f" Made a total of {total_replacements} replacement(s).")
if __name__ == "__main__": main()
|