generated from openmcp-project/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-docs-index.sh
executable file
·87 lines (73 loc) · 2.29 KB
/
generate-docs-index.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
set -euo pipefail
source "$(realpath "$(dirname $0)/environment.sh")"
DOCS_FOLDER="${DOCS_FOLDER:-${PROJECT_ROOT}/docs}"
METAFILE_NAME=".docnames"
doc_index_file=${1:-"$DOCS_FOLDER/README.md"}
# prints to the new doc index
function println() {
echo "$@" >> "$newindex"
}
# expects a path to a folder as argument
# returns how this folder should be named in an index
function getDocFolderName() {
local metafile="$1/$METAFILE_NAME"
if [[ -f "$metafile" ]]; then
cat "$metafile" | $JQ -r '.header'
fi
}
# expects two arguments:
# - path to a doc folder
# - name of the file in there
# the file is expected to contain its header in the first line
# or there should be an overwrite present in <foldername>/$METAFILE_NAME
function getDocName() {
local metafile="$1/$METAFILE_NAME"
local filename="$2"
if [[ -f "$metafile" ]]; then
local overwrite="$(cat "$metafile" | $JQ -r '.overwrites[$name]' --arg name "$2")"
if [[ "$overwrite" != "null" ]]; then
echo "$overwrite"
return
fi
fi
if [[ -f "$filename" ]]; then
local firstheader=$(grep -m1 -E '^# (.*)$' "$filename")
echo "${firstheader#'# '}"
fi
}
echo "> Generating Documentation Index"
newindex=$(mktemp)
println '<!-- Do not edit this file, as it is auto-generated!-->'
println "# Documentation Index"
println
(
cd "$DOCS_FOLDER"
for f in *; do
if [[ -d "$f" ]]; then
foldername="$(getDocFolderName "$f")"
if [[ -z "$foldername" ]]; then
echo "Ignoring folder '$f' due to missing '$METAFILE_NAME' file."
continue
fi
println "## $foldername"
println
(
cd "$f"
for f2 in *.md; do
docname="$(getDocName "../$f" "$f2")"
if [[ -z "$docname" ]]; then
echo "Ignoring file '$f/$f2' because the header could not be determined."
# There are two possible reasons for this:
# 1. The file doesn't start with a '# <headline>' in the first line and no overwrite is defined in the folder's metafile.
# 2. The overwrite in the folder's metafile explicitly sets the name to an empty string, meaning this file should be ignored.
continue
fi
println "- [$docname]($f/$f2)"
done
)
println
fi
done
)
cp "$newindex" "$doc_index_file"