Skip to content

Commit 00a8453

Browse files
authored
Add relays to unversioned targets (#19)
This is basically a compatibility layer so old links to the unversioned pages will continue to work.
1 parent 1aef31b commit 00a8453

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ html: clone-subrepos
2727
export ROS_DISTRO="$$ros"; \
2828
$(SPHINXBUILD) -b html "$(SOURCEDIR)" "$(BUILDDIR)/html/$$ros" $(SPHINXOPTS) $(O)
2929

30-
# Humble, Jazzy, Kilted, Rolling; optional root index redirect (see script)
30+
# Jazzy, Kilted, Rolling; version-less pages redirect to chosen distro (see script)
3131
html-all:
3232
@./scripts/build_docs.sh
3333

scripts/build_docs.sh

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ SUBREPOS=(
1818
DISTROS=(jazzy kilted rolling)
1919
REDIRECT_TARGET="${DOCUMENTATION_ROOT_REDIRECT:-rolling}"
2020

21+
# Relative URL from _build/html/<relpath> to _build/html/<target>/<relpath>
22+
redirect_url_to_versioned() {
23+
local versionless_relpath="$1"
24+
local target="$2"
25+
local dir
26+
dir="$(dirname "$versionless_relpath")"
27+
local up=""
28+
while [[ "$dir" != "." ]]; do
29+
up="../${up}"
30+
dir="$(dirname "$dir")"
31+
done
32+
printf '%s' "${up}${target}/${versionless_relpath}"
33+
}
34+
2135
clean_subrepos() {
2236
rm -rf "${SUBREPOS[@]}"
2337
}
@@ -31,29 +45,43 @@ build_one() {
3145
sphinx-build -b html . "${ROOT}/_build/html/${distro}"
3246
}
3347

34-
write_root_redirect() {
48+
# Place a version-less HTML stub for each page in <target>/ so that
49+
# /path/to/page.html redirects to /<target>/path/to/page.html (GitHub Pages).
50+
write_versionless_redirects() {
3551
local target="$1"
36-
cat > "${ROOT}/_build/html/index.html" <<EOF
52+
local src="${ROOT}/_build/html/${target}"
53+
if [[ ! -d "$src" ]]; then
54+
echo "write_versionless_redirects: missing ${src}" >&2
55+
return 1
56+
fi
57+
while IFS= read -r -d '' f; do
58+
local relpath="${f#"${src}/"}"
59+
local dest="${ROOT}/_build/html/${relpath}"
60+
mkdir -p "$(dirname "$dest")"
61+
local url
62+
url="$(redirect_url_to_versioned "$relpath" "$target")"
63+
cat > "$dest" <<EOF
3764
<!DOCTYPE html>
3865
<html lang="en">
3966
<head>
4067
<meta charset="utf-8">
41-
<meta http-equiv="refresh" content="0; url=${target}/index.html">
42-
<link rel="canonical" href="${target}/index.html">
68+
<meta http-equiv="refresh" content="0; url=${url}">
69+
<link rel="canonical" href="${url}">
4370
<title>Universal Robots ROS 2 Driver Documentation</title>
4471
</head>
4572
<body>
46-
<p><a href="${target}/index.html">Continue to ${target} documentation</a>.</p>
73+
<p><a href="${url}">Continue to ${target} documentation</a>.</p>
4774
</body>
4875
</html>
4976
EOF
77+
done < <(find "$src" -name '*.html' -print0)
5078
}
5179

5280
if [[ $# -eq 0 ]]; then
5381
for d in "${DISTROS[@]}"; do
5482
build_one "$d"
5583
done
56-
write_root_redirect "$REDIRECT_TARGET"
84+
write_versionless_redirects "$REDIRECT_TARGET"
5785
else
5886
for d in "$@"; do
5987
build_one "$d"

0 commit comments

Comments
 (0)