From e06dfe98de1faa7346ac22eaf5e96ed8adb86580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Woimb=C3=A9e?= Date: Thu, 6 May 2021 11:09:15 +0200 Subject: [PATCH] cleanup JS template --- .../_template/docs-versions-menu.js_t | 192 +++++++++--------- 1 file changed, 97 insertions(+), 95 deletions(-) diff --git a/src/docs_versions_menu/_template/docs-versions-menu.js_t b/src/docs_versions_menu/_template/docs-versions-menu.js_t index 5706569..b46ae47 100644 --- a/src/docs_versions_menu/_template/docs-versions-menu.js_t +++ b/src/docs_versions_menu/_template/docs-versions-menu.js_t @@ -4,31 +4,87 @@ async function UrlExists(url) { return fetch(url, { method: "HEAD" }).then(r => r.status === 200); } +/** Tries every possible path until versions.json is found */ async function getRootUrl() { - let rootUrl = window.location.href; - let done = false; - while (!done) { - rootUrl = rootUrl.substr(0, rootUrl.lastIndexOf("/")); - if (rootUrl === "") { - throw new Error("Could not find versions.json !"); + let tmpRootUrl = window.location.href; + while (tmpRootUrl !== "") { + tmpRootUrl = tmpRootUrl.substr(0, tmpRootUrl.lastIndexOf("/")); + if (await UrlExists(tmpRootUrl + "/versions.json")) { + return tmpRootUrl; } - done = await UrlExists(rootUrl + "/versions.json"); } - return rootUrl + console.warning("versions.json not found, silently stopping execution"); + return undefined; } -async function getCurrentVersionFolder(rootUrl) { +function getCurrentVersionFolder(rootUrl) { return window.location.href.substr(rootUrl.length + 1).split("/")[0]; } -async function _addVersionsMenu(versionData, rootUrl) { +async function generateInnerHtml(versionsJson, rootUrl) { // The menu was reverse-engineered from the RTD websites, so it's very // specific to the sphinx_rtd_theme - let folders = versionData["versions"]; - let current_url = document.URL; - let current_folder = await getCurrentVersionFolder(rootUrl); - let current_version = versionData["labels"][current_folder]; - let menu = document.createElement('div'); + const currentUrl = document.URL; + const currentFolder = getCurrentVersionFolder(rootUrl); + const currentVersion = versionsJson["labels"][currentFolder]; + let innerHtml = ` + + {% if not badge_only %}{{ menu_title }}{% endif%} + ${currentVersion} + + +
+
+
+
Versions
+ `; + for (let folder of versionsJson["versions"]) { + if (folder == currentFolder) { + innerHtml += `
${currentVersion}
`; + } else { + const folder_url = currentUrl.replace(currentFolder, folder); + const folder_name = versionsJson["labels"][folder]; + innerHtml += `
${folder_name}
`; + } + } + let downloads = versionsJson["downloads"][currentFolder]; + if (downloads.length > 0) { + innerHtml += "
Downloads
"; + for (let d of downloads) { + let downloadLabel = d[0]; + let downloadUrl = d[1]; + if (!(/^(https?|ftp):/.test(downloadUrl))) { + if (!downloadUrl.startsWith('/')) { + downloadUrl = '/' + downloadUrl; + } + downloadUrl = root_url + downloadUrl; + } + innerHtml += `
${downloadLabel}
`; + } + } + innerHtml += `

`; + return innerHtml +} + + +async function addVersionsMenu() { + const rootUrl = await getRootUrl(); + if (rootUrl == undefined) { + return ; + } + const jsonFile = rootUrl + "/versions.json"; + + const response = await fetch(jsonFile, { + method: 'GET', + cache: 'no-cache', + }); + const versionsJson = await response.json(); + + // The menu was reverse-engineered from the RTD websites, so it's very + // specific to the sphinx_rtd_theme + const currentUrl = document.URL; + const currentFolder = getCurrentVersionFolder(rootUrl); + const menu = document.createElement('div'); {%- if badge_only %} menu.setAttribute('class', 'rst-versions rst-badge'); {%- else %} @@ -37,105 +93,51 @@ async function _addVersionsMenu(versionData, rootUrl) { menu.setAttribute('data-toggle', 'rst-versions'); menu.setAttribute('role', 'note'); menu.setAttribute('aria-label', 'versions'); - let inner_html = - "" + - " {% if not badge_only %}{{ menu_title }}{% endif%} " + - "" + current_version + " " + - "" + - "" + - "
" + - "
" + - "
" + - "
Versions
"; - let i; - for (i in folders) { - let folder = folders[i]; - if (folder == current_folder) { - inner_html = inner_html + "
" + current_version + "
"; - } else { - inner_html = inner_html + "
" + versionData["labels"][folder] + "
"; - } - } - let downloads = versionData["downloads"][current_folder]; - if (downloads.length > 0){ - inner_html = inner_html + - "
Downloads
"; - for (i in downloads) { - let download_label = downloads[i][0]; - let download_url = downloads[i][1]; - if (!(/^(https?|ftp):/.test(download_url))){ - if (!download_url.startsWith('/')){ - download_url = '/' + download_url; - } - download_url = root_url + download_url; - } - inner_html = inner_html + "
" - + download_label + "
"; - } - } - - inner_html = inner_html + - "
" + - "
" + - "Generated by Docs Versions Menu" + - "" + - "
" + - "
"; - menu.innerHTML = inner_html; - let parent = document.body; + const innerHtml = await generateInnerHtml(versionsJson, rootUrl); + menu.innerHTML = innerHtml; + const parent = document.body; parent.insertBefore(menu, parent.lastChild); // Add a warning banner for dev/outdated versions - let warning; - let msg; - if (versionData["warnings"][current_folder].indexOf("outdated") >=0){ + const currentVersionWarnings = versionsJson["warnings"][currentFolder]; + let warning = undefined; + let msg = undefined; + if (currentVersionWarnings.includes("outdated")) { warning = document.createElement('div'); warning.setAttribute('class', 'admonition danger'); msg = "This document is for an outdated version."; - } else if (versionData["warnings"][current_folder].indexOf("unreleased") >=0){ + } else if (currentVersionWarnings.includes("unreleased")) { warning = document.createElement('div'); warning.setAttribute('class', 'admonition danger'); msg = "This document is for an unreleased development version."; - } else if (versionData["warnings"][current_folder].indexOf("prereleased") >=0){ + } else if (currentVersionWarnings.includes("prereleased")) { warning = document.createElement('div'); warning.setAttribute('class', 'admonition danger'); msg = "This document is for a pre-release development version."; } - if (warning !== undefined){ - if (versionData["latest"] !== null){ - msg = msg + " Documentation is available for the " + "latest public release." + if (warning !== undefined) { + if (versionsJson["latest"] !== null) { + const latestVersionUrl = currentUrl.replace(currentFolder, versionsJson["latest"]); + msg += ` + Documentation is available for the + latest public release. + `; } - warning.innerHTML = "

Note

" + - "

" + msg + "

"; - let parent = document.querySelector('div.body') - || document.querySelector('div.document') - || document.body; + warning.innerHTML = ` +

Note

+

${msg}

+ `; + const parent = document.querySelector('div.body') + || document.querySelector('div.document') + || document.body; parent.insertBefore(warning, parent.firstChild); } -} - -async function addVersionsMenu() { - const rootUrl = await getRootUrl(); - let jsonFile = rootUrl + "/versions.json"; - - const response = await fetch(jsonFile, { - method: 'GET', - cache: 'no-cache', - }); - - await _addVersionsMenu(await response.json(), rootUrl); {%- if badge_only %} - $( "body" ).on('click', "div.rst-versions.rst-badge", function() { - $('.rst-other-versions').toggle(); - $('.rst-versions .rst-current-version .fa-book').toggleClass('shift-up'); + $("body").on('click', "div.rst-versions.rst-badge", function () { + $('.rst-other-versions').toggle(); + $('.rst-versions .rst-current-version .fa-book').toggleClass('shift-up'); }); {%- endif %}