Skip to content

Commit f0683f9

Browse files
committed
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).
1 parent 3649b90 commit f0683f9

File tree

5 files changed

+37
-49
lines changed

5 files changed

+37
-49
lines changed

src/librustdoc/html/layout.rs

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct PageLayout<'a> {
5454
sidebar: String,
5555
content: String,
5656
krate_with_trailing_slash: String,
57+
crate rustdoc_version: &'a str,
5758
}
5859

5960
crate fn render<T: Print, S: Print>(
@@ -71,6 +72,7 @@ crate fn render<T: Print, S: Print>(
7172
.map(StylePath::basename)
7273
.collect::<Result<_, Error>>()
7374
.unwrap_or_default();
75+
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7476
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7577
let sidebar = Buffer::html().to_display(sidebar);
7678
let teractx = tera::Context::from_serialize(PageLayout {
@@ -81,6 +83,7 @@ crate fn render<T: Print, S: Print>(
8183
sidebar,
8284
content,
8385
krate_with_trailing_slash,
86+
rustdoc_version,
8487
})
8588
.unwrap();
8689
templates.render("page.html", &teractx).unwrap()

src/librustdoc/html/render/write_shared.rs

+2-32
Original file line numberDiff line numberDiff line change
@@ -265,45 +265,15 @@ pub(super) fn write_shared(
265265
let mut themes: Vec<&String> = themes.iter().collect();
266266
themes.sort();
267267

268-
// FIXME: this should probably not be a toolchain file since it depends on `--theme`.
269-
// But it seems a shame to copy it over and over when it's almost always the same.
270-
// Maybe we can change the representation to move this out of main.js?
271-
write_minify(
272-
"main.js",
273-
static_files::MAIN_JS
274-
.replace(
275-
"/* INSERT THEMES HERE */",
276-
&format!(" = {}", serde_json::to_string(&themes).unwrap()),
277-
)
278-
.replace(
279-
"/* INSERT RUSTDOC_VERSION HERE */",
280-
&format!(
281-
"rustdoc {}",
282-
rustc_interface::util::version_str().unwrap_or("unknown version")
283-
),
284-
),
285-
cx,
286-
options,
287-
)?;
268+
write_minify("main.js", static_files::MAIN_JS, cx, options)?;
288269
write_minify("search.js", static_files::SEARCH_JS, cx, options)?;
289270
write_minify("settings.js", static_files::SETTINGS_JS, cx, options)?;
290271

291272
if cx.include_sources {
292273
write_minify("source-script.js", static_files::sidebar::SOURCE_SCRIPT, cx, options)?;
293274
}
294275

295-
{
296-
write_minify(
297-
"storage.js",
298-
format!(
299-
"var resourcesSuffix = \"{}\";{}",
300-
cx.shared.resource_suffix,
301-
static_files::STORAGE_JS
302-
),
303-
cx,
304-
options,
305-
)?;
306-
}
276+
write_minify("storage.js", static_files::STORAGE_JS, cx, options)?;
307277

308278
if cx.shared.layout.scrape_examples_extension {
309279
cx.write_minify(

src/librustdoc/html/static/js/main.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,29 @@ if (!DOMTokenList.prototype.remove) {
3737
};
3838
}
3939

40-
(function () {
41-
var rustdocVars = document.getElementById("rustdoc-vars");
42-
if (rustdocVars) {
43-
window.rootPath = rustdocVars.attributes["data-root-path"].value;
44-
window.currentCrate = rustdocVars.attributes["data-current-crate"].value;
45-
window.searchJS = rustdocVars.attributes["data-search-js"].value;
46-
window.searchIndexJS = rustdocVars.attributes["data-search-index-js"].value;
40+
// Get a value from the rustdoc-vars div, which is used to convey data from
41+
// Rust to the JS. If there is no such element, return null.
42+
function getVar(name) {
43+
var el = document.getElementById("rustdoc-vars");
44+
if (el) {
45+
return el.attributes["data-" + name].value;
46+
} else {
47+
return null;
4748
}
49+
}
50+
51+
// Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL
52+
// for a resource under the root-path, with the resource-suffix.
53+
function resourcePath(basename, extension) {
54+
return getVar("root-path") + basename + getVar("resource-suffix") + extension;
55+
}
56+
57+
58+
(function () {
59+
window.rootPath = getVar("root-path");
60+
window.currentCrate = getVar("current-crate");
61+
window.searchJS = resourcePath("search", "js");
62+
window.searchIndexJS = resourcePath("search-index", "js");
4863
var sidebarVars = document.getElementById("sidebar-vars");
4964
if (sidebarVars) {
5065
window.sidebarCurrent = {
@@ -115,7 +130,7 @@ function hideThemeButtonState() {
115130
(function () {
116131
var themeChoices = getThemesElement();
117132
var themePicker = getThemePickerElement();
118-
var availableThemes/* INSERT THEMES HERE */;
133+
var availableThemes = getVar("themes").split(",");
119134

120135
function switchThemeButtonState() {
121136
if (themeChoices.style.display === "block") {
@@ -980,7 +995,7 @@ function hideThemeButtonState() {
980995
var rustdoc_version = document.createElement("span");
981996
rustdoc_version.className = "bottom";
982997
var rustdoc_version_code = document.createElement("code");
983-
rustdoc_version_code.innerText = "/* INSERT RUSTDOC_VERSION HERE */";
998+
rustdoc_version_code.innerText = "rustdoc " + getVar("rustdoc-version");
984999
rustdoc_version.appendChild(rustdoc_version_code);
9851000

9861001
container.appendChild(rustdoc_version);

src/librustdoc/html/static/js/storage.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// From rust:
2-
/* global resourcesSuffix */
31
var darkThemes = ["dark", "ayu"];
42
window.currentTheme = document.getElementById("themeStyle");
53
window.mainTheme = document.getElementById("mainThemeStyle");
@@ -107,9 +105,8 @@ function getCurrentValue(name) {
107105
}
108106

109107
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
110-
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
111-
var fullNewTheme = newTheme + resourcesSuffix + ".css";
112-
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
108+
var newHref = mainStyleElem.href.replace(
109+
/\/rustdoc([^/]*)\.css/, "/" + newTheme + "$1" + ".css");
113110

114111
// If this new value comes from a system setting or from the previously
115112
// saved theme, no need to save it.

src/librustdoc/html/templates/page.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
id="mainThemeStyle"> {#- -#}
1515
{%- for theme in themes -%}
1616
<link rel="stylesheet" type="text/css" {# -#}
17+
href="{{static_root_path | safe}}{{theme}}{{page.resource_suffix}}.css" {#- -#}
1718
{%- if theme == "light" -%}
1819
id="themeStyle"
1920
{%- else -%}
2021
disabled
2122
{%- endif -%}
22-
href="{{static_root_path | safe}}{{theme}}{{page.resource_suffix}}.css"> {#- -#}
23+
>
2324
{%- endfor -%}
2425
<script id="default-settings" {# -#}
2526
{% for k, v in layout.default_settings %}
@@ -122,8 +123,10 @@
122123
<div id="rustdoc-vars" {# -#}
123124
data-root-path="{{page.root_path | safe}}" {# -#}
124125
data-current-crate="{{layout.krate}}" {# -#}
125-
data-search-index-js="{{page.root_path | safe}}search-index{{page.resource_suffix}}.js" {# -#}
126-
data-search-js="{{static_root_path | safe}}search{{page.resource_suffix}}.js"> {#- -#}
126+
data-themes="{{themes | join(sep=",") }}" {# -#}
127+
data-resource-suffix="{{page.resource_suffix}}" {# -#}
128+
data-rustdoc-version="{{rustdoc_version}}" {# -#}
129+
> {#- -#}
127130
</div>
128131
</body> {#- -#}
129132
</html> {#- -#}

0 commit comments

Comments
 (0)