rusefi_documentation/wiki-tools/linkifyurls.sh

66 lines
2.5 KiB
Bash
Raw Permalink Normal View History

2022-01-24 14:16:31 -08:00
#!/usr/bin/env bash
2022-01-25 15:44:44 -08:00
##################################################################################################
# linkifyurls.sh #
# 01/24/2022 #
# Written By David Holdeman #
# Searches for URLs in a Github Wiki repo, and turns them into markdown links. #
##################################################################################################
2022-01-24 14:16:31 -08:00
# These two functions are used to escape variables for use in a sed command
# Passed a single string
escape() {
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1";
}
export -f escape
escapeReplace() {
sed 's/[&/\]/\\&/g' <<<"$1";
}
export -f escapeReplace
# Main processing function
# Passed the path to a .md file
searchfile() {
2022-01-25 15:44:44 -08:00
# This loops for every link in the file.
# See the end of the function for the grep that finds the links in the file.
# We use file descriptor 3, because if we used stdin, the read calls inside this loop would read from that instead of
# reading the user's input.
2022-01-24 14:16:31 -08:00
while IFS= read -r -u 3 link; do
2022-01-25 15:44:44 -08:00
# Get the line the link was in
2022-01-24 14:59:53 -08:00
LINE=$(grep "$link" "$1")
2022-01-25 15:44:44 -08:00
# If the line is different than the link, print it so the user can see the context of the link.
2022-01-24 14:59:53 -08:00
if [ "$LINE" != "$link" ]; then
echo "$LINE"
fi
2022-01-25 15:44:44 -08:00
# Print the link and prompt for a title
2022-01-24 14:16:31 -08:00
echo $link
2022-01-24 15:42:36 -08:00
read -p "Enter a title or leave empty or [n:skip|e:edit]: " TITLE
2022-01-25 15:44:44 -08:00
# Do nothing if the user entered 'n'
2022-01-24 15:04:18 -08:00
if [ "$TITLE" == "n" ]; then
2022-01-24 15:19:05 -08:00
echo
2022-01-24 15:04:18 -08:00
continue
fi
2022-01-25 15:44:44 -08:00
# Open the file in an editor if the user entered 'e'
2022-01-24 15:42:36 -08:00
if [ "$TITLE" == "e" ]; then
$EDITOR "$1"
echo
continue
fi
2022-01-25 15:44:44 -08:00
# Set the title to the URL if the user didn't enter anything
2022-01-24 14:16:31 -08:00
if [ $(echo -n "$TITLE" | wc -c) -lt 1 ]; then
TITLE="$link"
fi
2022-01-25 15:44:44 -08:00
# Build a markdown-style link
2022-01-24 14:16:31 -08:00
REPLACE=$(escape "$link")
REPLACEWITH=$(escapeReplace '['"$TITLE"']''('"$link"')')
2022-01-25 15:44:44 -08:00
# Replace the old URL with the new link in the file
2022-01-24 14:16:31 -08:00
sed -i "s/$REPLACE/$REPLACEWITH/" "$1"
2022-01-24 15:16:48 -08:00
echo
2022-01-25 15:44:44 -08:00
# This regex finds URLs in the file that is passed to searchfile
# Results are fed to file descriptor 3 for the reasons previously explained.
2022-01-24 14:50:04 -08:00
done 3< <(grep -oP '((?<![\(\[])http[s]?:\/\/[^\s]*)' "$1")
2022-01-24 14:16:31 -08:00
}
export -f searchfile
# run searchfile on every .md file in the repo
find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;