Skip to content

Inline some SVG icons into rustdoc.css #91724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4602,6 +4602,7 @@ dependencies = [
"expect-test",
"itertools 0.9.0",
"minifier",
"percent-encoding 2.1.0",
"pulldown-cmark",
"rayon",
"regex",
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustdoc-json-types = { path = "../rustdoc-json-types" }
tracing = "0.1"
tracing-tree = "0.1.9"
tera = { version = "1.10.0", default-features = false }
percent-encoding = "2.1"

[dependencies.tracing-subscriber]
version = "0.2.13"
Expand Down
55 changes: 37 additions & 18 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,26 @@ pub(super) fn write_shared(
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
};

// Given "foo.svg", return e.g. "url(\"foo1.58.0.svg\")"
fn ver_url(cx: &Context<'_>, basename: &'static str) -> String {
format!(
"url(\"{}\")",
SharedResource::ToolchainSpecific { basename }
.path(cx)
.file_name()
.unwrap()
.to_str()
.unwrap()
)
}
// Given "foo.svg", either include it directly in the CSS as a data URL,
// or link to it, depending on the options.
let css_icon_url = |basename: &'static str, icon: &[u8]| -> String {
if basename.ends_with(".svg") && options.enable_minification {
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
const DATA_URI: &AsciiSet = &CONTROLS.add(b'\'').add(b'#').add(b'?');
let icn = utf8_percent_encode(std::str::from_utf8(icon).unwrap(), DATA_URI);
format!("url('data:image/svg+xml,{}')", icn,)
} else {
format!(
"url(\"{}\")",
SharedResource::ToolchainSpecific { basename }
.path(cx)
.file_name()
.unwrap()
.to_str()
.unwrap()
)
}
};

// We use the AUTOREPLACE mechanism to inject into our static JS and CSS certain
// values that are only known at doc build time. Since this mechanism is somewhat
Expand All @@ -202,10 +210,16 @@ pub(super) fn write_shared(
static_files::RUSTDOC_CSS
.replace(
"/* AUTOREPLACE: */url(\"toggle-minus.svg\")",
&ver_url(cx, "toggle-minus.svg"),
&css_icon_url("toggle-minus.svg", static_files::TOGGLE_MINUS_SVG),
)
.replace(
"/* AUTOREPLACE: */url(\"toggle-plus.svg\")",
&css_icon_url("toggle-plus.svg", static_files::TOGGLE_PLUS_SVG),
)
.replace("/* AUTOREPLACE: */url(\"toggle-plus.svg\")", &ver_url(cx, "toggle-plus.svg"))
.replace("/* AUTOREPLACE: */url(\"down-arrow.svg\")", &ver_url(cx, "down-arrow.svg")),
.replace(
"/* AUTOREPLACE: */url(\"down-arrow.svg\")",
&css_icon_url("down-arrow.svg", static_files::DOWN_ARROW_SVG),
),
cx,
options,
)?;
Expand Down Expand Up @@ -250,9 +264,14 @@ pub(super) fn write_shared(
write_toolchain("brush.svg", static_files::BRUSH_SVG)?;
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;

// The following icons are embeded in the CSS file,
// unless we're in tweak-the-theme-mode.
if !options.enable_minification {
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized but: could you merge the write_toolchain call with the css_icon_url closure call please? It would make it impossible to forget to update this part.

write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_SVG)?;
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_SVG)?;
}

let mut themes: Vec<&String> = themes.iter().collect();
themes.sort();
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/images/down-arrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/librustdoc/html/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ crate static CLIPBOARD_SVG: &[u8] = include_bytes!("static/images/clipboard.svg"
crate static DOWN_ARROW_SVG: &[u8] = include_bytes!("static/images/down-arrow.svg");

/// The file contents of `toggle-minus.svg`, the icon used for opened toggles.
crate static TOGGLE_MINUS_PNG: &[u8] = include_bytes!("static/images/toggle-minus.svg");
crate static TOGGLE_MINUS_SVG: &[u8] = include_bytes!("static/images/toggle-minus.svg");

/// The file contents of `toggle-plus.svg`, the icon used for closed toggles.
crate static TOGGLE_PLUS_PNG: &[u8] = include_bytes!("static/images/toggle-plus.svg");
crate static TOGGLE_PLUS_SVG: &[u8] = include_bytes!("static/images/toggle-plus.svg");

/// The contents of `COPYRIGHT.txt`, the license listing for files distributed with documentation
/// output.
Expand Down