在咕噜维基中,如何找到孤儿页面 - 即没有链接的页面?
我尝试编写一个 bash 脚本(见下文(,但它似乎不适用于名称中包含特殊字符 (, (,/的页面。
#!/bin/bash
found=""
find . -name HelpPages -prune -o -name '*.md' -type f -print0 | while IFS= read -r -d '' f; do
escaped=$(echo $f | sed -e "s/.md//g")
escaped=$(echo $escaped | sed -e "s/.///g")
escaped=$(echo $escaped | sed -e "s/(/\\(/g")
escaped=$(echo $escaped | sed -e "s/)/\\)/g")
escaped=$(echo $escaped | sed -e "s/.///g")
escaped=$(echo $escaped | sed -E 's/[- ]/[- /]/g')
escaped=$(echo $escaped | sed -E 's/ /\ /g')
found=$(grep -i -r "[|[]$escaped]" *.md)
echo "_________"
echo "Processing $escaped file..."
if [ -n "$found" ]; then
echo "found: $f"
echo "$found"
else
echo "NOT found: $f"
fi
done
此脚本首先查找目录中与*.md
文件名模式匹配的文件。然后,对于每个文件名(如 The-event-log-(errors-warnings).md
(,脚本需要找到链接,该链接可以是以下任何一项:
[[The event log (errors/warnings)]]
[[The event log (errors warnings)]]
[[The-event-log-(errors-warnings)]]
[[The event log|The-event-log-(errors-warnings)]]
原来我有一些不好的特殊字符转义。现成脚本(也可在 Gist 中使用(:
#!/bin/bash
#Usage:
#./find-links.sh #shows linked pages and orphans
#./find-links.sh --html #shows linked pages and orphans with HTML links
#./find-links.sh --md #shows linked pages and orphans with Markdown links
#./find-links.sh --orphans #shows only orphans
#./find-links.sh --orphans --html #shows only orphans with HTML links
#./find-links.sh --linked #shows only linked pages
#./find-links.sh --linked --html #shows only linked pages with HTML links
outputOrphans=1
outputLinked=1
outputHtml=""
outputMarkdown=""
while test $# -gt 0
do
case "$1" in
--orphans) outputLinked=""
;;
--linked) outputOrphans=""
;;
--html) outputHtml=1
;;
--md) outputMarkdown=1
esac
shift
done
found=""
find . -name HelpPages -prune -o -name '*.md' -type f -print0 | while IFS= read -r -d '' f; do
escaped=$(echo $f | sed -e "s/.md//g")
escaped=$(echo $escaped | sed -e "s/.///g")
escaped=$(echo $escaped | sed -e "s/(/\\(/g")
escaped=$(echo $escaped | sed -e "s/)/\\)/g")
escaped=$(echo $escaped | sed -e "s/.///g")
escaped=$(echo $escaped | sed -E 's/[- ]/[- /]/g')
escaped=$(echo $escaped | sed -E 's/ /\ /g')
if [ -n "$outputHtml" ]; then
linktext=$(echo $f | sed -e "s/.md//g")
linktext=$(echo $linktext | sed -e "s/.///g")
linktext=$(echo $linktext | sed -E 's/[- ]/ /g')
url=$(echo $f | sed -e "s/.md//g")
url=$(echo $url | sed -e "s/.///g")
url=$(echo https://github.com/Starcounter/Starcounter/wiki/$url)
output=$(echo "<a href='$url'>$linktext</a>")
elif [ -n "$outputMarkdown" ]; then
linktext=$(echo $f | sed -e "s/.md//g")
linktext=$(echo $linktext | sed -e "s/.///g")
linktext=$(echo $linktext | sed -E 's/[- ]/ /g')
url=$(echo $f | sed -e "s/.md//g")
url=$(echo $url | sed -e "s/.///g")
url=$(echo https://github.com/Starcounter/Starcounter/wiki/$url)
output=$(echo "- [$linktext]($url)")
else
output=$f
fi
found=$(grep -E -i -r "[|[]$escaped]" *.md)
if [ -n "$found" ]; then
if [ -n "$outputLinked" ]; then
echo $output
found=$(echo "$found" | sed "s/^/ /g") #indent
echo "$found"
fi
else
if [ -n "$outputOrphans" ]; then
echo $output
fi
fi
done