update script

This commit is contained in:
hoangvv 2025-02-28 10:39:52 +07:00
parent 52ff35cc22
commit 44d23b4aba

View File

@ -1,8 +1,14 @@
#!/bin/bash #!/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 # Check if input file is provided
if [ $# -ne 1 ]; then if [ $# -ne 1 ]; then
echo "Usage: $0 <input_file>" echo -e "${RED}Usage: $0 <input_file>${NC}"
exit 1 exit 1
fi fi
@ -10,7 +16,7 @@ input_file="$1"
# Check if input file exists # Check if input file exists
if [ ! -f "$input_file" ]; then if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found" echo -e "${RED}Error: File '$input_file' not found${NC}"
exit 1 exit 1
fi 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:]]*$//') desc=$(echo "$desc" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
data=$(echo "$data" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') data=$(echo "$data" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "DEBUG: Processing line:" echo -e "${GRAY}DEBUG: Processing line:${NC}"
echo "DEBUG: file_path=[$file_path]" echo -e "${GRAY}DEBUG: file_path=[$file_path]${NC}"
echo "DEBUG: line_err=[$line_err]" echo -e "${GRAY}DEBUG: line_err=[$line_err]${NC}"
echo "DEBUG: status=[$status]" echo -e "${GRAY}DEBUG: status=[$status]${NC}"
echo "DEBUG: desc=[$desc]" echo -e "${GRAY}DEBUG: desc=[$desc]${NC}"
echo "DEBUG: data=[$data]" echo -e "${GRAY}DEBUG: data=[$data]${NC}"
# Check if file exists and description matches conditions # Check if file exists and description matches conditions
if [ -f "$file_path" ]; then 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 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 # Replace \, /, or _ with - in data
new_data=$(echo "$data" | tr '\\/_' '-') new_data=$(echo "$data" | tr '\\/_' '-')
echo "DEBUG: Changing '$data' to '$new_data'" echo -e "${GRAY}DEBUG: Changing '$data' to '$new_data'${NC}"
# Replace the old data with new data in the file
if sed -i "s|$data|$new_data|g" "$file_path"; then # Check if the data appears in an {image} directive
changes_made=$((changes_made + 1)) if grep -B1 "$data" "$file_path" | grep -q "{image}"; then
echo "Processed: $file_path - Changed '$data' to '$new_data'" echo -e "${GRAY}DEBUG: Skipping '$data' as it appears in an {image} directive${NC}"
else 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 fi
elif [[ "$desc" =~ "toctree contains reference to nonexisting document" ]]; then 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) # Extract the document reference from data (assuming it's in single quotes)
doc_ref=$(echo "$data" | grep -o "'[^']*'" | tr -d "'") doc_ref=$(echo "$data" | grep -o "'[^']*'" | tr -d "'")
if [ -n "$doc_ref" ]; then if [ -n "$doc_ref" ]; then
# Replace \, /, or _ with - in doc_ref # Replace \, /, or _ with - in doc_ref
new_data=$(echo "$doc_ref" | tr '\\/_' '-') new_data=$(echo "$doc_ref" | tr '\\/_' '-')
echo "DEBUG: Changing '$doc_ref' to '$new_data'" echo -e "${GRAY}DEBUG: Changing '$doc_ref' to '$new_data'${NC}"
# Replace the old data with new data in the file
if sed -i "s|$doc_ref|$new_data|g" "$file_path"; then # Check if the doc_ref appears in an {image} directive
changes_made=$((changes_made + 1)) if grep -B1 "$doc_ref" "$file_path" | grep -q "{image}"; then
echo "Processed: $file_path - Changed '$doc_ref' to '$new_data'" echo -e "${GRAY}DEBUG: Skipping '$doc_ref' as it appears in an {image} directive${NC}"
else 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 fi
else 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 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 else
echo "DEBUG: Description '$desc' doesn't match any conditions" echo -e "${GRAY}DEBUG: Description '$desc' doesn't match any conditions${NC}"
fi fi
else else
echo "WARNING: File not found: $file_path" echo -e "${RED}WARNING: File not found: $file_path${NC}"
fi fi
echo "DEBUG: ---" echo -e "${GRAY}DEBUG: ---${NC}"
done < "$input_file" done < "$input_file"
echo "Processing completed!" echo -e "${GREEN}Processing completed!${NC}"
echo "Total changes made: $changes_made" echo -e "${GREEN}Total changes made: $changes_made${NC}"