stura-infra/docs/build-docs.sh

118 lines
2.8 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Script to collect README files and prepare them for Hugo
# This script is called during the Nix build process
REPO_ROOT="${1:-.}"
CONTENT_DIR="${2:-content}"
echo "Building documentation from $REPO_ROOT to $CONTENT_DIR"
mkdir -p "$CONTENT_DIR"
mkdir -p "$CONTENT_DIR/hosts"
# Function to convert relative links to work in Hugo
# Converts markdown links like [text](../other/README.md) to [text](/other/)
fix_links() {
local file="$1"
local base_path="$2"
sed -E \
-e 's|\[([^\]]+)\]\((\.\./)+hosts/([^/]+)/README\.md\)|\[\1\](/hosts/\3/)|g' \
-e 's|\[([^\]]+)\]\(\.\./([^/]+)/README\.md\)|\[\1\](/hosts/\2/)|g' \
-e 's|\[([^\]]+)\]\(\.\./(README\.md)\)|\[\1\](/)|g' \
-e 's|\[([^\]]+)\]\(\.\./\.\./README\.md\)|\[\1\](/)|g' \
-e 's|\[([^\]]+)\]\(\./([^/]+)/README\.md\)|\[\1\](/\2/)|g' \
-e 's|\[hosts/([^/]+)/README\.md\]\(hosts/\1/README\.md\)|\[hosts/\1/\]\(/hosts/\1/\)|g' \
-e 's|\[([^\]]+)\]\(hosts/([^/]+)/README\.md\)|\[\1\](/hosts/\2/)|g' \
-e 's|\[([^\]]+)\]\(README\.md\)|\[\1\](/)|g' \
"$file"
}
# Process main README.md
if [ -f "$REPO_ROOT/README.md" ]; then
echo "Processing main README.md..."
{
cat <<'EOF'
---
title: "StuRa HTW Infrastructure"
date: 2024-01-01
weight: 1
---
EOF
fix_links "$REPO_ROOT/README.md" "/"
} > "$CONTENT_DIR/_index.md"
fi
# Process CLAUDE.md as a separate page
if [ -f "$REPO_ROOT/CLAUDE.md" ]; then
echo "Processing CLAUDE.md..."
{
cat <<'EOF'
---
title: "Claude Code Guide"
date: 2024-01-01
weight: 10
---
EOF
fix_links "$REPO_ROOT/CLAUDE.md" "/"
} > "$CONTENT_DIR/claude-guide.md"
fi
# Create hosts index page
cat > "$CONTENT_DIR/hosts/_index.md" <<'EOF'
---
title: "Hosts"
date: 2024-01-01
weight: 2
---
# Host Configurations
This section contains documentation for each host in the infrastructure.
EOF
# Process host README files
if [ -d "$REPO_ROOT/hosts" ]; then
for host_dir in "$REPO_ROOT/hosts"/*; do
if [ -d "$host_dir" ]; then
host_name=$(basename "$host_dir")
readme="$host_dir/README.md"
if [ -f "$readme" ]; then
echo "Processing host: $host_name"
{
cat <<EOF
---
title: "$host_name"
date: 2024-01-01
---
EOF
fix_links "$readme" "/hosts/$host_name"
} > "$CONTENT_DIR/hosts/$host_name.md"
fi
fi
done
fi
# Process keys README if it exists
if [ -f "$REPO_ROOT/keys/README.md" ]; then
echo "Processing keys/README.md..."
{
cat <<'EOF'
---
title: "Key Management"
date: 2024-01-01
weight: 5
---
EOF
fix_links "$REPO_ROOT/keys/README.md" "/keys"
} > "$CONTENT_DIR/keys.md"
fi
echo "Documentation build complete!"