From b9f221d9ab10367fb5a6b8964ddf8a3650afe060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Woimb=C3=A9e?= Date: Wed, 5 May 2021 20:25:09 +0200 Subject: [PATCH] make it work outside github pages --- .../_template/docs-versions-menu.js_t | 131 ++++++++---------- src/docs_versions_menu/ext.py | 5 - 2 files changed, 57 insertions(+), 79 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 887a720..5706569 100644 --- a/src/docs_versions_menu/_template/docs-versions-menu.js_t +++ b/src/docs_versions_menu/_template/docs-versions-menu.js_t @@ -1,47 +1,34 @@ "use strict"; -function getGhPagesCurrentFolder() { - // Extract version folder under the assumpgion that the URL is of the form - // https://.github.io///... - if (window.location.hostname.includes("github.io")){ - return window.location.pathname.split('/')[2]; - } +async function UrlExists(url) { + return fetch(url, { method: "HEAD" }).then(r => r.status === 200); } -function getRootUrl() { - // Return the "root" URL, i.e. everything before the current folder - // (getGhPagesCurrentFolder). On gh-pages, this includes the project name. - var root_url = window.location.origin; - if (window.location.hostname.includes("github.io")){ - root_url = root_url + '/' + window.location.pathname.split('/')[1]; +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 !"); + } + done = await UrlExists(rootUrl + "/versions.json"); } - return root_url; + return rootUrl } -function getGithubProjectUrl(){ - // Return the project url on Github, under the assumption that the current - // page is hosted on github-pages (https://.github.io//) - var root_url = getRootUrl(); - var match = root_url.match(/([\w\d-]+)\.github\.io\/([\w\d-]+)/) - if (match !== null){ - var username = match[1]; - var projectname = match[2]; - return "https://github.com/" + username + "/" + projectname; - } else { - return null - } +async function getCurrentVersionFolder(rootUrl) { + return window.location.href.substr(rootUrl.length + 1).split("/")[0]; } -function _addVersionsMenu(version_data) { +async function _addVersionsMenu(versionData, rootUrl) { // The menu was reverse-engineered from the RTD websites, so it's very // specific to the sphinx_rtd_theme - var folders = version_data["versions"]; - var root_url = getRootUrl(); - var current_url = document.URL; - var current_folder = {{ "%r" % current_folder }}; - if (current_folder === undefined) return; - var current_version = version_data["labels"][current_folder]; - var menu = document.createElement('div'); + 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'); {%- if badge_only %} menu.setAttribute('class', 'rst-versions rst-badge'); {%- else %} @@ -50,7 +37,7 @@ function _addVersionsMenu(version_data) { menu.setAttribute('data-toggle', 'rst-versions'); menu.setAttribute('role', 'note'); menu.setAttribute('aria-label', 'versions'); - var inner_html = + let inner_html = "" + " {% if not badge_only %}{{ menu_title }}{% endif%} " + "" + current_version + " " + @@ -60,44 +47,38 @@ function _addVersionsMenu(version_data) { "
" + "
" + "
Versions
"; - var i; + let i; for (i in folders) { - var folder = folders[i]; - if (folder == current_folder){ - var inner_html = inner_html + "
" + current_version + "
"; } else { - var inner_html = inner_html + "
" + version_data["labels"][folder] + "
"; + + "'>" + versionData["labels"][folder] + ""; } } - var downloads = version_data["downloads"][current_folder]; + let downloads = versionData["downloads"][current_folder]; if (downloads.length > 0){ - var inner_html = inner_html + + inner_html = inner_html + "
Downloads
"; for (i in downloads) { - var download_label = downloads[i][0]; - var download_url = downloads[i][1]; + let download_label = downloads[i][0]; + let download_url = downloads[i][1]; if (!(/^(https?|ftp):/.test(download_url))){ if (!download_url.startsWith('/')){ - var download_url = '/' + download_url; + download_url = '/' + download_url; } - var download_url = root_url + download_url; + download_url = root_url + download_url; } - var inner_html = inner_html + "
" - + download_label + "
"; + inner_html = inner_html + "
" + + download_label + "
"; } } - var github_project_url = {{ "%r" % github_project_url }}; - if (github_project_url !== null && github_project_url.length > 0){ - var inner_html = inner_html + - "
On Github
" - + "
Project Home
" - + "
Issues
"; - } - var inner_html = inner_html + + + inner_html = inner_html + "
" + "
" + "Generated by Docs Versions Menu" + @@ -105,48 +86,50 @@ function _addVersionsMenu(version_data) { "
" + ""; menu.innerHTML = inner_html; - var parent = document.body; + let parent = document.body; parent.insertBefore(menu, parent.lastChild); // Add a warning banner for dev/outdated versions - var warning; - var msg; - if (version_data["warnings"][current_folder].indexOf("outdated") >=0){ + let warning; + let msg; + if (versionData["warnings"][current_folder].indexOf("outdated") >=0){ warning = document.createElement('div'); warning.setAttribute('class', 'admonition danger'); msg = "This document is for an outdated version."; - } else if (version_data["warnings"][current_folder].indexOf("unreleased") >=0){ + } else if (versionData["warnings"][current_folder].indexOf("unreleased") >=0){ warning = document.createElement('div'); warning.setAttribute('class', 'admonition danger'); msg = "This document is for an unreleased development version."; - } else if (version_data["warnings"][current_folder].indexOf("prereleased") >=0){ + } else if (versionData["warnings"][current_folder].indexOf("prereleased") >=0){ warning = document.createElement('div'); warning.setAttribute('class', 'admonition danger'); msg = "This document is for a pre-release development version."; } if (warning !== undefined){ - if (version_data["latest"] !== null){ + if (versionData["latest"] !== null){ msg = msg + " Documentation is available for the " + "latest public release." } warning.innerHTML = "

Note

" + "

" + msg + "

"; - var parent = document.querySelector('div.body') - || document.querySelector('div.document') - || document.body; + let 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', + }); -function addVersionsMenu() { - // We assume that we can load versions.json from - // https://.github.io//versions.json - // That is, there's a path between the hostname and versions.json - var json_file = {{ "%r" % json_file }}; - $.getJSON(json_file, _addVersionsMenu); + await _addVersionsMenu(await response.json(), rootUrl); {%- if badge_only %} diff --git a/src/docs_versions_menu/ext.py b/src/docs_versions_menu/ext.py index 0ea3b94..37139bc 100644 --- a/src/docs_versions_menu/ext.py +++ b/src/docs_versions_menu/ext.py @@ -43,11 +43,6 @@ def add_versions_menu_js_file(app): template_path.append(str(Path(__file__).parent / '_template')) renderer = SphinxRenderer(template_path=template_path) context = dict( - json_file=_JS( - '"/" + window.location.pathname.split("/")[1] + "/versions.json"' - ), - github_project_url=_JS('getGithubProjectUrl()'), - current_folder=_JS('getGhPagesCurrentFolder()'), badge_only=(app.config.html_theme != 'sphinx_rtd_theme'), menu_title="Docs", )