|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 5 | +MODULES_DIR="${REPO_ROOT}/modules" |
| 6 | +PAGES_DIR="${MODULES_DIR}/ROOT/pages" |
| 7 | +PARTIALS_DIR="${MODULES_DIR}/ROOT/partials" |
| 8 | +NAV_FILE="${MODULES_DIR}/ROOT/nav.adoc" |
| 9 | + |
| 10 | +ERROR_LOG=$(mktemp) |
| 11 | +WORK_DIR=$(mktemp -d) |
| 12 | +trap 'rm -f "${ERROR_LOG}"; rm -rf "${WORK_DIR}"' EXIT |
| 13 | + |
| 14 | +error() { |
| 15 | + echo "ERROR: $1" >&2 |
| 16 | + echo "x" >> "${ERROR_LOG}" |
| 17 | +} |
| 18 | + |
| 19 | +warn() { |
| 20 | + echo "WARN: $1" >&2 |
| 21 | +} |
| 22 | + |
| 23 | +# --- AsciiDoc syntax validation --- |
| 24 | +validate_syntax() { |
| 25 | + echo "==> Validating AsciiDoc syntax..." |
| 26 | + |
| 27 | + if ! command -v asciidoctor &>/dev/null; then |
| 28 | + warn "asciidoctor not found, skipping syntax validation." |
| 29 | + warn "Install with: gem install asciidoctor" |
| 30 | + return |
| 31 | + fi |
| 32 | + |
| 33 | + # Antora's partial$ prefix isn't understood by standalone asciidoctor. |
| 34 | + # Resolve it to the actual partials path in a temp copy. |
| 35 | + cp -a "${PAGES_DIR}" "${WORK_DIR}/pages" |
| 36 | + find "${WORK_DIR}/pages" -name '*.adoc' -exec \ |
| 37 | + sed -i "s|include::partial\\\$|include::${PARTIALS_DIR}/|g" {} + |
| 38 | + |
| 39 | + local syntax_errors=0 |
| 40 | + while IFS= read -r -d '' file; do |
| 41 | + local basename="${file##*/}" |
| 42 | + local output |
| 43 | + output=$(asciidoctor \ |
| 44 | + --failure-level=WARN \ |
| 45 | + --backend=html5 \ |
| 46 | + --safe-mode=safe \ |
| 47 | + --out-file=/dev/null \ |
| 48 | + "$file" 2>&1) || true |
| 49 | + |
| 50 | + if [[ -n "$output" ]]; then |
| 51 | + echo "$output" | while IFS= read -r line; do |
| 52 | + echo " $line" |
| 53 | + done |
| 54 | + syntax_errors=$((syntax_errors + 1)) |
| 55 | + fi |
| 56 | + done < <(find "${WORK_DIR}/pages" -name '*.adoc' -print0) |
| 57 | + |
| 58 | + if [[ ${syntax_errors} -gt 0 ]]; then |
| 59 | + error "${syntax_errors} file(s) produced asciidoctor warnings or errors" |
| 60 | + fi |
| 61 | +} |
| 62 | + |
| 63 | +# --- xref validation --- |
| 64 | +validate_xrefs() { |
| 65 | + echo "==> Validating xref targets..." |
| 66 | + |
| 67 | + while IFS= read -r -d '' file; do |
| 68 | + local relpath="${file#"${REPO_ROOT}/"}" |
| 69 | + local matches |
| 70 | + matches=$(grep -noP 'xref:([^\[]+)\[' "$file" 2>/dev/null) || continue |
| 71 | + |
| 72 | + while IFS=: read -r lineno match; do |
| 73 | + local target="${match#xref:}" |
| 74 | + target="${target%\[}" |
| 75 | + target="${target%%#*}" |
| 76 | + |
| 77 | + # Skip cross-component xrefs (contain component:module: prefix) |
| 78 | + if [[ "$target" == *:* ]]; then |
| 79 | + continue |
| 80 | + fi |
| 81 | + |
| 82 | + if [[ ! -f "${PAGES_DIR}/${target}" ]]; then |
| 83 | + error "${relpath}:${lineno}: broken xref to '${target}' (file not found in pages/)" |
| 84 | + fi |
| 85 | + done <<< "$matches" |
| 86 | + done < <(find "${MODULES_DIR}" -name '*.adoc' -print0) |
| 87 | +} |
| 88 | + |
| 89 | +# --- include validation --- |
| 90 | +validate_includes() { |
| 91 | + echo "==> Validating include targets..." |
| 92 | + |
| 93 | + while IFS= read -r -d '' file; do |
| 94 | + local relpath="${file#"${REPO_ROOT}/"}" |
| 95 | + local matches |
| 96 | + matches=$(grep -noP 'include::[^\[]+\[' "$file" 2>/dev/null) || continue |
| 97 | + |
| 98 | + while IFS=: read -r lineno match; do |
| 99 | + local target="${match#include::}" |
| 100 | + target="${target%\[}" |
| 101 | + |
| 102 | + if [[ "$target" == partial\$* ]]; then |
| 103 | + local partial_name="${target#partial\$}" |
| 104 | + if [[ ! -f "${PARTIALS_DIR}/${partial_name}" ]]; then |
| 105 | + error "${relpath}:${lineno}: broken include, partial '${partial_name}' not found in partials/" |
| 106 | + fi |
| 107 | + fi |
| 108 | + done <<< "$matches" |
| 109 | + done < <(find "${MODULES_DIR}" -name '*.adoc' -print0) |
| 110 | +} |
| 111 | + |
| 112 | +# --- nav validation --- |
| 113 | +validate_nav() { |
| 114 | + echo "==> Validating navigation references..." |
| 115 | + |
| 116 | + if [[ ! -f "${NAV_FILE}" ]]; then |
| 117 | + error "Navigation file not found: ${NAV_FILE}" |
| 118 | + return |
| 119 | + fi |
| 120 | + |
| 121 | + for navfile in "${NAV_FILE}" "${PARTIALS_DIR}/contents.adoc"; do |
| 122 | + [[ -f "$navfile" ]] || continue |
| 123 | + local relpath="${navfile#"${REPO_ROOT}/"}" |
| 124 | + local matches |
| 125 | + matches=$(grep -noP 'xref:([^\[]+)\[' "$navfile" 2>/dev/null) || continue |
| 126 | + |
| 127 | + while IFS=: read -r lineno match; do |
| 128 | + local target="${match#xref:}" |
| 129 | + target="${target%\[}" |
| 130 | + target="${target%%#*}" |
| 131 | + |
| 132 | + if [[ "$target" == *:* ]]; then |
| 133 | + continue |
| 134 | + fi |
| 135 | + |
| 136 | + if [[ ! -f "${PAGES_DIR}/${target}" ]]; then |
| 137 | + error "${relpath}:${lineno}: nav references non-existent page '${target}'" |
| 138 | + fi |
| 139 | + done <<< "$matches" |
| 140 | + done |
| 141 | +} |
| 142 | + |
| 143 | +echo "Validating documentation in ${REPO_ROOT}..." |
| 144 | +echo |
| 145 | + |
| 146 | +validate_syntax |
| 147 | +validate_xrefs |
| 148 | +validate_includes |
| 149 | +validate_nav |
| 150 | + |
| 151 | +echo |
| 152 | +error_count=$(wc -l < "${ERROR_LOG}" | tr -d ' ') |
| 153 | +if [[ ${error_count} -gt 0 ]]; then |
| 154 | + echo "FAILED: ${error_count} error(s) found." |
| 155 | + exit 1 |
| 156 | +else |
| 157 | + echo "PASSED: All validation checks passed." |
| 158 | + exit 0 |
| 159 | +fi |
0 commit comments