diff --git a/wiki-tools/brokenlinks.sh b/wiki-tools/brokenlinks.sh index e9e5934e..d31e9055 100644 --- a/wiki-tools/brokenlinks.sh +++ b/wiki-tools/brokenlinks.sh @@ -27,7 +27,7 @@ searchfile() { while IFS= read -r -u 3 link; do # If it's an internet link, ignore it. # That's beyond the scope of this tool. - if echo $link | grep -E '^[http|\/]' >/dev/null; then + if echo "$link" | grep -E '^[http|\/]' >/dev/null; then continue fi # At some point in this scripts development, fixed links to files/images were given the './' prefix. @@ -35,26 +35,28 @@ searchfile() { # I added this to fix the problems I caused, and decided it was worth keeping around. if echo $link | grep -E '^\./' >/dev/null; then # NEWLINK is the corrected link - NEWLINK=$(echo $link | sed 's/^\.\///') + NEWLINK=$(echo "$link" | sed 's/^\.\///') # Print the file and the old link echo "In $1:" - echo $link + echo "$link" # Print the options as though they are a list in order to have the same UI as other types of correction - echo $NEWLINK | cat --number - # Read the user input - read PICK - if [ $PICK -eq 1 ]; then - # Replace the old link with the new one. - # Parentheses are placed around both the old link and new one in order to ensure we replace the link, - # and not some other place in the file that happens to use the same words. - REPLACE=$(escape '('"$link"')') - REPLACEWITH=$(escapeReplace "$NEWLINK") - sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" + echo "$NEWLINK" | cat --number + if [ "$SCRIPT" -lt 1 ]; then + # Read the user input + read -r PICK + if [ "$PICK" -eq 1 ]; then + # Replace the old link with the new one. + # Parentheses are placed around both the old link and new one in order to ensure we replace the link, + # and not some other place in the file that happens to use the same words. + REPLACE=$(escape '('"$link"')') + REPLACEWITH=$(escapeReplace "$NEWLINK") + sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" + fi + # We don't continue here because the link we fixed might be broken. fi - # We don't continue here because the link we fixed might be broken. fi # Skip links that are to an .md file and aren't broken. - if [ $(find . -name "$link"".md" 2>/dev/null | wc -l) -gt 0 ]; then + if [ "$(find . -name "$link"".md" 2>/dev/null | wc -l)" -gt 0 ]; then continue fi # Skip links that are to a hash fragment. @@ -72,7 +74,7 @@ searchfile() { fi # Print the filename and the broken link. echo "In $1:" - echo $link + echo "$link" # Build the search term we will look for. # All hyphens and underscores are replaced with asterisks, so we # can find files with mismatched hyphens or underscores. @@ -80,30 +82,32 @@ searchfile() { # Search for matching files. FILES=$(find . -iname "$SEARCH") # If there are no files, skip to next link. - if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then + if [ "$(echo -n "$FILES" | wc -c)" -lt 1 ]; then echo "Could not find" continue fi # List the potential files, with numbers. echo "$FILES" | cat --number - # Read the user input - read PICK - # If the selection isn't a number, skip to the next link. - if ! [[ $PICK =~ ^[0-9]+$ ]]; then - continue + if [ "$SCRIPT" -lt 1 ]; then + # Read the user input + read -r PICK + # If the selection isn't a number, skip to the next link. + if ! [[ $PICK =~ ^[0-9]+$ ]]; then + continue + fi + # Get the selected file path, without the preceding ./ + FILE=$(echo "$FILES" | head -n "$PICK" | tail -n 1 | sed 's/^\.\///') + # Replace the old link with the new one. + # Parentheses are placed around both the old link and new one in order to ensure we replace the link, + # and not some other place in the file that happens to use the same words. + REPLACE=$(escape '('"$link"')') + REPLACEWITH=$(escapeReplace "$(basename "$FILE" .md)") + sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" fi - # Get the selected file path, without the preceding ./ - FILE=$(echo "$FILES" | head -n $PICK | tail -n 1 | sed 's/^\.\///') - # Replace the old link with the new one. - # Parentheses are placed around both the old link and new one in order to ensure we replace the link, - # and not some other place in the file that happens to use the same words. - REPLACE=$(escape '('"$link"')') - REPLACEWITH=$(escapeReplace "$(basename "$FILE" .md)") - sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" continue fi echo "In $1:" - echo $link + echo "$link" # Build the search term we will look for. # All hyphens and underscores are replaced with asterisks, so we # can find files with mismatched hyphens or underscores. @@ -111,31 +115,50 @@ searchfile() { # Search for matching files. FILES=$(find . -iname "$SEARCH") # If there are no files, skip to next link. - if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then + if [ "$(echo -n "$FILES" | wc -c)" -lt 1 ]; then echo "Could not find" continue fi # List the potential files, with numbers. echo "$FILES" | cat --number - # Read the user input - read PICK - # If the selection isn't a number, skip to the next link. - if ! [[ $PICK =~ ^[0-9]+$ ]]; then - continue + if [ "$SCRIPT" -lt 1 ]; then + # Read the user input + read -r PICK + # If the selection isn't a number, skip to the next link. + if ! [[ $PICK =~ ^[0-9]+$ ]]; then + continue + fi + # Get the selected file path, without the preceding ./ + FILE=$(basename "$(echo "$FILES" | head -n "$PICK" | tail -n 1)" .md) + # Replace the old link with the new one. + # Parentheses are placed around both the old link and new one in order to ensure we replace the link, + # and not some other place in the file that happens to use the same words. + REPLACE=$(escape '('"$link"')') + REPLACEWITH=$(escapeReplace "$FILE") + sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" fi - # Get the selected file path, without the preceding ./ - FILE=$(basename "$(echo "$FILES" | head -n $PICK | tail -n 1)" .md) - # Replace the old link with the new one. - # Parentheses are placed around both the old link and new one in order to ensure we replace the link, - # and not some other place in the file that happens to use the same words. - REPLACE=$(escape '('$link')') - REPLACEWITH=$(escapeReplace "$FILE") - sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" # This regex finds links in the file that is passed to searchfile # Results are fed to file descriptor 3 for the reasons previously explained. done 3< <(grep -oP '(?<=\]\().*?(?=[\)])' "$1" | sed -e "s/^$//g" | cut -d '"' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') } export -f searchfile -# run searchfile on every .md file in the repo -find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \; +FILES=() + +export SCRIPT=0 +for i in $@; do + if [ "$i" == "-s" ]; then + export SCRIPT=1 + else + FILES+=("${i}") + fi +done + +if [ "${#FILES[@]}" -gt 0 ]; then + for f in "${FILES[@]}"; do + searchfile "$f" + done +else + # run searchfile on every .md file in the repo + find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \; +fi