diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 087e9219b67c..c1b3526eb454 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -35,15 +35,18 @@ pub(crate) struct Page<'a> { } impl<'a> Page<'a> { - pub(crate) fn get_static_root_path(&self) -> &str { - self.static_root_path.unwrap_or(self.root_path) + pub(crate) fn get_static_root_path(&self) -> String { + match self.static_root_path { + Some(s) => s.to_string(), + None => format!("{}{}", self.root_path, "static.files/"), + } } } #[derive(Template)] #[template(path = "page.html")] struct PageLayout<'a> { - static_root_path: &'a str, + static_root_path: String, page: &'a Page<'a>, layout: &'a Layout, diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 5263d0d22328..51843a505f70 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -636,7 +636,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { \ ", - static_root_path = page.static_root_path.unwrap_or(""), + static_root_path = page.get_static_root_path(), settings_css = static_files::STATIC_FILES.settings_css, settings_js = static_files::STATIC_FILES.settings_js, ) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 713c5d1bcb66..2920a81e5b22 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -147,7 +147,7 @@ pub(super) fn print_item( }; let item_vars = ItemVars { - static_root_path: page.get_static_root_path(), + static_root_path: &page.get_static_root_path(), clipboard_svg: &static_files::STATIC_FILES.clipboard_svg, typ, name: item.name.as_ref().unwrap().as_str(), diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 723c502c584f..07d139e9e121 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -24,7 +24,8 @@ use crate::{try_err, try_none}; /// URL if the contents change, so they are safe to cache with the /// `Cache-Control: immutable` directive. They are written under the static.files/ /// directory and are written when --emit-type is empty (default) or contains -/// "toolchain-specific". +/// "toolchain-specific". If using the --static-root-path flag, it should point +/// to a URL path prefix where each of these filenames can be fetched. /// - Invocation specific files. These are generated based on the crate(s) being /// documented. Their filenames need to be predictable without knowing their /// contents, so they do not include a hash in their filename and are not safe to @@ -85,8 +86,10 @@ pub(super) fn write_shared( if options.emit.is_empty() || options.emit.contains(&EmitType::Toolchain) { for f in static_files::STATIC_FILES_LIST { - let filename = static_files::static_filename(f.filename, f.bytes); - cx.shared.fs.write(cx.dst.join(filename), f.minified())?; + let filename = cx.dst.join( + Path::new("static.files/").join(static_files::static_filename(f.filename, f.bytes)), + ); + cx.shared.fs.write(filename, f.minified())?; } } diff --git a/src/librustdoc/html/static_files.rs b/src/librustdoc/html/static_files.rs index 7b89dc8cd9f7..c922890bc0ba 100644 --- a/src/librustdoc/html/static_files.rs +++ b/src/librustdoc/html/static_files.rs @@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHasher; use std::hash::Hasher; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::{fmt, str}; pub(crate) struct StaticFile { @@ -51,7 +51,7 @@ pub(crate) fn suffix_path(filename: &str, suffix: &str) -> PathBuf { pub(crate) fn static_filename(filename: &str, contents: &[u8]) -> PathBuf { let filename = filename.rsplit("/").next().unwrap(); - Path::new("static.files").join(suffix_path(filename, &static_suffix(contents))) + suffix_path(filename, &static_suffix(contents)) } fn static_suffix(bytes: &[u8]) -> String { diff --git a/src/test/rustdoc/static-root-path.rs b/src/test/rustdoc/static-root-path.rs index 04de52c0df0f..86928b0fb0a8 100644 --- a/src/test/rustdoc/static-root-path.rs +++ b/src/test/rustdoc/static-root-path.rs @@ -1,18 +1,18 @@ // compile-flags:-Z unstable-options --static-root-path /cache/ // @has static_root_path/struct.SomeStruct.html -// @matchesraw - '"/cache/static.files/main-' +// @matchesraw - '"/cache/main-' // @!matchesraw - '"\.\./main' // @matchesraw - 'data-root-path="\.\./"' // @!matchesraw - '"/cache/search-index\.js"' pub struct SomeStruct; // @has src/static_root_path/static-root-path.rs.html -// @matchesraw - '"/cache/static.files/source-script-' +// @matchesraw - '"/cache/source-script-' // @!matchesraw - '"\.\./\.\./source-script' // @matchesraw - '"\.\./\.\./source-files.js"' // @!matchesraw - '"/cache/source-files\.js"' // @has settings.html -// @matchesraw - '/cache/static.files/settings-' +// @matchesraw - '/cache/settings-' // @!matchesraw - '\../settings'