Skip to content

Commit 8dc8d7a

Browse files
split extra_scripts to handle root_path shenanigans
1 parent 0b0a00c commit 8dc8d7a

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/librustdoc/html/layout.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ pub struct Page<'a> {
3030
pub description: &'a str,
3131
pub keywords: &'a str,
3232
pub resource_suffix: &'a str,
33+
pub extra_scripts: &'a [&'a str],
34+
pub static_extra_scripts: &'a [&'a str],
3335
}
3436

3537
pub fn render<T: fmt::Display, S: fmt::Display>(
3638
dst: &mut dyn io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
37-
css_file_extension: bool, themes: &[PathBuf], extra_scripts: &[&str])
39+
css_file_extension: bool, themes: &[PathBuf])
3840
-> io::Result<()>
3941
{
4042
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
@@ -164,6 +166,7 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
164166
</script>\
165167
<script src=\"{root_path}aliases.js\"></script>\
166168
<script src=\"{static_root_path}main{suffix}.js\"></script>\
169+
{static_extra_scripts}\
167170
{extra_scripts}\
168171
<script defer src=\"{root_path}search-index.js\"></script>\
169172
</body>\
@@ -211,9 +214,12 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
211214
page.resource_suffix))
212215
.collect::<String>(),
213216
suffix=page.resource_suffix,
214-
// TODO: break out a separate `static_extra_scripts` that uses `static_root_path` instead,
215-
// then leave `source-files.js` here and move `source-script.js` to the static version
216-
extra_scripts=extra_scripts.iter().map(|e| {
217+
static_extra_scripts=page.static_extra_scripts.iter().map(|e| {
218+
format!("<script src=\"{static_root_path}{extra_script}.js\"></script>",
219+
static_root_path=static_root_path,
220+
extra_script=e)
221+
}).collect::<String>(),
222+
extra_scripts=page.extra_scripts.iter().map(|e| {
217223
format!("<script src=\"{root_path}{extra_script}.js\"></script>",
218224
root_path=page.root_path,
219225
extra_script=e)

src/librustdoc/html/render.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,8 @@ themePicker.onblur = handleThemeButtonsBlur;
10891089
description: "List of crates",
10901090
keywords: BASIC_KEYWORDS,
10911091
resource_suffix: &cx.shared.resource_suffix,
1092+
extra_scripts: &[],
1093+
static_extra_scripts: &[],
10921094
};
10931095
krates.push(krate.name.clone());
10941096
krates.sort();
@@ -1107,7 +1109,7 @@ themePicker.onblur = handleThemeButtonsBlur;
11071109
try_err!(layout::render(&mut w, &cx.shared.layout,
11081110
&page, &(""), &content,
11091111
cx.shared.css_file_extension.is_some(),
1110-
&cx.shared.themes, &[]), &dst);
1112+
&cx.shared.themes), &dst);
11111113
try_err!(w.flush(), &dst);
11121114
}
11131115
}
@@ -1376,12 +1378,13 @@ impl<'a> SourceCollector<'a> {
13761378
description: &desc,
13771379
keywords: BASIC_KEYWORDS,
13781380
resource_suffix: &self.scx.resource_suffix,
1381+
extra_scripts: &["source-files"],
1382+
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
13791383
};
13801384
layout::render(&mut w, &self.scx.layout,
13811385
&page, &(""), &Source(contents),
13821386
self.scx.css_file_extension.is_some(),
1383-
&self.scx.themes, &["source-files",
1384-
&format!("source-script{}", page.resource_suffix)])?;
1387+
&self.scx.themes)?;
13851388
w.flush()?;
13861389
self.scx.local_sources.insert(p.clone(), href);
13871390
Ok(())
@@ -1967,6 +1970,8 @@ impl Context {
19671970
description: "List of all items in this crate",
19681971
keywords: BASIC_KEYWORDS,
19691972
resource_suffix: &self.shared.resource_suffix,
1973+
extra_scripts: &[],
1974+
static_extra_scripts: &[],
19701975
};
19711976
let sidebar = if let Some(ref version) = cache().crate_version {
19721977
format!("<p class='location'>Crate {}</p>\
@@ -1981,7 +1986,7 @@ impl Context {
19811986
try_err!(layout::render(&mut w, &self.shared.layout,
19821987
&page, &sidebar, &all,
19831988
self.shared.css_file_extension.is_some(),
1984-
&self.shared.themes, &[]),
1989+
&self.shared.themes),
19851990
&final_file);
19861991

19871992
// Generating settings page.
@@ -2001,7 +2006,7 @@ impl Context {
20012006
try_err!(layout::render(&mut w, &layout,
20022007
&page, &sidebar, &settings,
20032008
self.shared.css_file_extension.is_some(),
2004-
&themes, &[]),
2009+
&themes),
20052010
&settings_file);
20062011

20072012
Ok(())
@@ -2048,6 +2053,8 @@ impl Context {
20482053
description: &desc,
20492054
keywords: &keywords,
20502055
resource_suffix: &self.shared.resource_suffix,
2056+
extra_scripts: &[],
2057+
static_extra_scripts: &[],
20512058
};
20522059

20532060
{
@@ -2060,7 +2067,7 @@ impl Context {
20602067
&Sidebar{ cx: self, item: it },
20612068
&Item{ cx: self, item: it },
20622069
self.shared.css_file_extension.is_some(),
2063-
&self.shared.themes, &[])?;
2070+
&self.shared.themes)?;
20642071
} else {
20652072
let mut url = self.root_path();
20662073
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {

src/test/rustdoc/static-root-path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@
1616
// @matches - '"\.\./search-index\.js"'
1717
// @!matches - '"/cache/search-index\.js"'
1818
pub struct SomeStruct;
19+
20+
// @has src/static_root_path/static-root-path.rs.html
21+
// @matches - '"/cache/source-script\.js"'
22+
// @!matches - '"\.\./\.\./source-script\.js"'
23+
// @matches - '"\.\./\.\./source-files.js"'
24+
// @!matches - '"/cache/source-files\.js"'

0 commit comments

Comments
 (0)