Add rustdoc settings menu

This commit is contained in:
Guillaume Gomez
2018-04-13 22:54:09 +02:00
parent 9379bcdd0b
commit f2ad3c3f6c
9 changed files with 238 additions and 26 deletions

View File

@@ -742,6 +742,8 @@ fn write_shared(cx: &Context,
write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
include_bytes!("static/rustdoc.css"))?;
write(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
include_bytes!("static/settings.css"))?;
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
// then we'll run over the "official" styles.
@@ -761,6 +763,8 @@ fn write_shared(cx: &Context,
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
include_bytes!("static/brush.svg"))?;
write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
include_bytes!("static/wheel.svg"))?;
write(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
include_bytes!("static/themes/light.css"))?;
themes.insert("light".to_owned());
@@ -794,8 +798,7 @@ themePicker.onclick = function() {{
switchTheme(currentTheme, mainTheme, item);
}};
themes.appendChild(but);
}});
"#,
}});"#,
themes.iter()
.map(|s| format!("\"{}\"", s))
.collect::<Vec<String>>()
@@ -804,6 +807,8 @@ themePicker.onclick = function() {{
write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
include_bytes!("static/main.js"))?;
write(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
include_bytes!("static/settings.js"))?;
{
let mut data = format!("var resourcesSuffix = \"{}\";\n",
@@ -1503,6 +1508,51 @@ impl fmt::Display for AllTypes {
}
}
#[derive(Debug)]
struct Settings<'a> {
// (id, explanation, default value)
settings: Vec<(&'static str, &'static str, bool)>,
root_path: &'a str,
suffix: &'a str,
}
impl<'a> Settings<'a> {
pub fn new(root_path: &'a str, suffix: &'a str) -> Settings<'a> {
Settings {
settings: vec![
("item-declarations", "Auto-hide item declarations.", true),
("item-attributes", "Auto-hide item attributes.", true),
],
root_path,
suffix,
}
}
}
impl<'a> fmt::Display for Settings<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"<h1 class='fqn'>\
<span class='in-band'>Rustdoc settings</span>\
</h1>\
<div class='settings'>{}</div>\
<script src='{}settings{}.js'></script>",
self.settings.iter()
.map(|(id, text, enabled)| {
format!("<div class='setting-line'>\
<label class='toggle'>\
<input type='checkbox' id='{}' {}>\
<span class='slider'></span>\
</label>\
<div>{}</div>\
</div>", id, if *enabled { " checked" } else { "" }, text)
})
.collect::<String>(),
self.root_path,
self.suffix)
}
}
impl Context {
/// String representation of how to get back to the root path of the 'doc/'
/// folder in terms of a relative URL.
@@ -1546,6 +1596,8 @@ impl Context {
};
let final_file = self.dst.join(&krate.name)
.join("all.html");
let settings_file = self.dst.join("settings.html");
let crate_name = krate.name.clone();
item.name = Some(krate.name);
@@ -1567,7 +1619,7 @@ impl Context {
if !root_path.ends_with('/') {
root_path.push('/');
}
let page = layout::Page {
let mut page = layout::Page {
title: "List of all items in this crate",
css_class: "mod",
root_path: "../",
@@ -1590,6 +1642,25 @@ impl Context {
self.shared.css_file_extension.is_some(),
&self.shared.themes),
&final_file);
// If the file already exists, no need to generate it again...
if !settings_file.is_file() {
let settings = Settings::new("./", &self.shared.resource_suffix);
page.title = "Rustdoc settings";
page.description = "Settings of Rustdoc";
page.root_path = "./";
let mut w = BufWriter::new(try_err!(File::create(&settings_file), &settings_file));
let mut themes = self.shared.themes.clone();
let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'>".to_owned();
themes.push(PathBuf::from("settings.css"));
try_err!(layout::render(&mut w, &self.shared.layout,
&page, &sidebar, &settings,
self.shared.css_file_extension.is_some(),
&themes),
&settings_file);
}
Ok(())
}