如何在Gollum找到孤儿页面

时间:2014-12-18 07:03:03

标签: bash gollum-wiki

在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)]]

1 个答案:

答案 0 :(得分:1)

原来,我有一些不好的特殊角色逃脱。准备好的脚本(also available in 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