| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
#------------------------------------------------------------------------------ |
| 4 |
# @file |
| 5 |
# Verifies heading structure across the site. |
| 6 |
# |
| 7 |
# Every Markdown file under content/en must not contain a level 1, level 5, |
| 8 |
# or level 6 heading. In addition, every Markdown file within the |
| 9 |
# directories listed in the sections array in the main function below, |
| 10 |
# excluding _index.md files and the paths listed in the exclusions array, |
| 11 |
# must contain exactly two level 2 headings, in this order: "Usage" and |
| 12 |
# "Examples". Reports one error message per file per check that does not |
| 13 |
# comply. |
| 14 |
#------------------------------------------------------------------------------ |
| 15 |
|
| 16 |
#------------------------------------------------------------------------------ |
| 17 |
# @function |
| 18 |
# Displays usage message. |
| 19 |
# |
| 20 |
# @return string $msg |
| 21 |
# The usage message. |
| 22 |
#------------------------------------------------------------------------------ |
| 23 |
usage() { |
| 24 |
declare msg |
| 25 |
msg=$(cat <<EOT |
| 26 |
Verify heading structure across the site: no level 1, level 5, or level 6 |
| 27 |
headings anywhere, and standard level 2 headings, Usage followed by |
| 28 |
Examples, within the configured sections. |
| 29 |
|
| 30 |
Run with no arguments to discover and check every Markdown file under |
| 31 |
content/en. Alternatively, pass one or more file paths to check specific |
| 32 |
files. |
| 33 |
|
| 34 |
Usage: $(basename "$0") [file ...] |
| 35 |
|
| 36 |
Example: $(basename "$0") content/en/functions/compare/Eq.md |
| 37 |
EOT |
| 38 |
) |
| 39 |
printf "%s\\n" "${msg}" |
| 40 |
exit 1 |
| 41 |
} |
| 42 |
|
| 43 |
#------------------------------------------------------------------------------ |
| 44 |
# @function |
| 45 |
# Reports whether the given path matches any of the given excluded paths. |
| 46 |
# |
| 47 |
# @param string $1 |
| 48 |
# The path to check. |
| 49 |
# @param string $2... |
| 50 |
# Zero or more excluded paths. |
| 51 |
# |
| 52 |
# @return bool |
| 53 |
# True if excluded. |
| 54 |
#------------------------------------------------------------------------------ |
| 55 |
is_excluded() { |
| 56 |
declare -r path="$1" |
| 57 |
shift |
| 58 |
declare excluded |
| 59 |
for excluded in "$@"; do |
| 60 |
[[ "${path}" == "${excluded}" ]] && return 0 |
| 61 |
done |
| 62 |
return 1 |
| 63 |
} |
| 64 |
|
| 65 |
#------------------------------------------------------------------------------ |
| 66 |
# @function |
| 67 |
# Reports whether the given path is within any of the given section |
| 68 |
# directories. |
| 69 |
# |
| 70 |
# @param string $1 |
| 71 |
# The path to check. |
| 72 |
# @param string $2... |
| 73 |
# Zero or more section directories. |
| 74 |
# |
| 75 |
# @return bool |
| 76 |
# True if within a section. |
| 77 |
#------------------------------------------------------------------------------ |
| 78 |
in_section() { |
| 79 |
declare -r path="$1" |
| 80 |
shift |
| 81 |
declare section |
| 82 |
for section in "$@"; do |
| 83 |
[[ "${path}" == "${section}"/* ]] && return 0 |
| 84 |
done |
| 85 |
return 1 |
| 86 |
} |
| 87 |
|
| 88 |
#------------------------------------------------------------------------------ |
| 89 |
# @function |
| 90 |
# Main function. |
| 91 |
# |
| 92 |
# With no arguments, discovers every Markdown file under content/en, then |
| 93 |
# re-invokes this script with that file list. With one or more arguments, |
| 94 |
# checks each given file for a disallowed heading level, and, for files |
| 95 |
# within the sections array that are not _index.md files or listed in the |
| 96 |
# exclusions array, for the expected level 2 headings. Reports a separate |
| 97 |
# error message for each check that fails. |
| 98 |
# |
| 99 |
# @param string $@ |
| 100 |
# Zero or more file paths to check. |
| 101 |
#------------------------------------------------------------------------------ |
| 102 |
main() { |
| 103 |
# If run with no arguments, discover files and re-run this script with the file list. |
| 104 |
if (( $# == 0 )); then |
| 105 |
find content/en -type f -name "*.md" -print0 | xargs -0 "$0" |
| 106 |
exit $? |
| 107 |
fi |
| 108 |
|
| 109 |
# Directories, relative to the project root, whose Markdown files must |
| 110 |
# contain exactly two level 2 headings: Usage and Examples. Add to this |
| 111 |
# list as needed. |
| 112 |
declare -ra sections=( |
| 113 |
"content/en/functions" |
| 114 |
"content/en/methods" |
| 115 |
) |
| 116 |
|
| 117 |
# Paths, relative to the project root, to exclude from the Usage and |
| 118 |
# Examples heading check. Add to this list as needed. |
| 119 |
declare -ra exclusions=( |
| 120 |
"content/en/functions/js/Batch.md" |
| 121 |
) |
| 122 |
|
| 123 |
declare -i errors=0 |
| 124 |
declare -r expected=$'Usage\nExamples' |
| 125 |
declare actual file |
| 126 |
|
| 127 |
for file in "$@"; do |
| 128 |
if grep -qE '^# |^##### |^###### ' "${file}"; then |
| 129 |
echo "Invalid heading structure in ${file}. Found a level 1, level 5, or level 6 heading." |
| 130 |
errors+=1 |
| 131 |
fi |
| 132 |
|
| 133 |
in_section "${file}" "${sections[@]}" || continue |
| 134 |
[[ "$(basename "${file}")" == "_index.md" ]] && continue |
| 135 |
is_excluded "${file}" "${exclusions[@]}" && continue |
| 136 |
|
| 137 |
actual=$(grep -E '^## ' "${file}" | sed 's/^## *//; s/ *$//' || true) |
| 138 |
if [[ "${actual}" != "${expected}" ]]; then |
| 139 |
echo "Invalid heading structure in ${file}. Expected exactly two level 2 headings, Usage and Examples." |
| 140 |
errors+=1 |
| 141 |
fi |
| 142 |
done |
| 143 |
|
| 144 |
(( errors == 0 )) |
| 145 |
} |
| 146 |
|
| 147 |
set -euo pipefail |
| 148 |
main "$@" |