Skip to content

Commit

Permalink
make it work outside github pages
Browse files Browse the repository at this point in the history
  • Loading branch information
awoimbee committed May 5, 2021
1 parent 273fb4b commit b9f221d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 79 deletions.
131 changes: 57 additions & 74 deletions src/docs_versions_menu/_template/docs-versions-menu.js_t
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
"use strict";

function getGhPagesCurrentFolder() {
// Extract version folder under the assumpgion that the URL is of the form
// https://<username>.github.io/<project>/<version>/...
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://<username>.github.io/<project>/)
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 %}
Expand All @@ -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 =
"<span class='rst-current-version' data-toggle='rst-current-version'>" +
"<span class='fa fa-book'> {% if not badge_only %}{{ menu_title }}{% endif%} </span>" +
"<span>" + current_version + " </span>" +
Expand All @@ -60,93 +47,89 @@ function _addVersionsMenu(version_data) {
"<div class='injected'>" +
"<dl>" +
"<dt>Versions</dt>";
var i;
let i;
for (i in folders) {
var folder = folders[i];
if (folder == current_folder){
var inner_html = inner_html + "<strong><dd><a href='"
let folder = folders[i];
if (folder == current_folder) {
inner_html = inner_html + "<strong><dd><a href='"
+ current_url
+ "'>" + current_version + "</a></dd></strong>";
} else {
var inner_html = inner_html + "<dd><a href='"
inner_html = inner_html + "<dd><a href='"
+ current_url.replace(current_folder, folder)
+ "'>" + version_data["labels"][folder] + "</a></dd>";
+ "'>" + versionData["labels"][folder] + "</a></dd>";
}
}
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 +
"<dt>Downloads</dt>";
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 + "<dd><a href='" + download_url + "'>"
+ download_label + "</a></dd>";
inner_html = inner_html + "<dd><a href='" + download_url + "'>"
+ download_label + "</a></dd>";
}
}
var github_project_url = {{ "%r" % github_project_url }};
if (github_project_url !== null && github_project_url.length > 0){
var inner_html = inner_html +
"<dt>On Github</dt>"
+ "<dd><a href='" + github_project_url + "'>Project Home</a></dd>"
+ "<dd><a href='" + github_project_url + "/issues'>Issues</a></dd>";
}
var inner_html = inner_html +

inner_html = inner_html +
"</dl>" +
"<hr>" +
"<small>Generated by <a href='https://goerz.github.io/docs_versions_menu'>Docs Versions Menu</a>" +
"</small>" +
"</div>" +
"</div>";
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 <strong>outdated version</strong>.";
} 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 <strong>unreleased development version</strong>.";
} 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 <strong>pre-release development version</strong>.";
}
if (warning !== undefined){
if (version_data["latest"] !== null){
if (versionData["latest"] !== null){
msg = msg + " Documentation is available for the " + "<a href='" +
current_url.replace(current_folder, version_data["latest"]) +
current_url.replace(current_folder, versionData["latest"]) +
"'>latest public release</a>."
}
warning.innerHTML = "<p class='first admonition-title'>Note</p> " +
"<p class='last'> " + msg + "</p>";
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://<username>.github.io/<project>/versions.json
// That is, there's a <project> 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 %}

Expand Down
5 changes: 0 additions & 5 deletions src/docs_versions_menu/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down

0 comments on commit b9f221d

Please sign in to comment.