@@ -18,6 +18,20 @@ SUBREPOS=(
1818DISTROS=(jazzy kilted rolling)
1919REDIRECT_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+
2135clean_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>
4976EOF
77+ done < <( find " $src " -name ' *.html' -print0)
5078}
5179
5280if [[ $# -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 "
5785else
5886 for d in " $@ " ; do
5987 build_one " $d "
0 commit comments