0

在 Gollum wiki 中,我如何找到孤立页面 - 即没有指向它们的链接的页面?

我曾尝试编写一个 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)]]
4

1 回答 1

1

原来我有一些特殊字符的错误转义。准备好的脚本(也可以在 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
于 2014-12-18T12:15:21.323 回答