forked from gemini-oss/rego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_hugo_index.sh
executable file
·97 lines (78 loc) · 2.86 KB
/
gen_hugo_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
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
##########
: <<'END'
This script initializes/updates the content directory for Hugo documentation.
:Copyright: (c) 2024 by Gemini Space Station, LLC., see AUTHORS for more info
:License: See the LICENSE file for details
:Author: Anthony Dardano <[email protected]>
END
##########
DOCS_DIR="hugo/content/docs"
SRC_DIR="pkg"
# Function to create directories and generate _index.md files
create_dir_and_index() {
local dir_path="$1"
local dir_name=$(basename "${dir_path}")
# Create directory if it doesn't exist
mkdir -p "${dir_path}"
# Path for the _index.md file
local index_file="${dir_path}/_index.md"
# Create _index.md if it doesn't exist
if [[ ! -f "${index_file}" ]]; then
# Use heredoc to write content to _index.md
cat << EOF > "${index_file}"
---
title: ${dir_name^}
weight: 1
---
Explore the following sections to learn more:
{{< cards >}}
EOF
fi
}
# Function to add cards to _index.md files
add_cards_to_index() {
local dir_path="$1"
local index_file="${dir_path}/_index.md"
# Check if _index.md exists
if [[ -f "${index_file}" ]]; then
# Temp file for storing new content
local temp_file=$(mktemp)
# Copy content before the cards section
awk '/{{< \/cards >}}/{exit} {print}' "${index_file}" > "${temp_file}"
# Add cards for each subdirectory or markdown file
for item in "${dir_path}"/*; do
if [[ -d "${item}" ]] && [[ "${item}" != "${dir_path}" ]]; then
local name=$(basename "${item}" | sed -e "s/.md\$//")
local link="${name}"
echo " {{< card link=\"${link}\" title=\"${name^}\" icon=\"document-duplicate\" >}}" >> "${temp_file}"
fi
done
# Add the closing cards tag and embed tags
cat << EOF >> "${temp_file}"
{{< /cards >}}
<!-- gomarkdoc:embed:start -->
<!-- gomarkdoc:embed:end -->
EOF
# Replace the original file with the new content
mv "${temp_file}" "${index_file}"
fi
}
# Traverse the source directory structure and mirror it in the docs directory
find "${SRC_DIR}" -type d | while read -r src_dir; do
docs_dir_path="${DOCS_DIR}/${src_dir}"
create_dir_and_index "${docs_dir_path}"
done
# Add cards to each _index.md file
find "${DOCS_DIR}" -type d | while read -r docs_dir; do
add_cards_to_index "${docs_dir}"
done
# Generate documentation last
if which gomarkdoc >/dev/null 2>&1; then
$(go env GOPATH)/bin/gomarkdoc ./... --output 'hugo/content/docs/{{.Dir}}/_index.md' --exclude-dirs ./pkg/internal/tests/... --embed
else
go install github.com/princjef/gomarkdoc/cmd/gomarkdoc@latest
$(go env GOPATH)/bin/gomarkdoc ./... --output 'hugo/content/docs/{{.Dir}}/_index.md' --exclude-dirs ./pkg/internal/tests/... --embed
fi
# Create the _index.md file for the root directory
cp README.md hugo/content/_index.md