gen-wiki.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env bash
  2. # Write the GitHub wiki tree for this repository from docs/wiki/.
  3. #
  4. # The pages are written and reviewed in the main repository, so pull requests,
  5. # secret scanning and review cover them. Publishing is not done here: the Wiki
  6. # workflow (.github/workflows/wiki.yml) runs this script as the generate step
  7. # of git-your-ship-together's wiki-publish.yml, which pushes the tree to the
  8. # wiki from main only and never from a pull request. Edits made in the wiki
  9. # editor are overwritten on the next publish, and a page that is not in
  10. # docs/wiki/ is removed from the wiki.
  11. #
  12. # Usage, from anywhere inside a clone:
  13. # scripts/gen-wiki.sh OUT_DIR
  14. #
  15. # OUT_DIR must not exist or must be empty: the whole directory is published, so
  16. # a leftover file would be published too. A non-empty one is refused.
  17. #
  18. # What it changes in each page: relative links such as (Twingate.md) or
  19. # (Twingate.md#section) become (Twingate) and (Twingate#section), which is how
  20. # the wiki addresses pages. Absolute links are left alone.
  21. #
  22. # Images: the .png files in docs/wiki/images/ are copied flat into OUT_DIR (the
  23. # wiki has no directories worth using), and an image link
  24. # ![alt](images/name.png) becomes an absolute raw URL,
  25. # https://raw.githubusercontent.com/wiki/OWNER/REPO/name.png
  26. # OWNER/REPO come from GITHUB_REPOSITORY (set in Actions), else from
  27. # WIKI_REPO, else the upstream repository, and must look like a GitHub
  28. # owner/repository name or the script stops. The value is handed to awk as a
  29. # variable, never written into a program. Links inside fenced code blocks,
  30. # indented code blocks and inline code are left as written, so a documentation
  31. # example shows what the author typed. docs/wiki/images/ may hold only regular
  32. # .png files; a symlink, a subdirectory or any other file is an error, and so is
  33. # a symlinked page.
  34. set -euo pipefail
  35. die() {
  36. echo "gen-wiki: $*" >&2
  37. exit "${2:-1}"
  38. }
  39. if [ "$#" -ne 1 ] || [ -z "$1" ]; then
  40. echo "usage: $0 OUT_DIR" >&2
  41. exit 2
  42. fi
  43. out=$1
  44. repo_root=$(cd "$(dirname "$0")/.." && pwd)
  45. src="$repo_root/docs/wiki"
  46. [ -d "$src" ] || die "$src not found"
  47. wiki_repo=${GITHUB_REPOSITORY:-${WIKI_REPO:-ChiefGyk3D/Clouds-Against-Humanity}}
  48. # Owner and repository names: letters, digits, dash, underscore, dot; one slash.
  49. # Names made only of dots are path segments, not repositories.
  50. if ! [[ $wiki_repo =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] \
  51. || [[ ${wiki_repo%%/*} =~ ^\.+$ ]] || [[ ${wiki_repo##*/} =~ ^\.+$ ]]; then
  52. die "GITHUB_REPOSITORY/WIKI_REPO is not an owner/repository name" 2
  53. fi
  54. raw_base="https://raw.githubusercontent.com/wiki/$wiki_repo"
  55. if [ -e "$out" ] || [ -L "$out" ]; then
  56. [ -d "$out" ] && [ ! -L "$out" ] || die "$out exists and is not a plain directory" 2
  57. [ -z "$(ls -A "$out")" ] || die "$out is not empty; give an empty or new directory" 2
  58. fi
  59. # Check the sources before writing anything.
  60. if [ -L "$src/images" ]; then
  61. die "$src/images is a symlink"
  62. fi
  63. if [ -d "$src/images" ]; then
  64. for entry in "$src"/images/* "$src"/images/.[!.]*; do
  65. [ -e "$entry" ] || [ -L "$entry" ] || continue
  66. [ -L "$entry" ] && die "$entry is a symlink; images must be regular files"
  67. [ -f "$entry" ] || die "$entry is not a regular file"
  68. case "$(basename "$entry")" in
  69. *.png) ;;
  70. *) die "$entry: only .png files are allowed in docs/wiki/images/" ;;
  71. esac
  72. done
  73. fi
  74. for page in "$src"/*.md; do
  75. [ -e "$page" ] || [ -L "$page" ] || continue
  76. [ -L "$page" ] && die "$page is a symlink; pages must be regular files"
  77. done
  78. # Rewrites ](images/NAME.png) to ](RAW/NAME.png) outside code.
  79. # Fences open and close on a line starting with three or more backticks or
  80. # tildes. An indented code block is a line indented four spaces or a tab after
  81. # a blank line, unless the text above it is a list item (then it is a nested
  82. # list line). Inline code spans (between single backticks) are skipped.
  83. awk_prog='
  84. function rewrite(s, res, name) {
  85. res = ""
  86. while (match(s, /\]\(images\/[A-Za-z0-9_-]+\.png\)/)) {
  87. name = substr(s, RSTART + 9, RLENGTH - 10)
  88. res = res substr(s, 1, RSTART - 1) "](" raw "/" name ")"
  89. s = substr(s, RSTART + RLENGTH)
  90. }
  91. return res s
  92. }
  93. function prose(line, n, parts, i, res) {
  94. n = split(line, parts, "`")
  95. res = ""
  96. for (i = 1; i <= n; i++) {
  97. res = res (i > 1 ? "`" : "") ((i % 2 == 1) ? rewrite(parts[i]) : parts[i])
  98. }
  99. return res
  100. }
  101. BEGIN { fence = 0; prev_blank = 1; inlist = 0 }
  102. {
  103. line = $0
  104. if (line ~ /^[ ]*(```|~~~)/) { fence = !fence; print line; prev_blank = 0; next }
  105. if (fence) { print line; next }
  106. if (line ~ /^[ ]*$/) { print line; prev_blank = 1; next }
  107. if (line ~ /^( |\t)/ && prev_blank && !inlist) { print line; prev_blank = 0; next }
  108. if (line !~ /^( |\t)/) { inlist = (line ~ /^[ ]?[ ]?[ ]?([-*+]|[0-9]+[.)]) /) }
  109. print prose(line)
  110. prev_blank = 0
  111. }
  112. '
  113. mkdir -p "$out"
  114. count=0
  115. for page in "$src"/*.md; do
  116. [ -e "$page" ] || continue
  117. name=$(basename "$page")
  118. sed -E 's/\]\(([A-Za-z0-9_-]+)\.md(#[^)]*)?\)/](\1\2)/g' "$page" \
  119. | awk -v raw="$raw_base" "$awk_prog" > "$out/$name"
  120. count=$((count + 1))
  121. done
  122. if [ -d "$src/images" ]; then
  123. for img in "$src"/images/*.png; do
  124. [ -e "$img" ] || continue
  125. cp "$img" "$out/$(basename "$img")"
  126. done
  127. fi
  128. # An empty tree would empty the wiki; wiki-publish.yml refuses one too, but
  129. # failing here names the cause.
  130. if [ "$count" -eq 0 ]; then
  131. echo "gen-wiki: no pages in $src" >&2
  132. exit 1
  133. fi
  134. echo "gen-wiki: wrote $count pages to $out"