Skip to content

Commit afb617a

Browse files
Rollup merge of rust-lang#48511 - GuillaumeGomez:rustdoc-resource-suffix, r=QuietMisdreavus
Add resource-suffix option for rustdoc Alternative version of rust-lang#48442. cc @onur r? @QuietMisdreavus
2 parents 75bc23d + 831009f commit afb617a

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

src/librustdoc/html/layout.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Page<'a> {
2828
pub root_path: &'a str,
2929
pub description: &'a str,
3030
pub keywords: &'a str,
31+
pub resource_suffix: &'a str,
3132
}
3233

3334
pub fn render<T: fmt::Display, S: fmt::Display>(
@@ -47,12 +48,13 @@ r##"<!DOCTYPE html>
4748
4849
<title>{title}</title>
4950
50-
<link rel="stylesheet" type="text/css" href="{root_path}normalize.css">
51-
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css" id="mainThemeStyle">
51+
<link rel="stylesheet" type="text/css" href="{root_path}normalize{suffix}.css">
52+
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc{suffix}.css"
53+
id="mainThemeStyle">
5254
{themes}
53-
<link rel="stylesheet" type="text/css" href="{root_path}dark.css">
54-
<link rel="stylesheet" type="text/css" href="{root_path}main.css" id="themeStyle">
55-
<script src="{root_path}storage.js"></script>
55+
<link rel="stylesheet" type="text/css" href="{root_path}dark{suffix}.css">
56+
<link rel="stylesheet" type="text/css" href="{root_path}main{suffix}.css" id="themeStyle">
57+
<script src="{root_path}storage{suffix}.js"></script>
5658
{css_extension}
5759
5860
{favicon}
@@ -76,11 +78,11 @@ r##"<!DOCTYPE html>
7678
7779
<div class="theme-picker">
7880
<button id="theme-picker" aria-label="Pick another theme!">
79-
<img src="{root_path}brush.svg" width="18" alt="Pick another theme!">
81+
<img src="{root_path}brush{suffix}.svg" width="18" alt="Pick another theme!">
8082
</button>
8183
<div id="theme-choices"></div>
8284
</div>
83-
<script src="{root_path}theme.js"></script>
85+
<script src="{root_path}theme{suffix}.js"></script>
8486
<nav class="sub">
8587
<form class="search-form js-only">
8688
<div class="search-container">
@@ -153,13 +155,14 @@ r##"<!DOCTYPE html>
153155
window.rootPath = "{root_path}";
154156
window.currentCrate = "{krate}";
155157
</script>
156-
<script src="{root_path}main.js"></script>
158+
<script src="{root_path}main{suffix}.js"></script>
157159
<script defer src="{root_path}search-index.js"></script>
158160
</body>
159161
</html>"##,
160162
css_extension = if css_file_extension {
161-
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
162-
root_path = page.root_path)
163+
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme{suffix}.css\">",
164+
root_path = page.root_path,
165+
suffix=page.resource_suffix)
163166
} else {
164167
"".to_owned()
165168
},
@@ -191,8 +194,10 @@ r##"<!DOCTYPE html>
191194
.filter_map(|t| t.file_stem())
192195
.filter_map(|t| t.to_str())
193196
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#,
194-
page.root_path, t))
197+
page.root_path,
198+
t.replace(".css", &format!("{}.css", page.resource_suffix))))
195199
.collect::<String>(),
200+
suffix=page.resource_suffix,
196201
)
197202
}
198203

src/librustdoc/html/render.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
//! for creating the corresponding search index and source file renderings.
3333
//! These threads are not parallelized (they haven't been a bottleneck yet), and
3434
//! both occur before the crate is rendered.
35+
3536
pub use self::ExternalLocation::*;
3637

3738
use std::borrow::Cow;
@@ -128,6 +129,9 @@ pub struct SharedContext {
128129
pub sort_modules_alphabetically: bool,
129130
/// Additional themes to be added to the generated docs.
130131
pub themes: Vec<PathBuf>,
132+
/// Suffix to be added on resource files (if suffix is "-v2" then "main.css" becomes
133+
/// "main-v2.css").
134+
pub resource_suffix: String,
131135
}
132136

