From 0e357b4ef86c457b336cdedd2b23e6ed48f0150b Mon Sep 17 00:00:00 2001 From: hoangvv Date: Fri, 28 Feb 2025 11:05:29 +0700 Subject: [PATCH] update --- fix-link.py | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100755 fix-link.py diff --git a/fix-link.py b/fix-link.py new file mode 100755 index 000000000..3d7d42cd4 --- /dev/null +++ b/fix-link.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 + +import sys +import os +import re +import fileinput +from colorama import init, Fore, Style + +# Initialize colorama for colored output +init() + +# Color definitions +GREEN = Fore.GREEN +RED = Fore.RED +GRAY = Fore.LIGHTBLACK_EX +NC = Style.RESET_ALL + +# Check if input file is provided +if len(sys.argv) != 2: + print(f"{RED}Usage: {sys.argv[0]} {NC}") + sys.exit(1) + +input_file = sys.argv[1] + +# Check if input file exists +if not os.path.isfile(input_file): + print(f"{RED}Error: File '{input_file}' not found{NC}") + sys.exit(1) + +changes_made = 0 + +# Process each line +with open(input_file, 'r') as f: + for line in f: + # Split line by ':' and trim whitespace + parts = [part.strip() for part in line.split(':', 4)] + if len(parts) < 5: + parts.extend([''] * (5 - len(parts))) # Ensure 5 parts + file_path, line_err, status, desc, data = parts + + print(f"{GRAY}DEBUG: Processing line:{NC}") + print(f"{GRAY}DEBUG: file_path=[{file_path}]{NC}") + print(f"{GRAY}DEBUG: line_err=[{line_err}]{NC}") + print(f"{GRAY}DEBUG: status=[{status}]{NC}") + print(f"{GRAY}DEBUG: desc=[{desc}]{NC}") + print(f"{GRAY}DEBUG: data=[{data}]{NC}") + + # Check if file exists and description matches conditions + if os.path.isfile(file_path): + print(f"{GRAY}DEBUG: File exists: {file_path}{NC}") + + if desc == "undefined label": + print(f"{GRAY}DEBUG: Found 'undefined label' case{NC}") + # Check if data is enclosed in < and > + if re.match(r'^<.*>$', data): + # Remove < and > for processing + clean_data = re.sub(r'^<|>$', '', data) + # Replace \, /, or _ with - in clean_data + new_data = clean_data.replace('\\', '-').replace('/', '-').replace('_', '-') + print(f"{GRAY}DEBUG: Changing '{data}' to '<{new_data}>'{NC}") + + # Check if the data appears in an {image} directive + with open(file_path, 'r') as file_content: + lines = file_content.readlines() + skip = False + for i, line_content in enumerate(lines): + if data in line_content and i > 0 and '{image}' in lines[i-1]: + skip = True + break + + if skip: + print(f"{GRAY}DEBUG: Skipping '{data}' as it appears in an {{image}} directive{NC}") + else: + # Replace the old data with new data in the file + try: + with fileinput.FileInput(file_path, inplace=True) as file: + for line_content in file: + if '{image}' not in line_content: + line_content = line_content.replace(data, f"<{new_data}>") + print(line_content, end='') + changes_made += 1 + print(f"{GREEN}Processed: {file_path} - Changed '{data}' to '<{new_data}>'{NC}") + except Exception as e: + print(f"{RED}WARNING: Failed to modify {file_path} - {str(e)}{NC}") + else: + print(f"{GRAY}DEBUG: Skipping '{data}' as it is not enclosed in <> {NC}") + + elif "toctree contains reference to nonexisting document" in desc: + print(f"{GRAY}DEBUG: Found 'toctree' case{NC}") + # Extract the document reference from data (assuming it's in single quotes) + doc_ref_match = re.search(r"'([^']*)'", data) + if doc_ref_match: + doc_ref = doc_ref_match.group(1) + # Replace \, /, or _ with - in doc_ref + new_data = doc_ref.replace('\\', '-').replace('/', '-').replace('_', '-') + print(f"{GRAY}DEBUG: Changing '{doc_ref}' to '{new_data}'{NC}") + + # Check if the doc_ref appears in an {image} directive + with open(file_path, 'r') as file_content: + lines = file_content.readlines() + skip = False + for i, line_content in enumerate(lines): + if doc_ref in line_content and i > 0 and '{image}' in lines[i-1]: + skip = True + break + + if skip: + print(f"{GRAY}DEBUG: Skipping '{doc_ref}' as it appears in an {{image}} directive{NC}") + else: + # Replace the old data with new data in the file + try: + with fileinput.FileInput(file_path, inplace=True) as file: + for line_content in file: + if '{image}' not in line_content: + line_content = line_content.replace(doc_ref, new_data) + print(line_content, end='') + changes_made += 1 + print(f"{GREEN}Processed: {file_path} - Changed '{doc_ref}' to '{new_data}'{NC}") + except Exception as e: + print(f"{RED}WARNING: Failed to modify {file_path} - {str(e)}{NC}") + else: + print(f"{RED}WARNING: Could not extract document reference from '{data}' in {file_path}{NC}") + + elif "card directive targets nonexisting document" in desc: + print(f"{GRAY}DEBUG: Found 'card directive' case{NC}") + # Extract the document reference from desc (since data might be empty), remove .rst + doc_ref_match = re.search(r"'([^']*)'", desc) + if doc_ref_match: + doc_ref = doc_ref_match.group(1).replace('.rst', '') + # Replace \, /, or _ with - in doc_ref + new_data = doc_ref.replace('\\', '-').replace('/', '-').replace('_', '-') + print(f"{GRAY}DEBUG: Changing '{doc_ref}' to '{new_data}'{NC}") + + # Check if the doc_ref appears in an {image} directive + with open(file_path, 'r') as file_content: + lines = file_content.readlines() + skip = False + for i, line_content in enumerate(lines): + if doc_ref in line_content and i > 0 and '{image}' in lines[i-1]: + skip = True + break + + if skip: + print(f"{GRAY}DEBUG: Skipping '{doc_ref}' as it appears in an {{image}} directive{NC}") + else: + # Replace the old data with new data in the file + try: + with fileinput.FileInput(file_path, inplace=True) as file: + for line_content in file: + if '{image}' not in line_content: + line_content = line_content.replace(doc_ref, new_data) + print(line_content, end='') + changes_made += 1 + print(f"{GREEN}Processed: {file_path} - Changed '{doc_ref}' to '{new_data}'{NC}") + except Exception as e: + print(f"{RED}WARNING: Failed to modify {file_path} - {str(e)}{NC}") + else: + print(f"{RED}WARNING: Could not extract document reference from '{desc}' in {file_path}{NC}") + + else: + print(f"{GRAY}DEBUG: Description '{desc}' doesn't match any conditions{NC}") + else: + print(f"{RED}WARNING: File not found: {file_path}{NC}") + print(f"{GRAY}DEBUG: ---{NC}") + +print(f"{GREEN}Processing completed!{NC}") +print(f"{GREEN}Total changes made: {changes_made}{NC}") \ No newline at end of file