]> git.lizzy.rs Git - rust.git/commit
rustdoc: Provide a way to set the default settings from Rust code
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 23 Sep 2020 21:44:54 +0000 (22:44 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 28 Oct 2020 17:54:06 +0000 (17:54 +0000)
commit5cd96d638c37dc7f92cb8b2fc84a3f7bfe7b7960
tree63e1a506626c7754d0a9f15b7bcb128f86c05d3e
parent2e10475fdd764365255596e4cb0c21b521456823
rustdoc: Provide a way to set the default settings from Rust code

rustdoc has various user-configurable preferences.  These are recorded
in web Local Storage (where available).  But we want to provide a way
to configure the default default, including for when web storage is
not available.

getSettingValue is the function responsible for looking up these
settings.  Here we make it fall back some in-DOM data, which
ultimately comes from RenderOptions.default_settings.

Using HTML data atrtributes is fairly convenient here, dsspite the
need to transform between snake and kebab case to avoid the DOM
converting kebab case to camel case (!)

We cache the element and dataset lookup in a global variable, to
ensure that getSettingValue remains fast.

The DOM representation has to be in an element which precedes the
inclusion of storage.js.  That means it has to be in the <head> and we
should not use an empty <div> as the container (although most browsers
will accept that).  An empty <script> element provides a convenient
and harmless container object.  <meta> would be another possibility
but runs a greater risk of having unwanted behaviours on weird
browsers.

We trust the RenderOptions not to contain unhelpful setting names,
which don't fit nicely into an HTML attribute.  It's awkward to quote
dataset keys.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/librustdoc/config.rs
src/librustdoc/html/layout.rs
src/librustdoc/html/markdown.rs
src/librustdoc/html/render/mod.rs
src/librustdoc/html/static/storage.js