133137
impl SharedContext {
@@ -492,6 +496,7 @@ pub fn run(mut krate: clean::Crate,
492496
external_html: &ExternalHtml,
493497
playground_url: Option<String>,
494498
dst: PathBuf,
499+
resource_suffix: String,
495500
passes: FxHashSet<String>,
496501
css_file_extension: Option<PathBuf>,
497502
renderinfo: RenderInfo,
@@ -520,6 +525,7 @@ pub fn run(mut krate: clean::Crate,
520525
created_dirs: RefCell::new(FxHashSet()),
521526
sort_modules_alphabetically,
522527
themes,
528+
resource_suffix,
523529
};
524530

525531
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -734,7 +740,7 @@ fn write_shared(cx: &Context,
734740
// Add all the static files. These may already exist, but we just
735741
// overwrite them anyway to make sure that they're fresh and up-to-date.
736742

737-
write(cx.dst.join("rustdoc.css"),
743+
write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
738744
include_bytes!("static/rustdoc.css"))?;
739745

740746
// To avoid "main.css" to be overwritten, we'll first run over the received themes and only
@@ -746,24 +752,28 @@ fn write_shared(cx: &Context,
746752

747753
let mut f = try_err!(File::open(&entry), &entry);
748754
try_err!(f.read_to_end(&mut content), &entry);
749-
write(cx.dst.join(try_none!(entry.file_name(), &entry)), content.as_slice())?;
750-
themes.insert(try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry).to_owned());
755+
let theme = try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry);
756+
let extension = try_none!(try_none!(entry.extension(), &entry).to_str(), &entry);
757+
write(cx.dst.join(format!("{}{}.{}", theme, cx.shared.resource_suffix, extension)),
758+
content.as_slice())?;
759+
themes.insert(theme.to_owned());
751760
}
752761

753-
write(cx.dst.join("brush.svg"),
762+
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
754763
include_bytes!("static/brush.svg"))?;
755-
write(cx.dst.join("main.css"),
764+
write(cx.dst.join(&format!("main{}.css", cx.shared.resource_suffix)),
756765
include_bytes!("static/themes/main.css"))?;
757766
themes.insert("main".to_owned());
758-
write(cx.dst.join("dark.css"),
767+
write(cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)),
759768
include_bytes!("static/themes/dark.css"))?;
760769
themes.insert("dark".to_owned());
761770

762771
let mut themes: Vec<&String> = themes.iter().collect();
763772
themes.sort();
764773
// To avoid theme switch latencies as much as possible, we put everything theme related
765774
// at the beginning of the html files into another js file.
766-
write(cx.dst.join("theme.js"), format!(
775+
write(cx.dst.join(&format!("theme{}.js", cx.shared.resource_suffix)),
776+
format!(
767777
r#"var themes = document.getElementById("theme-choices");
768778
var themePicker = document.getElementById("theme-picker");
769779
themePicker.onclick = function() {{
@@ -785,19 +795,28 @@ themePicker.onclick = function() {{
785795
}};
786796
themes.appendChild(but);
787797
}});
788-
"#, themes.iter()
789-
.map(|s| format!("\"{}\"", s))
790-
.collect::<Vec<String>>()
791-
.join(",")).as_bytes())?;
798+
"#,
799+
themes.iter()
800+
.map(|s| format!("\"{}\"", s))
801+
.collect::<Vec<String>>()
802+
.join(",")).as_bytes(),
803+
)?;
804+
805+
write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
806+
include_bytes!("static/main.js"))?;
792807

793-
write(cx.dst.join("main.js"), include_bytes!("static/main.js"))?;
794-
write(cx.dst.join("storage.js"), include_bytes!("static/storage.js"))?;
808+
{
809+
let mut data = format!("var resourcesSuffix = \"{}\";\n",
810+
cx.shared.resource_suffix).into_bytes();
811+
data.extend_from_slice(include_bytes!("static/storage.js"));
812+
write(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)), &data)?;
813+
}
795814

796815
if let Some(ref css) = cx.shared.css_file_extension {
797-
let out = cx.dst.join("theme.css");
816+
let out = cx.dst.join(&format!("theme{}.css", cx.shared.resource_suffix));
798817
try_err!(fs::copy(css, out), css);
799818
}
800-
write(cx.dst.join("normalize.css"),
819+
write(cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)),
801820
include_bytes!("static/normalize.css"))?;
802821
write(cx.dst.join("FiraSans-Regular.woff"),
803822
include_bytes!("static/FiraSans-Regular.woff"))?;
@@ -1084,6 +1103,7 @@ impl<'a> SourceCollector<'a> {
10841103
root_path: &root_path,
10851104
description: &desc,
10861105
keywords: BASIC_KEYWORDS,
1106+
resource_suffix: &self.scx.resource_suffix,
10871107
};
10881108
layout::render(&mut w, &self.scx.layout,
10891109
&page, &(""), &Source(contents),
@@ -1446,6 +1466,7 @@ impl Context {
14461466
title: &title,
14471467
description: &desc,
14481468
keywords: &keywords,
1469+
resource_suffix: &self.shared.resource_suffix,
14491470
};
14501471

14511472
reset_ids(true);

src/librustdoc/html/static/storage.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ function getCurrentValue(name) {
4141
}
4242

4343
function switchTheme(styleElem, mainStyleElem, newTheme) {
44-
var newHref = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
44+
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
45+
var fullNewTheme = newTheme + resourcesSuffix + ".css";
46+
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
4547
var found = false;
4648

4749
if (savedHref.length === 0) {

src/librustdoc/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ pub fn opts() -> Vec<RustcOptGroup> {
261261
"check if given theme is valid",
262262
"FILES")
263263
}),
264+
unstable("resource-suffix", |o| {
265+
o.optopt("",
266+
"resource-suffix",
267+
"suffix to add to CSS and JavaScript files, e.g. \"main.css\" will become \
268+
\"main-suffix.css\"",
269+
"PATH")
270+
}),
264271
]
265272
}
266273

@@ -417,6 +424,7 @@ pub fn main_args(args: &[String]) -> isize {
417424
let display_warnings = matches.opt_present("display-warnings");
418425
let linker = matches.opt_str("linker").map(PathBuf::from);
419426
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
427+
let resource_suffix = matches.opt_str("resource-suffix");
420428

421429
match (should_test, markdown_input) {
422430
(true, true) => {
@@ -442,6 +450,7 @@ pub fn main_args(args: &[String]) -> isize {
442450
Some("html") | None => {
443451
html::render::run(krate, &external_html, playground_url,
444452
output.unwrap_or(PathBuf::from("doc")),
453+
resource_suffix.unwrap_or(String::new()),
445454
passes.into_iter().collect(),
446455
css_file_extension,
447456
renderinfo,

0 commit comments

Comments
 (0)