rustdoc: Provide a general --default-setting SETTING[=VALUE] option

We just plumb through what the user tells us.

This is flagged as unstable, mostly because I don't understand the
compatibility rules that rustdoc obeys for local storage data, and how
error handling of invalid data works.

We collect() the needed HashMap from Vec of Vecs of (key, value)
pairs, so that there is a nice place to add new more-specific options.
It would have been possible to use Extend::extend but doing it this
way ensures that all the used inputs are (and will stay) right next to
each other.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
This commit is contained in:
Ian Jackson 2020-10-13 18:52:43 +01:00
parent 5cd96d638c
commit d8a4497561
3 changed files with 26 additions and 2 deletions

View File

@ -377,6 +377,20 @@ impl Options {
}
};
let mut default_settings: Vec<Vec<(String, String)>> = vec![
matches
.opt_strs("default-setting")
.iter()
.map(|s| {
let mut kv = s.splitn(2, '=');
let k = kv.next().unwrap().to_string();
let v = kv.next().unwrap_or("true").to_string();
(k, v)
})
.collect(),
];
let default_settings = default_settings.drain(..).flatten().collect();
let test_args = matches.opt_strs("test-args");
let test_args: Vec<String> =
test_args.iter().flat_map(|s| s.split_whitespace()).map(|s| s.to_string()).collect();
@ -599,7 +613,7 @@ impl Options {
themes,
extension_css,
extern_html_root_urls,
default_settings: Default::default(),
default_settings,
resource_suffix,
enable_minification,
enable_index_page,

View File

@ -178,7 +178,7 @@ pub fn render<T: Print, S: Print>(
default_settings = layout
.default_settings
.iter()
.map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-',"_"), Escape(v),))
.map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v),))
.collect::<String>(),
style_files = style_files
.iter()

View File

@ -269,6 +269,16 @@ fn opts() -> Vec<RustcOptGroup> {
"sort modules by where they appear in the program, rather than alphabetically",
)
}),
unstable("default-setting", |o| {
o.optmulti(
"",
"default-setting",
"Default value for a rustdoc setting (used when \"rustdoc-SETTING\" is absent \
from web browser Local Storage). If VALUE is not supplied, \"true\" is used. \
Supported SETTINGs and VALUEs are not documented and not stable.",
"SETTING[=VALUE]",
)
}),
stable("theme", |o| {
o.optmulti(
"",