Add resource-suffix option for rustdoc
This commit is contained in:
parent
3eeb5a665e
commit
831009f035
4 changed files with 64 additions and 27 deletions
|
@ -28,6 +28,7 @@ pub struct Page<'a> {
|
||||||
pub root_path: &'a str,
|
pub root_path: &'a str,
|
||||||
pub description: &'a str,
|
pub description: &'a str,
|
||||||
pub keywords: &'a str,
|
pub keywords: &'a str,
|
||||||
|
pub resource_suffix: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render<T: fmt::Display, S: fmt::Display>(
|
pub fn render<T: fmt::Display, S: fmt::Display>(
|
||||||
|
@ -47,12 +48,13 @@ r##"<!DOCTYPE html>
|
||||||
|
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
|
|
||||||
<link rel="stylesheet" type="text/css" href="{root_path}normalize.css">
|
<link rel="stylesheet" type="text/css" href="{root_path}normalize{suffix}.css">
|
||||||
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css" id="mainThemeStyle">
|
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc{suffix}.css"
|
||||||
|
id="mainThemeStyle">
|
||||||
{themes}
|
{themes}
|
||||||
<link rel="stylesheet" type="text/css" href="{root_path}dark.css">
|
<link rel="stylesheet" type="text/css" href="{root_path}dark{suffix}.css">
|
||||||
<link rel="stylesheet" type="text/css" href="{root_path}main.css" id="themeStyle">
|
<link rel="stylesheet" type="text/css" href="{root_path}main{suffix}.css" id="themeStyle">
|
||||||
<script src="{root_path}storage.js"></script>
|
<script src="{root_path}storage{suffix}.js"></script>
|
||||||
{css_extension}
|
{css_extension}
|
||||||
|
|
||||||
{favicon}
|
{favicon}
|
||||||
|
@ -76,11 +78,11 @@ r##"<!DOCTYPE html>
|
||||||
|
|
||||||
<div class="theme-picker">
|
<div class="theme-picker">
|
||||||
<button id="theme-picker" aria-label="Pick another theme!">
|
<button id="theme-picker" aria-label="Pick another theme!">
|
||||||
<img src="{root_path}brush.svg" width="18" alt="Pick another theme!">
|
<img src="{root_path}brush{suffix}.svg" width="18" alt="Pick another theme!">
|
||||||
</button>
|
</button>
|
||||||
<div id="theme-choices"></div>
|
<div id="theme-choices"></div>
|
||||||
</div>
|
</div>
|
||||||
<script src="{root_path}theme.js"></script>
|
<script src="{root_path}theme{suffix}.js"></script>
|
||||||
<nav class="sub">
|
<nav class="sub">
|
||||||
<form class="search-form js-only">
|
<form class="search-form js-only">
|
||||||
<div class="search-container">
|
<div class="search-container">
|
||||||
|
@ -153,13 +155,14 @@ r##"<!DOCTYPE html>
|
||||||
window.rootPath = "{root_path}";
|
window.rootPath = "{root_path}";
|
||||||
window.currentCrate = "{krate}";
|
window.currentCrate = "{krate}";
|
||||||
</script>
|
</script>
|
||||||
<script src="{root_path}main.js"></script>
|
<script src="{root_path}main{suffix}.js"></script>
|
||||||
<script defer src="{root_path}search-index.js"></script>
|
<script defer src="{root_path}search-index.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>"##,
|
</html>"##,
|
||||||
css_extension = if css_file_extension {
|
css_extension = if css_file_extension {
|
||||||
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
|
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme{suffix}.css\">",
|
||||||
root_path = page.root_path)
|
root_path = page.root_path,
|
||||||
|
suffix=page.resource_suffix)
|
||||||
} else {
|
} else {
|
||||||
"".to_owned()
|
"".to_owned()
|
||||||
},
|
},
|
||||||
|
@ -191,8 +194,10 @@ r##"<!DOCTYPE html>
|
||||||
.filter_map(|t| t.file_stem())
|
.filter_map(|t| t.file_stem())
|
||||||
.filter_map(|t| t.to_str())
|
.filter_map(|t| t.to_str())
|
||||||
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#,
|
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#,
|
||||||
page.root_path, t))
|
page.root_path,
|
||||||
|
t.replace(".css", &format!("{}.css", page.resource_suffix))))
|
||||||
.collect::<String>(),
|
.collect::<String>(),
|
||||||
|
suffix=page.resource_suffix,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
//! for creating the corresponding search index and source file renderings.
|
//! for creating the corresponding search index and source file renderings.
|
||||||
//! These threads are not parallelized (they haven't been a bottleneck yet), and
|
//! These threads are not parallelized (they haven't been a bottleneck yet), and
|
||||||
//! both occur before the crate is rendered.
|
//! both occur before the crate is rendered.
|
||||||
|
|
||||||
pub use self::ExternalLocation::*;
|
pub use self::ExternalLocation::*;
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
@ -128,6 +129,9 @@ pub struct SharedContext {
|
||||||
pub sort_modules_alphabetically: bool,
|
pub sort_modules_alphabetically: bool,
|
||||||
/// Additional themes to be added to the generated docs.
|
/// Additional themes to be added to the generated docs.
|
||||||
pub themes: Vec<PathBuf>,
|
pub themes: Vec<PathBuf>,
|
||||||
|
/// Suffix to be added on resource files (if suffix is "-v2" then "main.css" becomes
|
||||||
|
/// "main-v2.css").
|
||||||
|
pub resource_suffix: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SharedContext {
|
impl SharedContext {
|
||||||
|
@ -492,6 +496,7 @@ pub fn run(mut krate: clean::Crate,
|
||||||
external_html: &ExternalHtml,
|
external_html: &ExternalHtml,
|
||||||
playground_url: Option<String>,
|
playground_url: Option<String>,
|
||||||
dst: PathBuf,
|
dst: PathBuf,
|
||||||
|
resource_suffix: String,
|
||||||
passes: FxHashSet<String>,
|
passes: FxHashSet<String>,
|
||||||
css_file_extension: Option<PathBuf>,
|
css_file_extension: Option<PathBuf>,
|
||||||
renderinfo: RenderInfo,
|
renderinfo: RenderInfo,
|
||||||
|
@ -520,6 +525,7 @@ pub fn run(mut krate: clean::Crate,
|
||||||
created_dirs: RefCell::new(FxHashSet()),
|
created_dirs: RefCell::new(FxHashSet()),
|
||||||
sort_modules_alphabetically,
|
sort_modules_alphabetically,
|
||||||
themes,
|
themes,
|
||||||
|
resource_suffix,
|
||||||
};
|
};
|
||||||
|
|
||||||
// If user passed in `--playground-url` arg, we fill in crate name here
|
// If user passed in `--playground-url` arg, we fill in crate name here
|
||||||
|
@ -734,7 +740,7 @@ fn write_shared(cx: &Context,
|
||||||
// Add all the static files. These may already exist, but we just
|
// Add all the static files. These may already exist, but we just
|
||||||
// overwrite them anyway to make sure that they're fresh and up-to-date.
|
// overwrite them anyway to make sure that they're fresh and up-to-date.
|
||||||
|
|
||||||
write(cx.dst.join("rustdoc.css"),
|
write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/rustdoc.css"))?;
|
include_bytes!("static/rustdoc.css"))?;
|
||||||
|
|
||||||
// To avoid "main.css" to be overwritten, we'll first run over the received themes and only
|
// To avoid "main.css" to be overwritten, we'll first run over the received themes and only
|
||||||
|
@ -746,16 +752,19 @@ fn write_shared(cx: &Context,
|
||||||
|
|
||||||
let mut f = try_err!(File::open(&entry), &entry);
|
let mut f = try_err!(File::open(&entry), &entry);
|
||||||
try_err!(f.read_to_end(&mut content), &entry);
|
try_err!(f.read_to_end(&mut content), &entry);
|
||||||
write(cx.dst.join(try_none!(entry.file_name(), &entry)), content.as_slice())?;
|
let theme = try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry);
|
||||||
themes.insert(try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry).to_owned());
|
let extension = try_none!(try_none!(entry.extension(), &entry).to_str(), &entry);
|
||||||
|
write(cx.dst.join(format!("{}{}.{}", theme, cx.shared.resource_suffix, extension)),
|
||||||
|
content.as_slice())?;
|
||||||
|
themes.insert(theme.to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
write(cx.dst.join("brush.svg"),
|
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/brush.svg"))?;
|
include_bytes!("static/brush.svg"))?;
|
||||||
write(cx.dst.join("main.css"),
|
write(cx.dst.join(&format!("main{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/themes/main.css"))?;
|
include_bytes!("static/themes/main.css"))?;
|
||||||
themes.insert("main".to_owned());
|
themes.insert("main".to_owned());
|
||||||
write(cx.dst.join("dark.css"),
|
write(cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/themes/dark.css"))?;
|
include_bytes!("static/themes/dark.css"))?;
|
||||||
themes.insert("dark".to_owned());
|
themes.insert("dark".to_owned());
|
||||||
|
|
||||||
|
@ -763,7 +772,8 @@ fn write_shared(cx: &Context,
|
||||||
themes.sort();
|
themes.sort();
|
||||||
// To avoid theme switch latencies as much as possible, we put everything theme related
|
// To avoid theme switch latencies as much as possible, we put everything theme related
|
||||||
// at the beginning of the html files into another js file.
|
// at the beginning of the html files into another js file.
|
||||||
write(cx.dst.join("theme.js"), format!(
|
write(cx.dst.join(&format!("theme{}.js", cx.shared.resource_suffix)),
|
||||||
|
format!(
|
||||||
r#"var themes = document.getElementById("theme-choices");
|
r#"var themes = document.getElementById("theme-choices");
|
||||||
var themePicker = document.getElementById("theme-picker");
|
var themePicker = document.getElementById("theme-picker");
|
||||||
themePicker.onclick = function() {{
|
themePicker.onclick = function() {{
|
||||||
|
@ -785,19 +795,28 @@ themePicker.onclick = function() {{
|
||||||
}};
|
}};
|
||||||
themes.appendChild(but);
|
themes.appendChild(but);
|
||||||
}});
|
}});
|
||||||
"#, themes.iter()
|
"#,
|
||||||
|
themes.iter()
|
||||||
.map(|s| format!("\"{}\"", s))
|
.map(|s| format!("\"{}\"", s))
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(",")).as_bytes())?;
|
.join(",")).as_bytes(),
|
||||||
|
)?;
|
||||||
|
|
||||||
write(cx.dst.join("main.js"), include_bytes!("static/main.js"))?;
|
write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
|
||||||
write(cx.dst.join("storage.js"), include_bytes!("static/storage.js"))?;
|
include_bytes!("static/main.js"))?;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut data = format!("var resourcesSuffix = \"{}\";\n",
|
||||||
|
cx.shared.resource_suffix).into_bytes();
|
||||||
|
data.extend_from_slice(include_bytes!("static/storage.js"));
|
||||||
|
write(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)), &data)?;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(ref css) = cx.shared.css_file_extension {
|
if let Some(ref css) = cx.shared.css_file_extension {
|
||||||
let out = cx.dst.join("theme.css");
|
let out = cx.dst.join(&format!("theme{}.css", cx.shared.resource_suffix));
|
||||||
try_err!(fs::copy(css, out), css);
|
try_err!(fs::copy(css, out), css);
|
||||||
}
|
}
|
||||||
write(cx.dst.join("normalize.css"),
|
write(cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/normalize.css"))?;
|
include_bytes!("static/normalize.css"))?;
|
||||||
write(cx.dst.join("FiraSans-Regular.woff"),
|
write(cx.dst.join("FiraSans-Regular.woff"),
|
||||||
include_bytes!("static/FiraSans-Regular.woff"))?;
|
include_bytes!("static/FiraSans-Regular.woff"))?;
|
||||||
|
@ -1084,6 +1103,7 @@ impl<'a> SourceCollector<'a> {
|
||||||
root_path: &root_path,
|
root_path: &root_path,
|
||||||
description: &desc,
|
description: &desc,
|
||||||
keywords: BASIC_KEYWORDS,
|
keywords: BASIC_KEYWORDS,
|
||||||
|
resource_suffix: &self.scx.resource_suffix,
|
||||||
};
|
};
|
||||||
layout::render(&mut w, &self.scx.layout,
|
layout::render(&mut w, &self.scx.layout,
|
||||||
&page, &(""), &Source(contents),
|
&page, &(""), &Source(contents),
|
||||||
|
@ -1446,6 +1466,7 @@ impl Context {
|
||||||
title: &title,
|
title: &title,
|
||||||
description: &desc,
|
description: &desc,
|
||||||
keywords: &keywords,
|
keywords: &keywords,
|
||||||
|
resource_suffix: &self.shared.resource_suffix,
|
||||||
};
|
};
|
||||||
|
|
||||||
reset_ids(true);
|
reset_ids(true);
|
||||||
|
|
|
@ -41,7 +41,9 @@ function getCurrentValue(name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchTheme(styleElem, mainStyleElem, newTheme) {
|
function switchTheme(styleElem, mainStyleElem, newTheme) {
|
||||||
var newHref = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
|
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
|
||||||
|
var fullNewTheme = newTheme + resourcesSuffix + ".css";
|
||||||
|
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
|
||||||
var found = false;
|
var found = false;
|
||||||
|
|
||||||
if (savedHref.length === 0) {
|
if (savedHref.length === 0) {
|
||||||
|
|
|
@ -261,6 +261,13 @@ pub fn opts() -> Vec<RustcOptGroup> {
|
||||||
"check if given theme is valid",
|
"check if given theme is valid",
|
||||||
"FILES")
|
"FILES")
|
||||||
}),
|
}),
|
||||||
|
unstable("resource-suffix", |o| {
|
||||||
|
o.optopt("",
|
||||||
|
"resource-suffix",
|
||||||
|
"suffix to add to CSS and JavaScript files, e.g. \"main.css\" will become \
|
||||||
|
\"main-suffix.css\"",
|
||||||
|
"PATH")
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -417,6 +424,7 @@ pub fn main_args(args: &[String]) -> isize {
|
||||||
let display_warnings = matches.opt_present("display-warnings");
|
let display_warnings = matches.opt_present("display-warnings");
|
||||||
let linker = matches.opt_str("linker").map(PathBuf::from);
|
let linker = matches.opt_str("linker").map(PathBuf::from);
|
||||||
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
|
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
|
||||||
|
let resource_suffix = matches.opt_str("resource-suffix");
|
||||||
|
|
||||||
match (should_test, markdown_input) {
|
match (should_test, markdown_input) {
|
||||||
(true, true) => {
|
(true, true) => {
|
||||||
|
@ -442,6 +450,7 @@ pub fn main_args(args: &[String]) -> isize {
|
||||||
Some("html") | None => {
|
Some("html") | None => {
|
||||||
html::render::run(krate, &external_html, playground_url,
|
html::render::run(krate, &external_html, playground_url,
|
||||||
output.unwrap_or(PathBuf::from("doc")),
|
output.unwrap_or(PathBuf::from("doc")),
|
||||||
|
resource_suffix.unwrap_or(String::new()),
|
||||||
passes.into_iter().collect(),
|
passes.into_iter().collect(),
|
||||||
css_file_extension,
|
css_file_extension,
|
||||||
renderinfo,
|
renderinfo,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue