add file passing, non-interactive param, and fix lint errors (#392)
This commit is contained in:
parent
276904bd87
commit
1ec7007d72
|
@ -27,7 +27,7 @@ searchfile() {
|
||||||
while IFS= read -r -u 3 link; do
|
while IFS= read -r -u 3 link; do
|
||||||
# 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 $link | grep -E '^[http|\/]' >/dev/null; then
|
if echo "$link" | grep -E '^[http|\/]' >/dev/null; then
|
||||||
continue
|
continue
|
||||||
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.
|
||||||
|
@ -35,26 +35,28 @@ searchfile() {
|
||||||
# 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 $link | grep -E '^\./' >/dev/null; then
|
if echo $link | grep -E '^\./' >/dev/null; then
|
||||||
# NEWLINK is the corrected link
|
# NEWLINK is the corrected link
|
||||||
NEWLINK=$(echo $link | sed 's/^\.\///')
|
NEWLINK=$(echo "$link" | sed 's/^\.\///')
|
||||||
# Print the file and the old link
|
# Print the file and the old link
|
||||||
echo "In $1:"
|
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
|
# 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
|
echo "$NEWLINK" | cat --number
|
||||||
# Read the user input
|
if [ "$SCRIPT" -lt 1 ]; then
|
||||||
read PICK
|
# Read the user input
|
||||||
if [ $PICK -eq 1 ]; then
|
read -r PICK
|
||||||
# Replace the old link with the new one.
|
if [ "$PICK" -eq 1 ]; then
|
||||||
# Parentheses are placed around both the old link and new one in order to ensure we replace the link,
|
# Replace the old link with the new one.
|
||||||
# and not some other place in the file that happens to use the same words.
|
# Parentheses are placed around both the old link and new one in order to ensure we replace the link,
|
||||||
REPLACE=$(escape '('"$link"')')
|
# and not some other place in the file that happens to use the same words.
|
||||||
REPLACEWITH=$(escapeReplace "$NEWLINK")
|
REPLACE=$(escape '('"$link"')')
|
||||||
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
|
REPLACEWITH=$(escapeReplace "$NEWLINK")
|
||||||
|
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
|
||||||
|
fi
|
||||||
|
# We don't continue here because the link we fixed might be broken.
|
||||||
fi
|
fi
|
||||||
# We don't continue here because the link we fixed might be broken.
|
|
||||||
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 "$link"".md" 2>/dev/null | wc -l) -gt 0 ]; then
|
if [ "$(find . -name "$link"".md" 2>/dev/null | wc -l)" -gt 0 ]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
# Skip links that are to a hash fragment.
|
# Skip links that are to a hash fragment.
|
||||||
|
@ -72,7 +74,7 @@ searchfile() {
|
||||||
fi
|
fi
|
||||||
# Print the filename and the broken link.
|
# Print the filename and the broken link.
|
||||||
echo "In $1:"
|
echo "In $1:"
|
||||||
echo $link
|
echo "$link"
|
||||||
# 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.
|
||||||
|
@ -80,30 +82,32 @@ searchfile() {
|
||||||
# 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.
|
||||||
if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then
|
if [ "$(echo -n "$FILES" | wc -c)" -lt 1 ]; then
|
||||||
echo "Could not find"
|
echo "Could not find"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
# List the potential files, with numbers.
|
# List the potential files, with numbers.
|
||||||
echo "$FILES" | cat --number
|
echo "$FILES" | cat --number
|
||||||
# Read the user input
|
if [ "$SCRIPT" -lt 1 ]; then
|
||||||
read PICK
|
# Read the user input
|
||||||
# If the selection isn't a number, skip to the next link.
|
read -r PICK
|
||||||
if ! [[ $PICK =~ ^[0-9]+$ ]]; then
|
# If the selection isn't a number, skip to the next link.
|
||||||
continue
|
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
|
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
|
continue
|
||||||
fi
|
fi
|
||||||
echo "In $1:"
|
echo "In $1:"
|
||||||
echo $link
|
echo "$link"
|
||||||
# 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.
|
||||||
|
@ -111,31 +115,50 @@ searchfile() {
|
||||||
# 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.
|
||||||
if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then
|
if [ "$(echo -n "$FILES" | wc -c)" -lt 1 ]; then
|
||||||
echo "Could not find"
|
echo "Could not find"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
# List the potential files, with numbers.
|
# List the potential files, with numbers.
|
||||||
echo "$FILES" | cat --number
|
echo "$FILES" | cat --number
|
||||||
# Read the user input
|
if [ "$SCRIPT" -lt 1 ]; then
|
||||||
read PICK
|
# Read the user input
|
||||||
# If the selection isn't a number, skip to the next link.
|
read -r PICK
|
||||||
if ! [[ $PICK =~ ^[0-9]+$ ]]; then
|
# If the selection isn't a number, skip to the next link.
|
||||||
continue
|
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
|
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
|
# 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.
|
||||||
done 3< <(grep -oP '(?<=\]\().*?(?=[\)])' "$1" | sed -e "s/^<//g" -e "s/>$//g" | cut -d '"' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
done 3< <(grep -oP '(?<=\]\().*?(?=[\)])' "$1" | sed -e "s/^<//g" -e "s/>$//g" | cut -d '"' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
||||||
}
|
}
|
||||||
export -f searchfile
|
export -f searchfile
|
||||||
|
|
||||||
# run searchfile on every .md file in the repo
|
FILES=()
|
||||||
find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue