From ea51f8a13d20af173716811ce0b5943ebfd5b997 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Wed, 28 Sep 2022 23:52:00 -0700 Subject: [PATCH] Make --static-root-path point to static.files --- src/librustdoc/html/layout.rs | 9 ++++++--- src/librustdoc/html/render/print_item.rs | 2 +- src/librustdoc/html/render/write_shared.rs | 9 ++++++--- src/librustdoc/html/static_files.rs | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 087e9219b67c6..c1b3526eb4541 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/print_item.rs b/src/librustdoc/html/render/print_item.rs index 0979742ce24e6..b265daaec6da3 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 3b363be293553..f600b6577e7b4 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 1904077fcc54e..c1643b6f4f095 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 {