From 173528e3685bc4321630b7f979d01896c57a5c15 Mon Sep 17 00:00:00 2001 From: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> Date: Tue, 23 Jun 2020 17:05:12 -0400 Subject: [PATCH] Add version control menu (#5222) * Add version control menu * Constify things Co-authored-by: Lysandre Debut * Apply suggestions from code review Co-authored-by: Julien Chaumond Co-authored-by: Lysandre Debut Co-authored-by: Julien Chaumond --- docs/source/_static/css/huggingface.css | 41 +++++++++++++ docs/source/_static/js/custom.js | 78 +++++++++++++++++++++++++ docs/source/conf.py | 4 +- 3 files changed, 121 insertions(+), 2 deletions(-) diff --git a/docs/source/_static/css/huggingface.css b/docs/source/_static/css/huggingface.css index 808f8005fc..eeced355df 100644 --- a/docs/source/_static/css/huggingface.css +++ b/docs/source/_static/css/huggingface.css @@ -1,5 +1,45 @@ /* Our DOM objects */ +/* Version control */ + +.version-button { + background-color: #6670FF; + color: white; + border: none; + padding: 5px; + font-size: 15px; + cursor: pointer; +} + +.version-button:hover, .version-button:focus { + background-color: #A6B0FF; +} + +.version-dropdown { + display: none; + background-color: #6670FF; + min-width: 160px; + overflow: auto; + font-size: 15px; +} + +.version-dropdown a { + color: white; + padding: 3px 4px; + text-decoration: none; + display: block; +} + +.version-dropdown a:hover { + background-color: #A6B0FF; +} + +.version-show { + display: block; +} + +/* Framework selector */ + .framework-selector { display: flex; flex-direction: row; @@ -38,6 +78,7 @@ /* The research field on top of the toc tree */ .wy-side-nav-search{ + padding-top: 0; background-color: #6670FF; } diff --git a/docs/source/_static/js/custom.js b/docs/source/_static/js/custom.js index ac9388531b..4e760e7e1a 100644 --- a/docs/source/_static/js/custom.js +++ b/docs/source/_static/js/custom.js @@ -1,3 +1,26 @@ +// These two things need to be updated at each release for the version selector. +// Last stable version +const stableVersion = "v2.11.0" +// Dictionary doc folder to label +const versionMapping = { + "master": "master", + "": "v2.11.0 (stable)", + "v2.10.0": "v2.10.0", + "v2.9.1": "v2.9.0/v2.9.1", + "v2.8.0": "v2.8.0", + "v2.7.0": "v2.7.0", + "v2.6.0": "v2.6.0", + "v2.5.1": "v2.5.0/v2.5.1", + "v2.4.0": "v2.4.0/v2.4.1", + "v2.3.0": "v2.3.0", + "v2.2.0": "v2.2.0/v2.2.1/v2.2.2", + "v2.1.1": "v2.1.1", + "v2.0.0": "v2.0.0", + "v1.2.0": "v1.2.0", + "v1.1.0": "v1.1.0", + "v1.0.0": "v1.0.0" +} + function addIcon() { const huggingFaceLogo = "https://huggingface.co/landing/assets/transformers-docs/huggingface_logo.svg"; const image = document.createElement("img"); @@ -58,6 +81,60 @@ function addGithubButton() { document.querySelector(".wy-side-nav-search .icon-home").insertAdjacentHTML('afterend', div); } +function addVersionControl() { + // To grab the version currently in view, we parse the url + const parts = location.toString().split('/'); + let versionIndex = parts.length - 2 + // Main classes and models are nested so we need to go deeper + if (parts[versionIndex] == "main_classes" || parts[versionIndex] == "model_doc") { + versionIndex = parts.length - 3 + } + const version = parts[versionIndex]; + + // Menu with all the links, + const versionMenu = document.createElement("div"); + + const htmlLines = []; + for (const [key, value] of Object.entries(versionMapping)) { + var urlParts = (key == "") ? [] : [key]; + urlParts = urlParts.concat(parts.slice(versionIndex)); + htmlLines.push(`${value}`); + } + + versionMenu.classList.add("version-dropdown"); + versionMenu.innerHTML = htmlLines.join('\n'); + + // Button for version selection + const versionButton = document.createElement("div"); + versionButton.classList.add("version-button"); + let label = (version == "transformers") ? stableVersion : version + versionButton.innerText = label.concat(" ▼"); + + // Toggle the menu when we click on the button + versionButton.addEventListener("click", () => { + versionMenu.classList.toggle("version-show"); + }); + + // Hide the menu when we click elsewhere + window.addEventListener("click", (event) => { + if (event.target != versionButton){ + versionMenu.classList.remove('version-show'); + } + }); + + // Container + const div = document.createElement("div"); + div.appendChild(versionButton); + div.appendChild(versionMenu); + div.style.paddingTop = '25px'; + div.style.backgroundColor = '#6670FF'; + div.style.display = 'block'; + div.style.textAlign = 'center'; + + const scrollDiv = document.querySelector(".wy-side-scroll"); + scrollDiv.insertBefore(div, scrollDiv.children[1]); +} + function addHfMenu() { const div = `