Skip to content

Commit

Permalink
docs: add 'current' alias for latest released version (#979)
Browse files Browse the repository at this point in the history
Fixes #959.
Fixes #978.

---------

Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
  • Loading branch information
lidavidm and kou authored Aug 16, 2023
1 parent 500cb6b commit 2530e46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
24 changes: 17 additions & 7 deletions ci/scripts/website_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ main() {
# Copy the version script and regenerate the version list
# The versions get embedded into the JavaScript file to save a roundtrip
rm -f "${site}/version.js"
cp "${repo}/docs/source/_static/version.js" "${site}/version.js"
echo >> "${site}/version.js"
echo 'const versions = `' >> "${site}/version.js"

pushd "${site}"
rm -f "${site}/versions.txt"
for inv in */objects.inv; do
if [[ "$(dirname $inv)" = "current" ]]; then
continue
fi
echo "$(dirname $inv);$(sphobjinv convert json $inv - | jq -r .version)"
done | sort -t ";" --version-sort | tee --append "${site}/version.js" "${site}/versions.txt"
popd

echo '`;' >> "${site}/version.js"
git -C "${site}" add -f "version.js"

# Determine the latest stable version
local -r latest_docs=$(grep -E ';[0-9]+\.[0-9]+\.[0-9]+$' "${site}/versions.txt" | sort -t ';' --version-sort | tail -n1)
if [[ -z "${latest_docs}" ]]; then
Expand All @@ -85,18 +84,29 @@ main() {
fi
echo "Latest version: ${latest_version} in directory ${latest_dir}"

# Make a copy of the latest release under a stable URL
rm -rf "${site}/current/"
cp -r "${site}/${latest_dir}" "${site}/current/"
git -C "${site}" add -f "current"

echo "current;${latest_version} (current)" >> "${site}/version.js"

echo '`;' >> "${site}/version.js"
cat "${repo}/docs/source/_static/version.js" >> "${site}/version.js"
git -C "${site}" add -f "version.js"

# Generate index.html
cat > "${site}/index.html" << EOF
<!DOCTYPE html>
<meta http-equiv="Refresh" content="0; url=$latest_dir/index.html">
<meta http-equiv="Refresh" content="0; url=current/index.html">
EOF
git -C "${site}" add -f "index.html"

# Generate latest/index.html
mkdir -p "${site}/latest"
cat > "${site}/latest/index.html" << EOF
<!DOCTYPE html>
<meta http-equiv="Refresh" content="0; url=../$latest_dir/index.html">
<meta http-equiv="Refresh" content="0; url=../current/index.html">
EOF
git -C "${site}" add -f "latest/index.html"
}
Expand Down
19 changes: 16 additions & 3 deletions docs/source/_static/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
// update the script globally. It depends on certain variables being
// injected into the Sphinx template.

window.addEventListener("DOMContentLoaded", () => {
function adbcInjectVersionSwitcher() {
// The template should contain this list, we just populate it
const root = document.querySelector("#version-switcher ul");

// Variable injected by ci/scripts/website_build.sh
// Format:
// path;version\npath2;version2;\n...
// Versions are sorted at generation time

versions
.trim()
.split(/\n/g)
.map((version) => version.split(/;/))
// Most recent on top
.reverse()
.forEach((version) => {
const el = document.createElement("a");
// Variable injected by template
Expand All @@ -48,7 +51,11 @@ window.addEventListener("DOMContentLoaded", () => {
el.addEventListener("click", (e) => {
e.preventDefault();
try {
const relativePart = window.location.pathname.replace(/^\/[^\/]+\//, "");
let relativePart = window.location.pathname.replace(/^\//, "");
// Remove the adbc/ prefix
relativePart = relativePart.replace(/^adbc[^\/]+\//, "");
// Remove the version number
relativePart = relativePart.replace(/^[^\/]+\//, "");
const newUrl = `${el.getAttribute("href")}/${relativePart}`;
window.fetch(newUrl).then((resp) => {
if (resp.status === 200) {
Expand All @@ -65,4 +72,10 @@ window.addEventListener("DOMContentLoaded", () => {
return false;
});
});
});
};

if (document.readyState !== "loading") {
adbcInjectVersionSwitcher();
} else {
window.addEventListener("DOMContentLoaded", adbcInjectVersionSwitcher);
}

0 comments on commit 2530e46

Please sign in to comment.