Skip to content

Commit 53741d0

Browse files
larsonersnwnde
authored andcommitted
MAINT: Auto-update installer link for old doc versions (mne-tools#12056)
1 parent d267b4d commit 53741d0

File tree

3 files changed

+97
-29
lines changed

3 files changed

+97
-29
lines changed

doc/_static/js/set_installer_tab.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* inspired by https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/ */
2+
3+
function documentReady(callback) {
4+
if (document.readyState != "loading") callback();
5+
else document.addEventListener("DOMContentLoaded", callback);
6+
}
7+
8+
function setTabs() {
9+
var platform = "linux";
10+
if (navigator.userAgent.indexOf("Win") !== -1) {
11+
platform = "windows";
12+
}
13+
if (navigator.userAgent.indexOf("Mac") !== -1) {
14+
// there's no good way to distinguish intel vs M1 in javascript so we
15+
// just default to showing the first of the 2 macOS tabs
16+
platform = "macos-intel";
17+
}
18+
let all_tab_nodes = document.querySelectorAll(
19+
'.platform-selector-tabset')[0].children;
20+
let input_nodes = [...all_tab_nodes].filter(
21+
child => child.nodeName === "INPUT");
22+
let tab_label_nodes = [...document.querySelectorAll('.sd-tab-label')];
23+
let correct_label = tab_label_nodes.filter(
24+
// label.id is drawn from :name: property in the rST, which must
25+
// be unique across the whole site (*sigh*)
26+
label => label.id.startsWith(platform))[0];
27+
let input_id = correct_label.getAttribute('for');
28+
let correct_input = input_nodes.filter(node => node.id === input_id)[0];
29+
correct_input.checked = true;
30+
}
31+
32+
documentReady(setTabs);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* inspired by https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/ */
2+
3+
function documentReady(callback) {
4+
if (document.readyState != "loading") callback();
5+
else document.addEventListener("DOMContentLoaded", callback);
6+
}
7+
8+
async function getRelease() {
9+
result = await fetch("https://api.github.com/repos/mne-tools/mne-installers/releases/latest");
10+
data = await result.json();
11+
return data;
12+
}
13+
async function warnVersion() {
14+
data = await getRelease();
15+
// Take v1.5.1 for example and change to 1.5
16+
ids = ["linux-installers", "macos-intel-installers", "macos-apple-installers", "windows-installers"];
17+
warn = false;
18+
ids.forEach((id) => {
19+
label_id = document.getElementById(id);
20+
// tab is immediately after label
21+
children = [].slice.call(label_id.parentNode.children);
22+
div = children[children.indexOf(label_id) + 1];
23+
a = div.children[0].children[0]; // div->p->a
24+
ending = a.href.split("-").slice(-1)[0]; // Should be one of: ["macOS_Intel.pkg", "macOS_M1.pkg", "Linux.sh", "Windows.exe"]
25+
data["assets"].every((asset) => {
26+
// find the matching asset
27+
if (!asset["browser_download_url"].endsWith(ending)) {
28+
return true; // continue
29+
}
30+
old_stem = a.href.split("/").slice(-1)[0];
31+
new_stem = asset["browser_download_url"].split("/").slice(-1)[0];
32+
a.href = asset["browser_download_url"];
33+
// also replace the command on Linux
34+
if (ending === "Linux.sh") {
35+
code = document.getElementById("codecell0");
36+
}
37+
if (!warn) {
38+
// MNE-Python-1.5.1_0-Linux.sh to 1.5 for example
39+
old_ver = old_stem.split("-").slice(2)[0].split("_")[0].split(".").slice(0, 2).join(".");
40+
new_ver = new_stem.split("-").slice(2)[0].split("_")[0].split(".").slice(0, 2).join(".");
41+
if (old_ver !== new_ver) {
42+
warn = `The installers below are for version ${new_ver} as ${old_ver} is no longer supported`;
43+
}
44+
}
45+
return false; // do not continue
46+
});
47+
});
48+
if (warn) {
49+
let outer = document.createElement("div");
50+
let title = document.createElement("p");
51+
let inner = document.createElement("p");
52+
outer.setAttribute("class", "admonition warning");
53+
title.setAttribute("class", "admonition-title");
54+
title.innerText = "Warning";
55+
inner.innerText = warn;
56+
outer.append(title, inner);
57+
document.querySelectorAll('.platform-selector-tabset')[0].before(outer);
58+
}
59+
}
60+
61+
documentReady(warnVersion);

doc/install/installers.rst

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ Got any questions? Let us know on the `MNE Forum`_!
7878

7979
**Supported platforms:** Windows 10 and newer
8080

81+
.. raw:: html
82+
83+
<script async="async" src="../_static/js/update_installer_version.js"></script>
84+
<script async="async" src="../_static/js/set_installer_tab.js"></script>
8185

8286
First steps
8387
^^^^^^^^^^^
@@ -123,32 +127,3 @@ interpreter.
123127

124128
.. note::
125129
This information is currently not displayed on the Windows platform.
126-
127-
128-
.. raw:: html
129-
130-
<script type="text/javascript">
131-
var platform = "linux";
132-
if (navigator.userAgent.indexOf("Win") !== -1) {
133-
platform = "windows";
134-
}
135-
if (navigator.userAgent.indexOf("Mac") !== -1) {
136-
// there's no good way to distinguish intel vs M1 in javascript so we
137-
// just default to showing the first of the 2 macOS tabs
138-
platform = "macos-intel";
139-
}
140-
$(document).ready(function(){
141-
let all_tab_nodes = document.querySelectorAll(
142-
'.platform-selector-tabset')[0].children;
143-
let input_nodes = [...all_tab_nodes].filter(
144-
child => child.nodeName === "INPUT");
145-
let tab_label_nodes = [...document.querySelectorAll('.sd-tab-label')];
146-
let correct_label = tab_label_nodes.filter(
147-
// label.id is drawn from :name: property in the rST, which must
148-
// be unique across the whole site (*sigh*)
149-
label => label.id.startsWith(platform))[0];
150-
let input_id = correct_label.getAttribute('for');
151-
let correct_input = input_nodes.filter(node => node.id === input_id)[0];
152-
correct_input.checked = true;
153-
});
154-
</script>

0 commit comments

Comments
 (0)