From c7c9b5d848e31668625629da82a58a465c829408 Mon Sep 17 00:00:00 2001 From: David Holdeman Date: Mon, 15 Feb 2021 20:21:10 -0600 Subject: [PATCH] add link fixing tool --- wiki-tools/brokenlinks.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 wiki-tools/brokenlinks.sh diff --git a/wiki-tools/brokenlinks.sh b/wiki-tools/brokenlinks.sh new file mode 100644 index 00000000..3c0b09c9 --- /dev/null +++ b/wiki-tools/brokenlinks.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +escape() { + sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1"; +} +export -f escape +escapeReplace() { + sed 's/[&/\]/\\&/g' <<<"$1"; +} +export -f escapeReplace +searchfile() { + LINKS=$(grep -oP '(?<=\]\().+?(?=[\)| ])' "$1") + for link in $LINKS; do + if echo $link | grep -E '^http' >/dev/null; then + continue + fi + if echo $link | grep -E ".md$" && find . -name "$link"; then + continue + fi + echo $link + FILES=$(find . -iname "*$(basename $link)*") + echo "$FILES" | cat --number + read PICK + FILE=$(basename $(echo "$FILES" | head -n $PICK | tail -n 1) .md) + sed -i "s/$(escape '('$link')')/\($(escapeReplace $FILE)\)/" "$1" + done +} +export -f searchfile +find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;