Skip to content

Commit 53d88ad

Browse files
miyukocwhitequark
authored andcommitted
docs: add a version switch.
1 parent 638edff commit 53d88ad

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

.github/workflows/main.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,38 @@ jobs:
193193
branch: main
194194
folder: docs/
195195
target-folder: docs/amaranth/${{ github.ref_name }}/
196+
- name: Generate list of versions
197+
if: ${{ github.event_name == 'push' && (github.event.ref == 'refs/heads/main' || startsWith(github.event.ref, 'refs/tags/v') && !contains(github.event.ref, 'dev')) }}
198+
run: |
199+
git clone --depth=1 [email protected]:amaranth-lang/amaranth-lang.github.io.git ~/amaranth-lang.github.io
200+
cd ~/amaranth-lang.github.io/docs/amaranth/
201+
202+
(
203+
venv_path=$(mktemp -d)
204+
python3 -m venv "$venv_path"
205+
source "$venv_path"/bin/activate
206+
pip install packaging
207+
python >versions.json <<EOF
208+
import json
209+
from pathlib import Path
210+
from packaging.version import Version
211+
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))
217+
EOF
218+
)
219+
220+
git add versions.json
221+
if ! git diff-index --quiet --cached HEAD; then
222+
git \
223+
-c user.name="$GITHUB_ACTOR" \
224+
-c user.email="[email protected]" \
225+
commit -m "Update docs/amaranth/versions.json"
226+
git push
227+
fi
196228
197229
publish-docs-dev:
198230
needs: document

docs/_static/version-switch.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
let contentRoot = new URL(
3+
document.documentElement.dataset.content_root ?? DOCUMENTATION_OPTIONS.URL_ROOT,
4+
window.location.href,
5+
);
6+
7+
function insertVersionSwitch(versions) {
8+
let versionElement = document.querySelector('.wy-side-nav-search > .version');
9+
if (!versionElement) return;
10+
11+
let root = document.createElement('div');
12+
root.innerHTML = `
13+
<div class="switch-menus">
14+
<div class="version-switch"><select></select></div>
15+
</div>
16+
`;
17+
18+
let switchMenus = root.firstElementChild;
19+
let versionSwitch = switchMenus.firstElementChild;
20+
let versionSwitchSelect = versionSwitch.firstElementChild;
21+
22+
let versionElementStyles = getComputedStyle(versionElement);
23+
let cssStyleSheet = new CSSStyleSheet();
24+
cssStyleSheet.replaceSync(String.raw`
25+
.wy-side-nav-search > div.switch-menus {
26+
margin-top: ${versionElementStyles.marginTop};
27+
margin-bottom: ${versionElementStyles.marginBottom};
28+
color: ${versionElementStyles.color};
29+
30+
div.version-switch {
31+
select {
32+
display: inline-block;
33+
margin-right: -2rem;
34+
padding-right: 2rem;
35+
text-align-last: center;
36+
background: none;
37+
border: none;
38+
border-radius: 0em;
39+
box-shadow: none;
40+
font-family: inherit;
41+
font-size: 1em;
42+
font-weight: normal;
43+
color: inherit;
44+
cursor: pointer;
45+
appearance: none;
46+
47+
&:hover, &:active, &:focus {
48+
background: rgba(255, 255, 255, .1);
49+
color: rgba(255, 255, 255, .5);
50+
}
51+
52+
option {
53+
color: black;
54+
}
55+
}
56+
57+
&:has(> select):after {
58+
display: inline-block;
59+
width: 1.5em;
60+
height: 100%;
61+
padding: .1em;
62+
content: "\f0d7";
63+
font-size: 1em;
64+
line-height: 1.2em;
65+
font-family: FontAwesome;
66+
text-align: center;
67+
pointer-events: none;
68+
box-sizing: border-box;
69+
}
70+
}
71+
}
72+
`);
73+
document.adoptedStyleSheets.push(cssStyleSheet);
74+
75+
let currentVersion = DOCUMENTATION_OPTIONS.VERSION;
76+
if (!versions.includes(currentVersion)) {
77+
versions = [
78+
{ name: currentVersion, root_url: contentRoot },
79+
...versions,
80+
];
81+
}
82+
83+
for (let { name, root_url: rootURL } of versions) {
84+
rootURL = new URL(rootURL, window.location.href);
85+
86+
let versionOptionElement = document.createElement('option');
87+
versionOptionElement.textContent = name;
88+
versionOptionElement.dataset.url = rootURL;
89+
90+
if (name === currentVersion) {
91+
versionOptionElement.selected = true;
92+
}
93+
94+
versionSwitchSelect.appendChild(versionOptionElement);
95+
}
96+
97+
versionSwitchSelect.addEventListener('change', (event) => {
98+
let option = event.target.selectedIndex;
99+
let item = event.target.options[option];
100+
window.location.href = item.dataset.url;
101+
});
102+
103+
versionElement.replaceWith(switchMenus);
104+
}
105+
106+
fetch(new URL('../versions.json', contentRoot)).then(async (response) => {
107+
if (response.status !== 200) return;
108+
let versions = await response.json();
109+
insertVersionSwitch(versions);
110+
});
111+
});

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
html_theme = "sphinx_rtd_theme"
5858
html_static_path = ["_static"]
5959
html_css_files = ["custom.css"]
60+
html_js_files = ["version-switch.js"]
6061
html_logo = "_static/logo.png"
6162

6263
rst_prolog = """

0 commit comments

Comments
 (0)