Skip to content

Commit 0b0a00c

Browse files
new --static-root-path flag for controlling static file locations
1 parent 0a4a4ff commit 0b0a00c

File tree

6 files changed

+80
-14
lines changed

6 files changed

+80
-14
lines changed

src/doc/rustdoc/src/unstable-features.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,18 @@ Using `index-page` option enables `enable-index-page` option as well.
402402
### `--enable-index-page`: generate a default index page for docs
403403

404404
This feature allows the generation of a default index-page which lists the generated crates.
405+
406+
### `--static-root-path`: control how static files are loaded in HTML output
407+
408+
Using this flag looks like this:
409+
410+
```bash
411+
$ rustdoc src/lib.rs -Z unstable-options --static-root-path '/cache/'
412+
```
413+
414+
This flag controls how rustdoc links to its static files on HTML pages. If you're hosting a lot of
415+
crates' docs generated by the same version of rustdoc, you can use this flag to cache rustdoc's CSS,
416+
JavaScript, and font files in a single location, rather than duplicating it once per "doc root"
417+
(grouping of crate docs generated into the same output directory, like with `cargo doc`). Per-crate
418+
files like the search index will still load from the documentation root, but anything that gets
419+
renamed with `--resource-suffix` will load from the given path.

src/librustdoc/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ pub struct RenderOptions {
181181
/// A file to use as the index page at the root of the output directory. Overrides
182182
/// `enable_index_page` to be true if set.
183183
pub index_page: Option<PathBuf>,
184+
/// An optional path to use as the location of static files. If not set, uses combinations of
185+
/// `../` to reach the documentation root.
186+
pub static_root_path: Option<String>,
184187

185188
// Options specific to reading standalone Markdown files
186189

@@ -433,6 +436,7 @@ impl Options {
433436
let markdown_playground_url = matches.opt_str("markdown-playground-url");
434437
let crate_version = matches.opt_str("crate-version");
435438
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
439+
let static_root_path = matches.opt_str("static-root-path");
436440

437441
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
438442

@@ -471,6 +475,7 @@ impl Options {
471475
enable_minification,
472476
enable_index_page,
473477
index_page,
478+
static_root_path,
474479
markdown_no_toc,
475480
markdown_css,
476481
markdown_playground_url,

src/librustdoc/html/layout.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Page<'a> {
2626
pub title: &'a str,
2727
pub css_class: &'a str,
2828
pub root_path: &'a str,
29+
pub static_root_path: Option<&'a str>,
2930
pub description: &'a str,
3031
pub keywords: &'a str,
3132
pub resource_suffix: &'a str,
@@ -36,6 +37,7 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
3637
css_file_extension: bool, themes: &[PathBuf], extra_scripts: &[&str])
3738
-> io::Result<()>
3839
{
40+
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
3941
write!(dst,
4042
"<!DOCTYPE html>\
4143
<html lang=\"en\">\
@@ -46,20 +48,20 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
4648
<meta name=\"description\" content=\"{description}\">\
4749
<meta name=\"keywords\" content=\"{keywords}\">\
4850
<title>{title}</title>\
49-
<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}normalize{suffix}.css\">\
50-
<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}rustdoc{suffix}.css\" \
51+
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
52+
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
5153
id=\"mainThemeStyle\">\
5254
{themes}\
53-
<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}dark{suffix}.css\">\
54-
<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}light{suffix}.css\" \
55+
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}dark{suffix}.css\">\
56+
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}light{suffix}.css\" \
5557
id=\"themeStyle\">\
56-
<script src=\"{root_path}storage{suffix}.js\"></script>\
57-
<noscript><link rel=\"stylesheet\" href=\"{root_path}noscript{suffix}.css\"></noscript>\
58+
<script src=\"{static_root_path}storage{suffix}.js\"></script>\
59+
<noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
5860
{css_extension}\
5961
{favicon}\
6062
{in_header}\
6163
<style type=\"text/css\">\
62-
#crate-search{{background-image:url(\"{root_path}down-arrow{suffix}.svg\");}}\
64+
#crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
6365
</style>\
6466
</head>\
6567
<body class=\"rustdoc {css_class}\">\
@@ -77,11 +79,13 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
7779
</nav>\
7880
<div class=\"theme-picker\">\
7981
<button id=\"theme-picker\" aria-label=\"Pick another theme!\">\
80-
<img src=\"{root_path}brush{suffix}.svg\" width=\"18\" alt=\"Pick another theme!\">\
82+
<img src=\"{static_root_path}brush{suffix}.svg\" \
83+
width=\"18\" \
84+
alt=\"Pick another theme!\">\
8185
</button>\
8286
<div id=\"theme-choices\"></div>\
8387
</div>\
84-
<script src=\"{root_path}theme{suffix}.js\"></script>\
88+
<script src=\"{static_root_path}theme{suffix}.js\"></script>\
8589
<nav class=\"sub\">\
8690
<form class=\"search-form js-only\">\
8791
<div class=\"search-container\">\
@@ -96,7 +100,9 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
96100
type=\"search\">\
97101
</div>\
98102
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\
99-
<img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\
103+
<img src=\"{static_root_path}wheel{suffix}.svg\" \
104+
width=\"18\" \
105+
alt=\"Change settings\">\
100106
</a>\
101107
</div>\
102108
</form>\
@@ -157,19 +163,22 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
157163
window.currentCrate = \"{krate}\";\
158164
</script>\
159165
<script src=\"{root_path}aliases.js\"></script>\
160-
<script src=\"{root_path}main{suffix}.js\"></script>\
166+
<script src=\"{static_root_path}main{suffix}.js\"></script>\
161167
{extra_scripts}\
162168
<script defer src=\"{root_path}search-index.js\"></script>\
163169
</body>\
164170
</html>",
165171
css_extension = if css_file_extension {
166-
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme{suffix}.css\">",
167-
root_path = page.root_path,
172+
format!("<link rel=\"stylesheet\" \
173+
type=\"text/css\" \
174+
href=\"{static_root_path}theme{suffix}.css\">",
175+
static_root_path = static_root_path,
168176
suffix=page.resource_suffix)
169177
} else {
170178
String::new()
171179
},
172180
content = *t,
181+
static_root_path = static_root_path,
173182
root_path = page.root_path,
174183
css_class = page.css_class,
175184
logo = if layout.logo.is_empty() {
@@ -197,11 +206,13 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
197206
.filter_map(|t| t.file_stem())
198207
.filter_map(|t| t.to_str())
199208
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}{}.css">"#,
200-
page.root_path,
209+
static_root_path,
201210
t,
202211
page.resource_suffix))
203212
.collect::<String>(),
204213
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
205216
extra_scripts=extra_scripts.iter().map(|e| {
206217
format!("<script src=\"{root_path}{extra_script}.js\"></script>",
207218
root_path=page.root_path,

src/librustdoc/html/render.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ struct SharedContext {
140140
/// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
141141
/// "light-v2.css").
142142
pub resource_suffix: String,
143+
/// Optional path string to be used to load static files on output pages. If not set, uses
144+
/// combinations of `../` to reach the documentation root.
145+
pub static_root_path: Option<String>,
143146
}
144147

145148
impl SharedContext {
@@ -506,6 +509,7 @@ pub fn run(mut krate: clean::Crate,
506509
extension_css,
507510
extern_html_root_urls,
508511
resource_suffix,
512+
static_root_path,
509513
..
510514
} = options;
511515

@@ -533,6 +537,7 @@ pub fn run(mut krate: clean::Crate,
533537
sort_modules_alphabetically,
534538
themes,
535539
resource_suffix,
540+
static_root_path,
536541
};
537542

538543
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -1080,6 +1085,7 @@ themePicker.onblur = handleThemeButtonsBlur;
10801085
title: "Index of crates",
10811086
css_class: "mod",
10821087
root_path: "./",
1088+
static_root_path: cx.shared.static_root_path.deref(),
10831089
description: "List of crates",
10841090
keywords: BASIC_KEYWORDS,
10851091
resource_suffix: &cx.shared.resource_suffix,
@@ -1366,6 +1372,7 @@ impl<'a> SourceCollector<'a> {
13661372
title: &title,
13671373
css_class: "source",
13681374
root_path: &root_path,
1375+
static_root_path: self.scx.static_root_path.deref(),
13691376
description: &desc,
13701377
keywords: BASIC_KEYWORDS,
13711378
resource_suffix: &self.scx.resource_suffix,
@@ -1956,6 +1963,7 @@ impl Context {
19561963
title: "List of all items in this crate",
19571964
css_class: "mod",
19581965
root_path: "../",
1966+
static_root_path: self.shared.static_root_path.deref(),
19591967
description: "List of all items in this crate",
19601968
keywords: BASIC_KEYWORDS,
19611969
resource_suffix: &self.shared.resource_suffix,
@@ -2035,6 +2043,7 @@ impl Context {
20352043
let page = layout::Page {
20362044
css_class: tyname,
20372045
root_path: &self.root_path(),
2046+
static_root_path: self.shared.static_root_path.deref(),
20382047
title: &title,
20392048
description: &desc,
20402049
keywords: &keywords,

src/librustdoc/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(crate_visibility_modifier)]
2626
#![feature(const_fn)]
2727
#![feature(drain_filter)]
28+
#![feature(inner_deref)]
2829

2930
#![recursion_limit="256"]
3031

@@ -338,6 +339,13 @@ fn opts() -> Vec<RustcOptGroup> {
338339
"enable-index-page",
339340
"To enable generation of the index page")
340341
}),
342+
unstable("static-root-path", |o| {
343+
o.optopt("",
344+
"static-root-path",
345+
"Path string to force loading static files from in output pages. \
346+
If not set, uses combinations of '../' to reach the documentation root.",
347+
"PATH")
348+
}),
341349
]
342350
}
343351

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:-Z unstable-options --static-root-path /cache/
12+
13+
// @has static_root_path/struct.SomeStruct.html
14+
// @matches - '"/cache/main\.js"'
15+
// @!matches - '"\.\./main\.js"'
16+
// @matches - '"\.\./search-index\.js"'
17+
// @!matches - '"/cache/search-index\.js"'
18+
pub struct SomeStruct;

0 commit comments

Comments
 (0)