diff options
| author | Jacob Hoffman-Andrews <github@hoffman-andrews.com> | 2021-11-23 19:22:29 -0800 |
|---|---|---|
| committer | Jacob Hoffman-Andrews <github@hoffman-andrews.com> | 2021-11-24 19:41:47 -0800 |
| commit | f0683f98fa114cc4f9e795031f44be3eebb65790 (patch) | |
| tree | 8e63e8f5420674b0c742d1570b0ae0f3b8198340 /src/librustdoc/html/static | |
| parent | 3649b90b33acbc0248efa0bc35675081977aefc2 (diff) | |
| download | rust-f0683f98fa114cc4f9e795031f44be3eebb65790.tar.gz rust-f0683f98fa114cc4f9e795031f44be3eebb65790.zip | |
Move themes and version into rustdoc-vars
We had been injecting the list of themes and the rustdoc version into main.js by rewriting it at doc generation time. By avoiding this rewrite, we can make it easier to edit main.js without regenerating all the docs. Added a more convenient accessor for rustdoc-vars. Changed storage.js to not rely on resourcesSuffix. It could in theory use rustdoc-vars, but because rustdoc-vars is at the end of the HTML, it's not available when storage.js runs (very early in page load).
Diffstat (limited to 'src/librustdoc/html/static')
| -rw-r--r-- | src/librustdoc/html/static/js/main.js | 33 | ||||
| -rw-r--r-- | src/librustdoc/html/static/js/storage.js | 7 |
2 files changed, 26 insertions, 14 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index c9fa72cbaab..4843aa575b8 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -37,14 +37,29 @@ if (!DOMTokenList.prototype.remove) { }; } -(function () { - var rustdocVars = document.getElementById("rustdoc-vars"); - if (rustdocVars) { - window.rootPath = rustdocVars.attributes["data-root-path"].value; - window.currentCrate = rustdocVars.attributes["data-current-crate"].value; - window.searchJS = rustdocVars.attributes["data-search-js"].value; - window.searchIndexJS = rustdocVars.attributes["data-search-index-js"].value; +// Get a value from the rustdoc-vars div, which is used to convey data from +// Rust to the JS. If there is no such element, return null. +function getVar(name) { + var el = document.getElementById("rustdoc-vars"); + if (el) { + return el.attributes["data-" + name].value; + } else { + return null; } +} + +// Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL +// for a resource under the root-path, with the resource-suffix. +function resourcePath(basename, extension) { + return getVar("root-path") + basename + getVar("resource-suffix") + extension; +} + + +(function () { + window.rootPath = getVar("root-path"); + window.currentCrate = getVar("current-crate"); + window.searchJS = resourcePath("search", "js"); + window.searchIndexJS = resourcePath("search-index", "js"); var sidebarVars = document.getElementById("sidebar-vars"); if (sidebarVars) { window.sidebarCurrent = { @@ -115,7 +130,7 @@ function hideThemeButtonState() { (function () { var themeChoices = getThemesElement(); var themePicker = getThemePickerElement(); - var availableThemes/* INSERT THEMES HERE */; + var availableThemes = getVar("themes").split(","); function switchThemeButtonState() { if (themeChoices.style.display === "block") { @@ -980,7 +995,7 @@ function hideThemeButtonState() { var rustdoc_version = document.createElement("span"); rustdoc_version.className = "bottom"; var rustdoc_version_code = document.createElement("code"); - rustdoc_version_code.innerText = "/* INSERT RUSTDOC_VERSION HERE */"; + rustdoc_version_code.innerText = "rustdoc " + getVar("rustdoc-version"); rustdoc_version.appendChild(rustdoc_version_code); container.appendChild(rustdoc_version); diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index 78ed17e6899..606c237aea7 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -1,5 +1,3 @@ -// From rust: -/* global resourcesSuffix */ var darkThemes = ["dark", "ayu"]; window.currentTheme = document.getElementById("themeStyle"); window.mainTheme = document.getElementById("mainThemeStyle"); @@ -107,9 +105,8 @@ function getCurrentValue(name) { } function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) { - var fullBasicCss = "rustdoc" + resourcesSuffix + ".css"; - var fullNewTheme = newTheme + resourcesSuffix + ".css"; - var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme); + var newHref = mainStyleElem.href.replace( + /\/rustdoc([^/]*)\.css/, "/" + newTheme + "$1" + ".css"); // If this new value comes from a system setting or from the previously // saved theme, no need to save it. |
