From b26d164a4c3f6bea683c5c2d67fd716123beac02 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 14 Apr 2019 23:19:54 +0200 Subject: [PATCH] Add CLI option to setup favicon --- src/librustdoc/config.rs | 11 +++++++++++ src/librustdoc/html/layout.rs | 7 ++++++- src/librustdoc/html/render.rs | 22 +++++++++++++++++++--- src/librustdoc/lib.rs | 6 ++++++ src/test/rustdoc/favicon-path.rs | 6 ++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/test/rustdoc/favicon-path.rs diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index f2682e00430d0..b869d470f487d 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -198,6 +198,8 @@ pub struct RenderOptions { pub generate_search_filter: bool, /// Option (disabled by default) to generate files used by RLS and some other tools. pub generate_redirect_pages: bool, + /// An optional path to to the favicon file to use. + pub favicon: Option, } impl Options { @@ -429,6 +431,14 @@ impl Options { } }); + let favicon = matches.opt_str("favicon-path").map(|s| PathBuf::from(&s)); + if let Some(ref favicon) = favicon { + if !favicon.is_file() { + diag.struct_err("option `--favicon-path` argument must be a file").emit(); + return Err(1); + } + } + let show_coverage = matches.opt_present("show-coverage"); let document_private = matches.opt_present("document-private-items"); @@ -508,6 +518,7 @@ impl Options { markdown_playground_url, generate_search_filter, generate_redirect_pages, + favicon, } }) } diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index acf019fd2254d..cc290a0260d6e 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -9,6 +9,7 @@ use crate::html::render::SlashChecker; pub struct Layout { pub logo: String, pub favicon: String, + pub favicon_path: Option, pub external_html: ExternalHtml, pub krate: String, } @@ -197,7 +198,11 @@ pub fn render( title = page.title, description = page.description, keywords = page.keywords, - favicon = if layout.favicon.is_empty() { + favicon = if layout.favicon_path.is_some() { + format!(r#""#, + static_root_path=static_root_path, + path=layout.favicon) + } else if layout.favicon.is_empty() { format!(r#""#, static_root_path=static_root_path, suffix=page.resource_suffix) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 3ee131d8f5c8c..effc08f1a696f 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -527,6 +527,7 @@ pub fn run(mut krate: clean::Crate, static_root_path, generate_search_filter, generate_redirect_pages, + favicon, .. } = options; @@ -545,7 +546,10 @@ pub fn run(mut krate: clean::Crate, issue_tracker_base_url: None, layout: layout::Layout { logo: String::new(), - favicon: String::new(), + favicon: favicon.as_ref() + .map(|_| format!("favicon-{}.ico", krate.name)) + .unwrap_or_else(String::new), + favicon_path: favicon, external_html, krate: krate.name.clone(), }, @@ -572,7 +576,16 @@ pub fn run(mut krate: clean::Crate, for attr in attrs.lists("doc") { match (attr.name_or_empty().get(), attr.value_str()) { ("html_favicon_url", Some(s)) => { - scx.layout.favicon = s.to_string(); + let s = s.to_string(); + if scx.layout.favicon_path.is_some() { + if !s.is_empty() { + diag.struct_warn("`favicon-path` option has been passed, ignoring \ + `html_favicon_url` attribute") + .emit(); + } + } else { + scx.layout.favicon = s; + } } ("html_logo_url", Some(s)) => { scx.layout.logo = s.to_string(); @@ -816,7 +829,10 @@ fn write_shared( write(cx.dst.join(&format!("rust-logo{}.png", cx.shared.resource_suffix)), static_files::RUST_LOGO)?; } - if (*cx.shared).layout.favicon.is_empty() { + if let Some(ref favicon) = (*cx.shared).layout.favicon_path { + let content = try_err!(fs::read(&favicon), &favicon); + write(cx.dst.join(&(*cx.shared).layout.favicon), &content)?; + } else if (*cx.shared).layout.favicon.is_empty() { write(cx.dst.join(&format!("favicon{}.ico", cx.shared.resource_suffix)), static_files::RUST_FAVICON)?; } diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 6cb937d9216ac..30d8d15f96c7d 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -356,6 +356,12 @@ fn opts() -> Vec { "show-coverage", "calculate percentage of public items with documentation") }), + unstable("favicon-path", |o| { + o.optopt("", + "favicon-path", + "Path string to the favicon file to be used for this crate", + "PATH") + }), ] } diff --git a/src/test/rustdoc/favicon-path.rs b/src/test/rustdoc/favicon-path.rs new file mode 100644 index 0000000000000..bd9b10cd51969 --- /dev/null +++ b/src/test/rustdoc/favicon-path.rs @@ -0,0 +1,6 @@ +// compile-flags:-Z unstable-options --favicon-path ./src/librustdoc/html/static/favicon.ico + +#![crate_name = "foo"] + +// @has foo/fn.foo.html '//link/@href' '../favicon-foo.ico' +pub fn foo() {}