This commit is contained in:
David Holdeman 2023-01-18 16:12:04 -06:00
parent 458957dc12
commit e6ac35b8be
1 changed files with 21 additions and 20 deletions

View File

@ -23,18 +23,18 @@ export -f escapeReplace
checkurl() { checkurl() {
# If it's an internet link, ignore it. # If it's an internet link, ignore it.
# That's beyond the scope of this tool. # That's beyond the scope of this tool.
if echo "$1" | grep -E '^[http|\/]' >/dev/null; then if echo "$2" | grep -E '^[http|\/]' >/dev/null; then
return 1 return 1
fi fi
# At some point in this scripts development, fixed links to files/images were given the './' prefix. # At some point in this scripts development, fixed links to files/images were given the './' prefix.
# This didn't really hurt anything, but it's not idiomatic. # This didn't really hurt anything, but it's not idiomatic.
# I added this to fix the problems I caused, and decided it was worth keeping around. # I added this to fix the problems I caused, and decided it was worth keeping around.
if echo "$1" | grep -E '^\./' >/dev/null; then if echo "$2" | grep -E '^\./' >/dev/null; then
# NEWLINK is the corrected link # NEWLINK is the corrected link
NEWLINK=$(echo "$1" | sed 's/^\.\///') NEWLINK=$(echo "$2" | sed 's/^\.\///')
# Print the file and the old link # Print the file and the old link
echo "In $1:" >&2 echo "In $2:" >&2
echo "$1" >&2 echo "$2" >&2
# Print the options as though they are a list in order to have the same UI as other types of correction # 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 >&2 echo "$NEWLINK" | cat --number >&2
if [ "$SCRIPT" -lt 1 ]; then if [ "$SCRIPT" -lt 1 ]; then
@ -44,7 +44,7 @@ checkurl() {
# Replace the old link with the new one. # 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, # 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. # and not some other place in the file that happens to use the same words.
REPLACE=$(escape '('"$1"')') REPLACE=$(escape '('"$2"')')
REPLACEWITH=$(escapeReplace "$NEWLINK") REPLACEWITH=$(escapeReplace "$NEWLINK")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
# print the new URL for use in checkhash # print the new URL for use in checkhash
@ -54,27 +54,27 @@ checkurl() {
fi fi
fi fi
# Skip links that are to an .md file and aren't broken. # Skip links that are to an .md file and aren't broken.
if [ "$(find . -name "$1"".md" 2>/dev/null | wc -l)" -gt 0 ]; then if [ "$(find . -name "$2"".md" 2>/dev/null | wc -l)" -gt 0 ]; then
# print the URL for use in checkhash # print the URL for use in checkhash
echo "$1" echo "$2"
return 0 return 0
fi fi
# Process links that are not to an .md file. # Process links that are not to an .md file.
# We need to do this separately because Github/gollum behave differently with different kinds of links. # We need to do this separately because Github/gollum behave differently with different kinds of links.
# .md files need the name of the file, without the .md extension. # .md files need the name of the file, without the .md extension.
# Everything else needs the path relative to the repo root. # Everything else needs the path relative to the repo root.
if echo "$1" | grep -Ev ".md$" >/dev/null; then if echo "$2" | grep -Ev ".md$" >/dev/null; then
# Skip the link if it's not broken. # Skip the link if it's not broken.
if ls "$1" 2>/dev/null >/dev/null; then if ls "$2" 2>/dev/null >/dev/null; then
return 1 return 1
fi fi
# Print the filename and the broken link. # Print the filename and the broken link.
echo "In $1:" >&2 echo "In $2:" >&2
echo "$1" >&2 echo "$2" >&2
# Build the search term we will look for. # Build the search term we will look for.
# All hyphens and underscores are replaced with asterisks, so we # All hyphens and underscores are replaced with asterisks, so we
# can find files with mismatched hyphens or underscores. # can find files with mismatched hyphens or underscores.
SEARCH='*'$(basename "$1" | sed 's/[-_ ]/*/g')'*' SEARCH='*'$(basename "$2" | sed 's/[-_ ]/*/g')'*'
# Search for matching files. # Search for matching files.
FILES=$(find . -iname "$SEARCH") FILES=$(find . -iname "$SEARCH")
# If there are no files, skip to next link. # If there are no files, skip to next link.
@ -96,18 +96,18 @@ checkurl() {
# Replace the old link with the new one. # 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, # 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. # and not some other place in the file that happens to use the same words.
REPLACE=$(escape '('"$1"')') REPLACE=$(escape '('"$2"')')
REPLACEWITH=$(escapeReplace "$(basename "$FILE" .md)") REPLACEWITH=$(escapeReplace "$(basename "$FILE" .md)")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
fi fi
return 1 return 1
fi fi
echo "In $1:" >&2 echo "In $2:" >&2
echo "$1" >&2 echo "$2" >&2
# Build the search term we will look for. # Build the search term we will look for.
# All hyphens and underscores are replaced with asterisks, so we # All hyphens and underscores are replaced with asterisks, so we
# can find files with mismatched hyphens or underscores. # can find files with mismatched hyphens or underscores.
SEARCH='*'$(basename "$1" | sed 's/[-_ ]/*/g')'*' SEARCH='*'$(basename "$2" | sed 's/[-_ ]/*/g')'*'
# Search for matching files. # Search for matching files.
FILES=$(find . -iname "$SEARCH") FILES=$(find . -iname "$SEARCH")
# If there are no files, skip to next link. # If there are no files, skip to next link.
@ -126,10 +126,11 @@ checkurl() {
fi fi
# Get the selected file path, without the preceding ./ # Get the selected file path, without the preceding ./
FILE=$(basename "$(echo "$FILES" | head -n "$PICK" | tail -n 1)" .md) FILE=$(basename "$(echo "$FILES" | head -n "$PICK" | tail -n 1)" .md)
echo "foo $FILE" >&2
# Replace the old link with the new one. # 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, # 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. # and not some other place in the file that happens to use the same words.
REPLACE=$(escape '('"$1"')') REPLACE=$(escape '('"$2"')')
REPLACEWITH=$(escapeReplace "$FILE") REPLACEWITH=$(escapeReplace "$FILE")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1" sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
echo "$FILE" echo "$FILE"
@ -157,11 +158,11 @@ searchfile() {
HASH=$(echo "$LINK" | cut -d '#' -f 2) HASH=$(echo "$LINK" | cut -d '#' -f 2)
URLSTATUS=0 URLSTATUS=0
if [ -n "$URL" ]; then if [ -n "$URL" ]; then
URL=$(checkurl "$URL" "$HASH") URL=$(checkurl "$1" "$URL" "$HASH")
URLSTATUS=$? URLSTATUS=$?
fi fi
if [ -n "$HASH" ] && [ "$URLSTATUS" -eq 0 ]; then if [ -n "$HASH" ] && [ "$URLSTATUS" -eq 0 ]; then
checkhash "$HASH" "$URL" checkhash "$1" "$HASH" "$URL"
fi fi
# This regex finds links in the file that is passed to searchfile # This regex finds links in the file that is passed to searchfile
# Results are fed to file descriptor 3 for the reasons previously explained. # Results are fed to file descriptor 3 for the reasons previously explained.