Skip to content

Commit 1ef99a5

Browse files
miyukocwhitequark
authored andcommitted
docs: include the latest version in the switcher.
1 parent e11cb25 commit 1ef99a5

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

.github/workflows/main.yaml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,41 @@ jobs:
199199
git clone --depth=1 [email protected]:amaranth-lang/amaranth-lang.github.io.git ~/amaranth-lang.github.io
200200
cd ~/amaranth-lang.github.io/docs/amaranth/
201201
202-
(
203-
venv_path=$(mktemp -d)
202+
venv_path=~/.generate-versions-json-venv
204203
python3 -m venv "$venv_path"
205204
source "$venv_path"/bin/activate
206205
pip install packaging
207-
python >versions.json <<EOF
206+
207+
python <<EOF
208+
import re
208209
import json
209210
from pathlib import Path
210211
from packaging.version import Version
211212
212-
versions = Path('.').glob('v*')
213-
versions = (path.name[1:] for path in versions if path.is_dir())
214-
versions = sorted(versions, key=Version, reverse=True)
215-
versions = ({"name": version, "root_url": f"/docs/amaranth/v{version}/"} for version in versions)
216-
print(json.dumps(list(versions), indent=2))
213+
versions = []
214+
for version_dir in [Path("latest"), *Path(".").glob("v*")]:
215+
if not version_dir.is_dir():
216+
continue
217+
with open(version_dir / "_static" / "documentation_options.js", "r") as f:
218+
doc_options = f.read()
219+
match = re.search(r"""VERSION:\s+(['"])(.+?)\1""", doc_options)
220+
if match is None:
221+
continue
222+
versions.append({
223+
"name": match.group(2),
224+
"root_url": f"/docs/amaranth/{version_dir.name}/",
225+
})
226+
227+
versions.sort(key=lambda x: Version(x["name"]), reverse=True)
228+
229+
if len(versions) > 0:
230+
with open("versions.json", "w") as f:
231+
json.dump(versions, f, indent=2)
232+
else:
233+
Path("versions.json").unlink()
217234
EOF
218-
)
219235
220-
git add versions.json
236+
[[ -f versions.json ]] && git add versions.json || git rm versions.json || true
221237
if ! git diff-index --quiet --cached HEAD; then
222238
git \
223239
-c user.name="$GITHUB_ACTOR" \

docs/_static/version-switch.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ document.addEventListener('DOMContentLoaded', () => {
55
);
66

77
function insertVersionSwitch(versions) {
8+
let currentVersion = DOCUMENTATION_OPTIONS.VERSION;
9+
if (!versions.some(({ name }) => name === currentVersion)) {
10+
return;
11+
}
12+
813
let versionElement = document.querySelector('.wy-side-nav-search > .version');
914
if (!versionElement) return;
1015

@@ -72,14 +77,6 @@ document.addEventListener('DOMContentLoaded', () => {
7277
`);
7378
document.adoptedStyleSheets.push(cssStyleSheet);
7479

75-
let currentVersion = DOCUMENTATION_OPTIONS.VERSION;
76-
if (!versions.some(({ name }) => name === currentVersion)) {
77-
versions = [
78-
{ name: currentVersion, root_url: contentRoot },
79-
...versions,
80-
];
81-
}
82-
8380
for (let { name, root_url: rootURL } of versions) {
8481
rootURL = new URL(rootURL, window.location.href);
8582

0 commit comments

Comments
 (0)