diff --git a/fix-link.sh b/fix-link.sh index a742e3e92..558d19ed4 100755 --- a/fix-link.sh +++ b/fix-link.sh @@ -1,8 +1,14 @@ #!/bin/bash +# ANSI color codes +GREEN='\033[0;32m' +RED='\033[0;31m' +GRAY='\033[0;90m' +NC='\033[0m' # No Color + # Check if input file is provided if [ $# -ne 1 ]; then - echo "Usage: $0 " + echo -e "${RED}Usage: $0 ${NC}" exit 1 fi @@ -10,7 +16,7 @@ input_file="$1" # Check if input file exists if [ ! -f "$input_file" ]; then - echo "Error: File '$input_file' not found" + echo -e "${RED}Error: File '$input_file' not found${NC}" exit 1 fi @@ -25,57 +31,95 @@ while IFS=':' read -r file_path line_err status desc data; do desc=$(echo "$desc" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') data=$(echo "$data" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - echo "DEBUG: Processing line:" - echo "DEBUG: file_path=[$file_path]" - echo "DEBUG: line_err=[$line_err]" - echo "DEBUG: status=[$status]" - echo "DEBUG: desc=[$desc]" - echo "DEBUG: data=[$data]" + echo -e "${GRAY}DEBUG: Processing line:${NC}" + echo -e "${GRAY}DEBUG: file_path=[$file_path]${NC}" + echo -e "${GRAY}DEBUG: line_err=[$line_err]${NC}" + echo -e "${GRAY}DEBUG: status=[$status]${NC}" + echo -e "${GRAY}DEBUG: desc=[$desc]${NC}" + echo -e "${GRAY}DEBUG: data=[$data]${NC}" # Check if file exists and description matches conditions if [ -f "$file_path" ]; then - echo "DEBUG: File exists: $file_path" + echo -e "${GRAY}DEBUG: File exists: $file_path${NC}" if [ "$desc" = "undefined label" ]; then - echo "DEBUG: Found 'undefined label' case" + echo -e "${GRAY}DEBUG: Found 'undefined label' case${NC}" # Replace \, /, or _ with - in data new_data=$(echo "$data" | tr '\\/_' '-') - echo "DEBUG: Changing '$data' to '$new_data'" - # Replace the old data with new data in the file - if sed -i "s|$data|$new_data|g" "$file_path"; then - changes_made=$((changes_made + 1)) - echo "Processed: $file_path - Changed '$data' to '$new_data'" + echo -e "${GRAY}DEBUG: Changing '$data' to '$new_data'${NC}" + + # Check if the data appears in an {image} directive + if grep -B1 "$data" "$file_path" | grep -q "{image}"; then + echo -e "${GRAY}DEBUG: Skipping '$data' as it appears in an {image} directive${NC}" else - echo "WARNING: Failed to modify $file_path" + # Replace the old data with new data in the file, only if not preceded by {image} + if sed -i "/{image}/!s|$data|$new_data|g" "$file_path" 2>/dev/null; then + changes_made=$((changes_made + 1)) + echo -e "${GREEN}Processed: $file_path - Changed '$data' to '$new_data'${NC}" + else + echo -e "${RED}WARNING: Failed to modify $file_path${NC}" + fi fi elif [[ "$desc" =~ "toctree contains reference to nonexisting document" ]]; then - echo "DEBUG: Found 'toctree' case" + echo -e "${GRAY}DEBUG: Found 'toctree' case${NC}" # Extract the document reference from data (assuming it's in single quotes) doc_ref=$(echo "$data" | grep -o "'[^']*'" | tr -d "'") if [ -n "$doc_ref" ]; then # Replace \, /, or _ with - in doc_ref new_data=$(echo "$doc_ref" | tr '\\/_' '-') - echo "DEBUG: Changing '$doc_ref' to '$new_data'" - # Replace the old data with new data in the file - if sed -i "s|$doc_ref|$new_data|g" "$file_path"; then - changes_made=$((changes_made + 1)) - echo "Processed: $file_path - Changed '$doc_ref' to '$new_data'" + echo -e "${GRAY}DEBUG: Changing '$doc_ref' to '$new_data'${NC}" + + # Check if the doc_ref appears in an {image} directive + if grep -B1 "$doc_ref" "$file_path" | grep -q "{image}"; then + echo -e "${GRAY}DEBUG: Skipping '$doc_ref' as it appears in an {image} directive${NC}" else - echo "WARNING: Failed to modify $file_path" + # Replace the old data with new data in the file, only if not preceded by {image} + if sed -i "/{image}/!s|$doc_ref|$new_data|g" "$file_path" 2>/dev/null; then + changes_made=$((changes_made + 1)) + echo -e "${GREEN}Processed: $file_path - Changed '$doc_ref' to '$new_data'${NC}" + else + echo -e "${RED}WARNING: Failed to modify $file_path${NC}" + fi fi else - echo "WARNING: Could not extract document reference from '$data' in $file_path" + echo -e "${RED}WARNING: Could not extract document reference from '$data' in $file_path${NC}" fi + + elif [[ "$desc" =~ "card directive targets nonexisting document" ]]; then + echo -e "${GRAY}DEBUG: Found 'card directive' case${NC}" + # Extract the document reference from desc (since data is empty), remove .rst + doc_ref=$(echo "$desc" | grep -o "'[^']*'" | tr -d "'" | sed 's/\.rst$//') + if [ -n "$doc_ref" ]; then + # Replace \, /, or _ with - in doc_ref + new_data=$(echo "$doc_ref" | tr '\\/_' '-') + echo -e "${GRAY}DEBUG: Changing '$doc_ref' to '$new_data'${NC}" + + # Check if the doc_ref appears in an {image} directive + if grep -B1 "$doc_ref" "$file_path" | grep -q "{image}"; then + echo -e "${GRAY}DEBUG: Skipping '$doc_ref' as it appears in an {image} directive${NC}" + else + # Replace the old data with new data in the file, only if not preceded by {image} + if sed -i "/{image}/!s|$doc_ref|$new_data|g" "$file_path" 2>/dev/null; then + changes_made=$((changes_made + 1)) + echo -e "${GREEN}Processed: $file_path - Changed '$doc_ref' to '$new_data'${NC}" + else + echo -e "${RED}WARNING: Failed to modify $file_path${NC}" + fi + fi + else + echo -e "${RED}WARNING: Could not extract document reference from '$desc' in $file_path${NC}" + fi + else - echo "DEBUG: Description '$desc' doesn't match any conditions" + echo -e "${GRAY}DEBUG: Description '$desc' doesn't match any conditions${NC}" fi else - echo "WARNING: File not found: $file_path" + echo -e "${RED}WARNING: File not found: $file_path${NC}" fi - echo "DEBUG: ---" + echo -e "${GRAY}DEBUG: ---${NC}" done < "$input_file" -echo "Processing completed!" -echo "Total changes made: $changes_made" \ No newline at end of file +echo -e "${GREEN}Processing completed!${NC}" +echo -e "${GREEN}Total changes made: $changes_made${NC}" \ No newline at end of file