gen-wiki.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. # Navigation (issue #521): the pages in docs/wiki hold only hand-written text and
  19. # the contents block of a long page. The previous/next links, the list of the rest
  20. # of the group and the "In this track: N pages" count are rendered here, from
  21. # docs/wiki/_nav/, by scripts/gen_wiki_nav.py --publish-to, so adding a page
  22. # does not rewrite its neighbors. That needs python3 with PyYAML (the Wiki
  23. # workflow installs it). A tree with no _nav directory (a scratch tree in a test) is
  24. # published as written.
  25. #
  26. # What it changes in each page: relative links such as (Twingate.md) or
  27. # (Twingate.md#section) become (Twingate) and (Twingate#section), which is how
  28. # the wiki addresses pages. Absolute links are left alone.
  29. #
  30. # Images: the .png files in docs/wiki/images/ are copied flat into OUT_DIR (the
  31. # wiki has no directories worth using), and an image link
  32. # ![alt](images/name.png) becomes an absolute raw URL,
  33. # https://raw.githubusercontent.com/wiki/OWNER/REPO/name.png
  34. # OWNER/REPO come from GITHUB_REPOSITORY (set in Actions), else from
  35. # WIKI_REPO, else the upstream repository, and must look like a GitHub
  36. # owner/repository name or the script stops. The value is handed to awk as a
  37. # variable, never written into a program. Links inside fenced code blocks,
  38. # indented code blocks and inline code are left as written, so a documentation
  39. # example shows what the author typed. docs/wiki/images/ may hold only regular
  40. # .png files; a symlink, a subdirectory or any other file is an error, and so is
  41. # a symlinked page.
  42. set -euo pipefail
  43. die() {
  44. echo "gen-wiki: $*" >&2
  45. exit "${2:-1}"
  46. }
  47. if [ "$#" -ne 1 ] || [ -z "$1" ]; then
  48. echo "usage: $0 OUT_DIR" >&2
  49. exit 2
  50. fi
  51. out=$1
  52. repo_root=$(cd "$(dirname "$0")/.." && pwd)
  53. src="$repo_root/docs/wiki"
  54. [ -d "$src" ] || die "$src not found"
  55. wiki_repo=${GITHUB_REPOSITORY:-${WIKI_REPO:-ChiefGyk3D/Clouds-Against-Humanity}}
  56. # Owner and repository names: letters, digits, dash, underscore, dot; one slash.
  57. # Names made only of dots are path segments, not repositories.
  58. if ! [[ $wiki_repo =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] \
  59. || [[ ${wiki_repo%%/*} =~ ^\.+$ ]] || [[ ${wiki_repo##*/} =~ ^\.+$ ]]; then
  60. die "GITHUB_REPOSITORY/WIKI_REPO is not an owner/repository name" 2
  61. fi
  62. raw_base="https://raw.githubusercontent.com/wiki/$wiki_repo"
  63. if [ -e "$out" ] || [ -L "$out" ]; then
  64. [ -d "$out" ] && [ ! -L "$out" ] || die "$out exists and is not a plain directory" 2
  65. [ -z "$(ls -A "$out")" ] || die "$out is not empty; give an empty or new directory" 2
  66. fi
  67. # Check the sources before writing anything.
  68. if [ -L "$src/images" ]; then
  69. die "$src/images is a symlink"
  70. fi
  71. if [ -d "$src/images" ]; then
  72. for entry in "$src"/images/* "$src"/images/.[!.]*; do
  73. [ -e "$entry" ] || [ -L "$entry" ] || continue
  74. [ -L "$entry" ] && die "$entry is a symlink; images must be regular files"
  75. [ -f "$entry" ] || die "$entry is not a regular file"
  76. case "$(basename "$entry")" in
  77. *.png) ;;
  78. *) die "$entry: only .png files are allowed in docs/wiki/images/" ;;
  79. esac
  80. done
  81. fi
  82. for page in "$src"/*.md; do
  83. [ -e "$page" ] || [ -L "$page" ] || continue
  84. [ -L "$page" ] && die "$page is a symlink; pages must be regular files"
  85. done
  86. # Rewrites ](images/NAME.png) to ](RAW/NAME.png) outside code.
  87. # Fences open and close on a line starting with three or more backticks or
  88. # tildes. An indented code block is a line indented four spaces or a tab after
  89. # a blank line, unless the text above it is a list item (then it is a nested
  90. # list line). Inline code spans (between single backticks) are skipped.
  91. awk_prog='
  92. function rewrite(s, res, name) {
  93. res = ""
  94. while (match(s, /\]\(images\/[A-Za-z0-9_-]+\.png\)/)) {
  95. name = substr(s, RSTART + 9, RLENGTH - 10)
  96. res = res substr(s, 1, RSTART - 1) "](" raw "/" name ")"
  97. s = substr(s, RSTART + RLENGTH)
  98. }
  99. return res s
  100. }
  101. function prose(line, n, parts, i, res) {
  102. n = split(line, parts, "`")
  103. res = ""
  104. for (i = 1; i <= n; i++) {
  105. res = res (i > 1 ? "`" : "") ((i % 2 == 1) ? rewrite(parts[i]) : parts[i])
  106. }
  107. return res
  108. }
  109. BEGIN { fence = 0; prev_blank = 1; inlist = 0 }
  110. {
  111. line = $0
  112. if (line ~ /^[ ]*(```|~~~)/) { fence = !fence; print line; prev_blank = 0; next }
  113. if (fence) { print line; next }
  114. if (line ~ /^[ ]*$/) { print line; prev_blank = 1; next }
  115. if (line ~ /^( |\t)/ && prev_blank && !inlist) { print line; prev_blank = 0; next }
  116. if (line !~ /^( |\t)/) { inlist = (line ~ /^[ ]?[ ]?[ ]?([-*+]|[0-9]+[.)]) /) }
  117. print prose(line)
  118. prev_blank = 0
  119. }
  120. '
  121. # Pages with their navigation blocks go to a staging directory; the link rewrite
  122. # below reads from there. Without a manifest the pages are read as written.
  123. pages_src=$src
  124. if [ -d "$src/_nav" ] || [ -f "$src/_nav.yaml" ]; then
  125. stage=$(mktemp -d)
  126. trap 'rm -rf "$stage"' EXIT
  127. python3 -I "$repo_root/scripts/gen_wiki_nav.py" --root "$repo_root" --publish-to "$stage" >&2 \
  128. || die "could not render the page navigation from docs/wiki/_nav/"
  129. pages_src=$stage
  130. fi
  131. mkdir -p "$out"
  132. count=0
  133. for page in "$pages_src"/*.md; do
  134. [ -e "$page" ] || continue
  135. name=$(basename "$page")
  136. sed -E 's/\]\(([A-Za-z0-9_-]+)\.md(#[^)]*)?\)/](\1\2)/g' "$page" \
  137. | awk -v raw="$raw_base" "$awk_prog" > "$out/$name"
  138. count=$((count + 1))
  139. done
  140. if [ -d "$src/images" ]; then
  141. for img in "$src"/images/*.png; do
  142. [ -e "$img" ] || continue
  143. cp "$img" "$out/$(basename "$img")"
  144. done
  145. fi
  146. # An empty tree would empty the wiki; wiki-publish.yml refuses one too, but
  147. # failing here names the cause.
  148. if [ "$count" -eq 0 ]; then
  149. echo "gen-wiki: no pages in $src" >&2
  150. exit 1
  151. fi
  152. echo "gen-wiki: wrote $count pages to $out"