Skip to content

Commit

Permalink
split extra_scripts to handle root_path shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus committed Dec 20, 2018
1 parent 0b0a00c commit 8dc8d7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ pub struct Page<'a> {
pub description: &'a str,
pub keywords: &'a str,
pub resource_suffix: &'a str,
pub extra_scripts: &'a [&'a str],
pub static_extra_scripts: &'a [&'a str],
}

pub fn render<T: fmt::Display, S: fmt::Display>(
dst: &mut dyn io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
css_file_extension: bool, themes: &[PathBuf], extra_scripts: &[&str])
css_file_extension: bool, themes: &[PathBuf])
-> io::Result<()>
{
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
Expand Down Expand Up @@ -164,6 +166,7 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
</script>\
<script src=\"{root_path}aliases.js\"></script>\
<script src=\"{static_root_path}main{suffix}.js\"></script>\
{static_extra_scripts}\
{extra_scripts}\
<script defer src=\"{root_path}search-index.js\"></script>\
</body>\
Expand Down Expand Up @@ -211,9 +214,12 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
page.resource_suffix))
.collect::<String>(),
suffix=page.resource_suffix,
// TODO: break out a separate `static_extra_scripts` that uses `static_root_path` instead,
// then leave `source-files.js` here and move `source-script.js` to the static version
extra_scripts=extra_scripts.iter().map(|e| {
static_extra_scripts=page.static_extra_scripts.iter().map(|e| {
format!("<script src=\"{static_root_path}{extra_script}.js\"></script>",
static_root_path=static_root_path,
extra_script=e)
}).collect::<String>(),
extra_scripts=page.extra_scripts.iter().map(|e| {
format!("<script src=\"{root_path}{extra_script}.js\"></script>",
root_path=page.root_path,
extra_script=e)
Expand Down
19 changes: 13 additions & 6 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,8 @@ themePicker.onblur = handleThemeButtonsBlur;
description: "List of crates",
keywords: BASIC_KEYWORDS,
resource_suffix: &cx.shared.resource_suffix,
extra_scripts: &[],
static_extra_scripts: &[],
};
krates.push(krate.name.clone());
krates.sort();
Expand All @@ -1107,7 +1109,7 @@ themePicker.onblur = handleThemeButtonsBlur;
try_err!(layout::render(&mut w, &cx.shared.layout,
&page, &(""), &content,
cx.shared.css_file_extension.is_some(),
&cx.shared.themes, &[]), &dst);
&cx.shared.themes), &dst);
try_err!(w.flush(), &dst);
}
}
Expand Down Expand Up @@ -1376,12 +1378,13 @@ impl<'a> SourceCollector<'a> {
description: &desc,
keywords: BASIC_KEYWORDS,
resource_suffix: &self.scx.resource_suffix,
extra_scripts: &["source-files"],
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
};
layout::render(&mut w, &self.scx.layout,
&page, &(""), &Source(contents),
self.scx.css_file_extension.is_some(),
&self.scx.themes, &["source-files",
&format!("source-script{}", page.resource_suffix)])?;
&self.scx.themes)?;
w.flush()?;
self.scx.local_sources.insert(p.clone(), href);
Ok(())
Expand Down Expand Up @@ -1967,6 +1970,8 @@ impl Context {
description: "List of all items in this crate",
keywords: BASIC_KEYWORDS,
resource_suffix: &self.shared.resource_suffix,
extra_scripts: &[],
static_extra_scripts: &[],
};
let sidebar = if let Some(ref version) = cache().crate_version {
format!("<p class='location'>Crate {}</p>\
Expand All @@ -1981,7 +1986,7 @@ impl Context {
try_err!(layout::render(&mut w, &self.shared.layout,
&page, &sidebar, &all,
self.shared.css_file_extension.is_some(),
&self.shared.themes, &[]),
&self.shared.themes),
&final_file);

// Generating settings page.
Expand All @@ -2001,7 +2006,7 @@ impl Context {
try_err!(layout::render(&mut w, &layout,
&page, &sidebar, &settings,
self.shared.css_file_extension.is_some(),
&themes, &[]),
&themes),
&settings_file);

Ok(())
Expand Down Expand Up @@ -2048,6 +2053,8 @@ impl Context {
description: &desc,
keywords: &keywords,
resource_suffix: &self.shared.resource_suffix,
extra_scripts: &[],
static_extra_scripts: &[],
};

{
Expand All @@ -2060,7 +2067,7 @@ impl Context {
&Sidebar{ cx: self, item: it },
&Item{ cx: self, item: it },
self.shared.css_file_extension.is_some(),
&self.shared.themes, &[])?;
&self.shared.themes)?;
} else {
let mut url = self.root_path();
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/rustdoc/static-root-path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
// @matches - '"\.\./search-index\.js"'
// @!matches - '"/cache/search-index\.js"'
pub struct SomeStruct;

// @has src/static_root_path/static-root-path.rs.html
// @matches - '"/cache/source-script\.js"'
// @!matches - '"\.\./\.\./source-script\.js"'
// @matches - '"\.\./\.\./source-files.js"'
// @!matches - '"/cache/source-files\.js"'

0 comments on commit 8dc8d7a

Please sign in to comment.