Settings to function

This commit is contained in:
Mark Rousskov
2019-08-31 12:27:27 -04:00
parent bb40d5fa49
commit 17bef30d0b

View File

@@ -1801,42 +1801,25 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
} }
} }
#[derive(Debug)] fn settings(root_path: &str, suffix: &str) -> String {
struct Settings<'a> {
// (id, explanation, default value) // (id, explanation, default value)
settings: Vec<(&'static str, &'static str, bool)>, let settings = [
root_path: &'a str, ("item-declarations", "Auto-hide item declarations.", true),
suffix: &'a str, ("item-attributes", "Auto-hide item attributes.", true),
} ("trait-implementations", "Auto-hide trait implementations documentation",
true),
impl<'a> Settings<'a> { ("method-docs", "Auto-hide item methods' documentation", false),
pub fn new(root_path: &'a str, suffix: &'a str) -> Settings<'a> { ("go-to-only-result", "Directly go to item in search if there is only one result",
Settings { false),
settings: vec![ ("line-numbers", "Show line numbers on code examples", false),
("item-declarations", "Auto-hide item declarations.", true), ];
("item-attributes", "Auto-hide item attributes.", true), format!(
("trait-implementations", "Auto-hide trait implementations documentation",
true),
("method-docs", "Auto-hide item methods' documentation", false),
("go-to-only-result", "Directly go to item in search if there is only one result",
false),
("line-numbers", "Show line numbers on code examples", false),
],
root_path,
suffix,
}
}
}
impl<'a> fmt::Display for Settings<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"<h1 class='fqn'>\ "<h1 class='fqn'>\
<span class='in-band'>Rustdoc settings</span>\ <span class='in-band'>Rustdoc settings</span>\
</h1>\ </h1>\
<div class='settings'>{}</div>\ <div class='settings'>{}</div>\
<script src='{}settings{}.js'></script>", <script src='{}settings{}.js'></script>",
self.settings.iter() settings.iter()
.map(|(id, text, enabled)| { .map(|(id, text, enabled)| {
format!("<div class='setting-line'>\ format!("<div class='setting-line'>\
<label class='toggle'>\ <label class='toggle'>\
@@ -1847,9 +1830,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
</div>", id, if *enabled { " checked" } else { "" }, text) </div>", id, if *enabled { " checked" } else { "" }, text)
}) })
.collect::<String>(), .collect::<String>(),
self.root_path, root_path,
self.suffix) suffix)
}
} }
impl Context { impl Context {
@@ -1924,8 +1906,6 @@ impl Context {
self.shared.fs.write(&final_file, v.as_bytes())?; self.shared.fs.write(&final_file, v.as_bytes())?;
// Generating settings page. // Generating settings page.
let settings = Settings::new(self.shared.static_root_path.as_deref().unwrap_or("./"),
&self.shared.resource_suffix);
page.title = "Rustdoc settings"; page.title = "Rustdoc settings";
page.description = "Settings of Rustdoc"; page.description = "Settings of Rustdoc";
page.root_path = "./"; page.root_path = "./";
@@ -1935,7 +1915,10 @@ impl Context {
themes.push(PathBuf::from("settings.css")); themes.push(PathBuf::from("settings.css"));
let v = layout::render( let v = layout::render(
&self.shared.layout, &self.shared.layout,
&page, sidebar, |buf: &mut Buffer| buf.from_display(settings), &page, sidebar, settings(
self.shared.static_root_path.as_deref().unwrap_or("./"),
&self.shared.resource_suffix
),
&themes); &themes);
self.shared.fs.write(&settings_file, v.as_bytes())?; self.shared.fs.write(&settings_file, v.as_bytes())?;