Skip to content

Commit

Permalink
rustdoc: add hash to filename of toolchain files
Browse files Browse the repository at this point in the history
All static files used by rustdoc are now stored in static.files/ and
include a hash of their contents. They no longer include the contents of
the --resource-suffix flag. This clarifies caching semantics. Anything
in static.files can use Cache-Control: immutable because any updates
will show up as a new URL.

Invocation-specific files like crates-NN.js, search-index-NN.js,
and sidebar-items-NN.js still get the resource suffix.

The --disable-minification flag is removed because it would vary the
output of static files based on invocation flags. Instead, for
rustdoc development purposes it's preferable to symlink static files
to a non-minified copy for quick iteration.
  • Loading branch information
jsha committed Oct 29, 2022
1 parent 68c836a commit f9e1f6f
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 506 deletions.
13 changes: 6 additions & 7 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ pub(crate) struct RenderOptions {
pub(crate) default_settings: FxHashMap<String, String>,
/// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
pub(crate) resource_suffix: String,
/// Whether to run the static CSS/JavaScript through a minifier when outputting them. `true` by
/// default.
pub(crate) enable_minification: bool,
/// Whether to create an index page in the root of the output directory. If this is true but
/// `enable_index_page` is None, generate a static listing of crates instead.
pub(crate) enable_index_page: bool,
Expand Down Expand Up @@ -416,7 +413,9 @@ impl Options {

let to_check = matches.opt_strs("check-theme");
if !to_check.is_empty() {
let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
let paths = match theme::load_css_paths(
std::str::from_utf8(static_files::STATIC_FILES.theme_light_css.bytes).unwrap(),
) {
Ok(p) => p,
Err(e) => {
diag.struct_err(&e.to_string()).emit();
Expand Down Expand Up @@ -557,7 +556,9 @@ impl Options {

let mut themes = Vec::new();
if matches.opt_present("theme") {
let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
let paths = match theme::load_css_paths(
std::str::from_utf8(static_files::STATIC_FILES.theme_light_css.bytes).unwrap(),
) {
Ok(p) => p,
Err(e) => {
diag.struct_err(&e.to_string()).emit();
Expand Down Expand Up @@ -675,7 +676,6 @@ impl Options {
ModuleSorting::Alphabetical
};
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
let enable_minification = !matches.opt_present("disable-minification");
let markdown_no_toc = matches.opt_present("markdown-no-toc");
let markdown_css = matches.opt_strs("markdown-css");
let markdown_playground_url = matches.opt_str("markdown-playground-url");
Expand Down Expand Up @@ -768,7 +768,6 @@ impl Options {
extern_html_root_takes_precedence,
default_settings,
resource_suffix,
enable_minification,
enable_index_page,
index_page,
static_root_path,
Expand Down
14 changes: 8 additions & 6 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::path::PathBuf;

use rustc_data_structures::fx::FxHashMap;

use crate::error::Error;
use crate::externalfiles::ExternalHtml;
use crate::html::format::{Buffer, Print};
use crate::html::render::{ensure_trailing_slash, StylePath};

use askama::Template;

use super::static_files::{StaticFiles, STATIC_FILES};

#[derive(Clone)]
pub(crate) struct Layout {
pub(crate) logo: String,
Expand Down Expand Up @@ -45,6 +46,9 @@ struct PageLayout<'a> {
static_root_path: &'a str,
page: &'a Page<'a>,
layout: &'a Layout,

files: &'static StaticFiles,

themes: Vec<String>,
sidebar: String,
content: String,
Expand All @@ -61,19 +65,17 @@ pub(crate) fn render<T: Print, S: Print>(
) -> String {
let static_root_path = page.get_static_root_path();
let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
let mut themes: Vec<String> = style_files
.iter()
.map(StylePath::basename)
.collect::<Result<_, Error>>()
.unwrap_or_default();
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
themes.sort();

let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
let sidebar = Buffer::html().to_display(sidebar);
PageLayout {
static_root_path,
page,
layout,
files: &STATIC_FILES,
themes,
sidebar,
content,
Expand Down
26 changes: 7 additions & 19 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::html::escape::Escape;
use crate::html::format::{join_with_double_colon, Buffer};
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
use crate::html::url_parts_builder::UrlPartsBuilder;
use crate::html::{layout, sources};
use crate::html::{layout, sources, static_files};
use crate::scrape_examples::AllCallLocations;
use crate::try_err;

Expand Down Expand Up @@ -498,7 +498,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
);

let (sender, receiver) = channel();
let mut scx = SharedContext {
let scx = SharedContext {
tcx,
src_root,
local_sources,
Expand All @@ -521,19 +521,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
call_locations,
};

// Add the default themes to the `Vec` of stylepaths
//
// Note that these must be added before `sources::render` is called
// so that the resulting source pages are styled
//
// `light.css` is not disabled because it is the stylesheet that stays loaded
// by the browser as the theme stylesheet. The theme system (hackily) works by
// changing the href to this stylesheet. All other themes are disabled to
// prevent rule conflicts
scx.style_files.push(StylePath { path: PathBuf::from("light.css") });
scx.style_files.push(StylePath { path: PathBuf::from("dark.css") });
scx.style_files.push(StylePath { path: PathBuf::from("ayu.css") });

let dst = output;
scx.ensure_dir(&dst)?;

Expand Down Expand Up @@ -647,10 +634,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
</section>\
</noscript>\
<link rel=\"stylesheet\" type=\"text/css\" \
href=\"{root_path}settings{suffix}.css\">\
<script defer src=\"{root_path}settings{suffix}.js\"></script>",
root_path = page.static_root_path.unwrap_or(""),
suffix = page.resource_suffix,
href=\"{static_root_path}{settings_css}\">\
<script defer src=\"{static_root_path}{settings_js}\"></script>",
static_root_path = page.static_root_path.unwrap_or(""),
settings_css = static_files::STATIC_FILES.settings_css,
settings_js = static_files::STATIC_FILES.settings_js,
)
},
&shared.style_files,
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ use crate::html::format::{
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
Buffer, Ending, PrintWithSpace,
};
use crate::html::highlight;
use crate::html::layout::Page;
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use crate::html::url_parts_builder::UrlPartsBuilder;
use crate::html::{highlight, static_files};

use askama::Template;
use itertools::Itertools;
Expand All @@ -52,8 +52,8 @@ struct PathComponent {
#[derive(Template)]
#[template(path = "print_item.html")]
struct ItemVars<'a> {
page: &'a Page<'a>,
static_root_path: &'a str,
clipboard_svg: &'static static_files::StaticFile,
typ: &'a str,
name: &'a str,
item_type: &'a str,
Expand Down Expand Up @@ -147,8 +147,8 @@ pub(super) fn print_item(
};

let item_vars = ItemVars {
page,
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(),
item_type: &item.type_().to_string(),
Expand Down
Loading

0 comments on commit f9e1f6f

Please sign in to comment.