add fix link/ref script

This commit is contained in:
hoangvv 2025-02-28 09:46:43 +07:00
parent 71c1aeb355
commit ebc2396ce2

View File

@ -1,51 +1,28 @@
#!/bin/bash
# Check if input file is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <input_file>"
INPUT_FILE="$1"
if [[ ! -f "$INPUT_FILE" ]]; then
echo "Error: Input file does not exist."
exit 1
fi
input_file="$1"
while IFS=':' read -r file_path line_err status desc data; do
# Trim whitespace from variables
file_path="$(echo "$file_path" | xargs)"
line_err="$(echo "$line_err" | xargs)"
status="$(echo "$status" | xargs)"
desc="$(echo "$desc" | xargs)"
data="$(echo "$data" | xargs | tr -d "'")" # Remove quotes from data
# Check if file exists
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found"
exit 1
if [[ -f "$file_path" ]]; then
if [[ "$desc" == "undefined label" ]]; then
fixed_data="$(echo "$data" | tr '\\/_' '-')"
sed -i "s@$data@$fixed_data@g" "$file_path"
elif [[ "$desc" == *"toctree contains reference to nonexisting document"* ]]; then
fixed_data="$(echo "$data" | tr '\\/_' '-')"
sed -i "s@$data@$fixed_data@g" "$file_path"
fi
fi
# Process each line of the input file
while IFS= read -r line || [ -n "$line" ]; do
# Skip empty lines
[ -z "$line" ] && continue
# Extract filepath (everything before first colon)
filepath=$(echo "$line" | cut -d: -f1)
# Extract status (WARNING)
status="WARNING"
# Split based on different patterns
if [[ "$line" =~ "WARNING: toctree contains reference to nonexisting document" ]]; then
description="toctree contains reference to nonexisting document"
data=$(echo "$line" | sed "s|^.*$description ||")
elif [[ "$line" =~ "WARNING: undefined label:" ]]; then
description="undefined label:"
data=$(echo "$line" | sed "s|^.*$description ||")
else
# For the first line with deprecated substitutions
description=$(echo "$line" | sed "s|^.*WARNING: ||" | sed "s| \[.*$||")
data=$(echo "$line" | grep -o "\[.*$" | tr -d '[]')
fi
# Replace /, \, and _ with - in data
modified_data=$(echo "$data" | tr '/\\_' '-')
# Print the results
echo "filepath = $filepath"
echo "status = $status"
echo "description = $description"
echo "data = $modified_data"
echo "-------------------"
done < "$input_file"
done < "$INPUT_FILE"