Skip to content

Commit 7031e4e

Browse files
committed
Rollup merge of #55856 - QuietMisdreavus:static-discharge, r=GuillaumeGomez
rustdoc: refactor: move all static-file include!s into a single module This is a smaller refactor that creates a new module `rustdoc::html::static_files`, which contains a bunch of `static` variables with all the files in `html/static` that we use. The idea behind moving them all here was to remove the duplicate `include_bytes!()` that are used by the theme-checker code. It also continues to centralize more operations in rustdoc.
2 parents d0b68c0 + 2f8ce73 commit 7031e4e

File tree

4 files changed

+140
-27
lines changed

4 files changed

+140
-27
lines changed

src/librustdoc/config.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use core::new_handler;
2929
use externalfiles::ExternalHtml;
3030
use html;
3131
use html::markdown::IdMap;
32+
use html::static_files;
3233
use opts;
3334
use passes::{self, DefaultPassOption};
3435
use theme;
@@ -261,7 +262,7 @@ impl Options {
261262

262263
let to_check = matches.opt_strs("theme-checker");
263264
if !to_check.is_empty() {
264-
let paths = theme::load_css_paths(include_bytes!("html/static/themes/light.css"));
265+
let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
265266
let mut errors = 0;
266267

267268
println!("rustdoc: [theme-checker] Starting tests!");
@@ -338,7 +339,7 @@ impl Options {
338339

339340
let mut themes = Vec::new();
340341
if matches.opt_present("themes") {
341-
let paths = theme::load_css_paths(include_bytes!("html/static/themes/light.css"));
342+
let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
342343

343344
for (theme_file, theme_s) in matches.opt_strs("themes")
344345
.iter()

src/librustdoc/html/render.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
7676
use html::format::fmt_impl_for_trait_page;
7777
use html::item_type::ItemType;
7878
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap};
79-
use html::{highlight, layout};
79+
use html::{highlight, layout, static_files};
8080

8181
use minifier;
8282

@@ -767,10 +767,10 @@ fn write_shared(
767767
// overwrite them anyway to make sure that they're fresh and up-to-date.
768768

769769
write_minify(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
770-
include_str!("static/rustdoc.css"),
770+
static_files::RUSTDOC_CSS,
771771
options.enable_minification)?;
772772
write_minify(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
773-
include_str!("static/settings.css"),
773+
static_files::SETTINGS_CSS,
774774
options.enable_minification)?;
775775

776776
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
@@ -790,15 +790,15 @@ fn write_shared(
790790
}
791791

792792
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
793-
include_bytes!("static/brush.svg"))?;
793+
static_files::BRUSH_SVG)?;
794794
write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
795-
include_bytes!("static/wheel.svg"))?;
795+
static_files::WHEEL_SVG)?;
796796
write_minify(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
797-
include_str!("static/themes/light.css"),
797+
static_files::themes::LIGHT,
798798
options.enable_minification)?;
799799
themes.insert("light".to_owned());
800800
write_minify(cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)),
801-
include_str!("static/themes/dark.css"),
801+
static_files::themes::DARK,
802802
options.enable_minification)?;
803803
themes.insert("dark".to_owned());
804804

@@ -854,16 +854,16 @@ themePicker.onblur = handleThemeButtonsBlur;
854854
)?;
855855

856856
write_minify(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
857-
include_str!("static/main.js"),
857+
static_files::MAIN_JS,
858858
options.enable_minification)?;
859859
write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
860-
include_str!("static/settings.js"),
860+
static_files::SETTINGS_JS,
861861
options.enable_minification)?;
862862

863863
{
864864
let mut data = format!("var resourcesSuffix = \"{}\";\n",
865865
cx.shared.resource_suffix);
866-
data.push_str(include_str!("static/storage.js"));
866+
data.push_str(static_files::STORAGE_JS);
867867
write_minify(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)),
868868
&data,
869869
options.enable_minification)?;
@@ -882,36 +882,36 @@ themePicker.onblur = handleThemeButtonsBlur;
882882
}
883883
}
884884
write_minify(cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)),
885-
include_str!("static/normalize.css"),
885+
static_files::NORMALIZE_CSS,
886886
options.enable_minification)?;
887887
write(cx.dst.join("FiraSans-Regular.woff"),
888-
include_bytes!("static/FiraSans-Regular.woff"))?;
888+
static_files::fira_sans::REGULAR)?;
889889
write(cx.dst.join("FiraSans-Medium.woff"),
890-
include_bytes!("static/FiraSans-Medium.woff"))?;
890+
static_files::fira_sans::MEDIUM)?;
891891
write(cx.dst.join("FiraSans-LICENSE.txt"),
892-
include_bytes!("static/FiraSans-LICENSE.txt"))?;
892+
static_files::fira_sans::LICENSE)?;
893893
write(cx.dst.join("Heuristica-Italic.woff"),
894-
include_bytes!("static/Heuristica-Italic.woff"))?;
894+
static_files::heuristica::ITALIC)?;
895895
write(cx.dst.join("Heuristica-LICENSE.txt"),
896-
include_bytes!("static/Heuristica-LICENSE.txt"))?;
896+
static_files::heuristica::LICENSE)?;
897897
write(cx.dst.join("SourceSerifPro-Regular.woff"),
898-
include_bytes!("static/SourceSerifPro-Regular.woff"))?;
898+
static_files::source_serif_pro::REGULAR)?;
899899
write(cx.dst.join("SourceSerifPro-Bold.woff"),
900-
include_bytes!("static/SourceSerifPro-Bold.woff"))?;
900+
static_files::source_serif_pro::BOLD)?;
901901
write(cx.dst.join("SourceSerifPro-LICENSE.txt"),
902-
include_bytes!("static/SourceSerifPro-LICENSE.txt"))?;
902+
static_files::source_serif_pro::LICENSE)?;
903903
write(cx.dst.join("SourceCodePro-Regular.woff"),
904-
include_bytes!("static/SourceCodePro-Regular.woff"))?;
904+
static_files::source_code_pro::REGULAR)?;
905905
write(cx.dst.join("SourceCodePro-Semibold.woff"),
906-
include_bytes!("static/SourceCodePro-Semibold.woff"))?;
906+
static_files::source_code_pro::SEMIBOLD)?;
907907
write(cx.dst.join("SourceCodePro-LICENSE.txt"),
908-
include_bytes!("static/SourceCodePro-LICENSE.txt"))?;
908+
static_files::source_code_pro::LICENSE)?;
909909
write(cx.dst.join("LICENSE-MIT.txt"),
910-
include_bytes!("static/LICENSE-MIT.txt"))?;
910+
static_files::LICENSE_MIT)?;
911911
write(cx.dst.join("LICENSE-APACHE.txt"),
912-
include_bytes!("static/LICENSE-APACHE.txt"))?;
912+
static_files::LICENSE_APACHE)?;
913913
write(cx.dst.join("COPYRIGHT.txt"),
914-
include_bytes!("static/COPYRIGHT.txt"))?;
914+
static_files::COPYRIGHT)?;
915915

916916
fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
917917
let mut ret = Vec::new();

src/librustdoc/html/static_files.rs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Static files bundled with documentation output.
12+
//!
13+
//! All the static files are included here for centralized access in case anything other than the
14+
//! HTML rendering code (say, the theme checker) needs to access one of these files.
15+
//!
16+
//! Note about types: CSS and JavaScript files are included as `&'static str` to allow for the
17+
//! minifier to run on them. All other files are included as `&'static [u8]` so they can be
18+
//! directly written to a `Write` handle.
19+
20+
/// The file contents of the main `rustdoc.css` file, responsible for the core layout of the page.
21+
pub static RUSTDOC_CSS: &'static str = include_str!("static/rustdoc.css");
22+
23+
/// The file contents of `settings.css`, responsible for the items on the settings page.
24+
pub static SETTINGS_CSS: &'static str = include_str!("static/settings.css");
25+
26+
/// The file contents of `normalize.css`, included to even out standard elements between browser
27+
/// implementations.
28+
pub static NORMALIZE_CSS: &'static str = include_str!("static/normalize.css");
29+
30+
/// The file contents of `main.js`, which contains the core JavaScript used on documentation pages,
31+
/// including search behavior and docblock folding, among others.
32+
pub static MAIN_JS: &'static str = include_str!("static/main.js");
33+
34+
/// The file contents of `settings.js`, which contains the JavaScript used to handle the settings
35+
/// page.
36+
pub static SETTINGS_JS: &'static str = include_str!("static/settings.js");
37+
38+
/// The file contents of `storage.js`, which contains functionality related to browser Local
39+
/// Storage, used to store documentation settings.
40+
pub static STORAGE_JS: &'static str = include_str!("static/storage.js");
41+
42+
/// The file contents of `brush.svg`, the icon used for the theme-switch button.
43+
pub static BRUSH_SVG: &'static [u8] = include_bytes!("static/brush.svg");
44+
45+
/// The file contents of `wheel.svg`, the icon used for the settings button.
46+
pub static WHEEL_SVG: &'static [u8] = include_bytes!("static/wheel.svg");
47+
48+
/// The contents of `COPYRIGHT.txt`, the license listing for files distributed with documentation
49+
/// output.
50+
pub static COPYRIGHT: &'static [u8] = include_bytes!("static/COPYRIGHT.txt");
51+
52+
/// The contents of `LICENSE-APACHE.txt`, the text of the Apache License, version 2.0.
53+
pub static LICENSE_APACHE: &'static [u8] = include_bytes!("static/LICENSE-APACHE.txt");
54+
55+
/// The contents of `LICENSE-MIT.txt`, the text of the MIT License.
56+
pub static LICENSE_MIT: &'static [u8] = include_bytes!("static/LICENSE-MIT.txt");
57+
58+
/// The built-in themes given to every documentation site.
59+
pub mod themes {
60+
/// The "light" theme, selected by default when no setting is available. Used as the basis for
61+
/// the `--theme-checker` functionality.
62+
pub static LIGHT: &'static str = include_str!("static/themes/light.css");
63+
64+
/// The "dark" theme.
65+
pub static DARK: &'static str = include_str!("static/themes/dark.css");
66+
}
67+
68+
/// Files related to the Fira Sans font.
69+
pub mod fira_sans {
70+
/// The file `FiraSans-Regular.woff`, the Regular variant of the Fira Sans font.
71+
pub static REGULAR: &'static [u8] = include_bytes!("static/FiraSans-Regular.woff");
72+
73+
/// The file `FiraSans-Medium.woff`, the Medium variant of the Fira Sans font.
74+
pub static MEDIUM: &'static [u8] = include_bytes!("static/FiraSans-Medium.woff");
75+
76+
/// The file `FiraSans-LICENSE.txt`, the license text for the Fira Sans font.
77+
pub static LICENSE: &'static [u8] = include_bytes!("static/FiraSans-LICENSE.txt");
78+
}
79+
80+
/// Files related to the Heuristica font.
81+
pub mod heuristica {
82+
/// The file `Heuristica-Italic.woff`, the Italic variant of the Heuristica font.
83+
pub static ITALIC: &'static [u8] = include_bytes!("static/Heuristica-Italic.woff");
84+
85+
/// The file `Heuristica-LICENSE.txt`, the license text for the Heuristica font.
86+
pub static LICENSE: &'static [u8] = include_bytes!("static/Heuristica-LICENSE.txt");
87+
}
88+
89+
/// Files related to the Source Serif Pro font.
90+
pub mod source_serif_pro {
91+
/// The file `SourceSerifPro-Regular.woff`, the Regular variant of the Source Serif Pro font.
92+
pub static REGULAR: &'static [u8] = include_bytes!("static/SourceSerifPro-Regular.woff");
93+
94+
/// The file `SourceSerifPro-Bold.woff`, the Bold variant of the Source Serif Pro font.
95+
pub static BOLD: &'static [u8] = include_bytes!("static/SourceSerifPro-Bold.woff");
96+
97+
/// The file `SourceSerifPro-LICENSE.txt`, the license text for the Source Serif Pro font.
98+
pub static LICENSE: &'static [u8] = include_bytes!("static/SourceSerifPro-LICENSE.txt");
99+
}
100+
101+
/// Files related to the Source Code Pro font.
102+
pub mod source_code_pro {
103+
/// The file `SourceCodePro-Regular.woff`, the Regular variant of the Source Code Pro font.
104+
pub static REGULAR: &'static [u8] = include_bytes!("static/SourceCodePro-Regular.woff");
105+
106+
/// The file `SourceCodePro-Semibold.woff`, the Semibold variant of the Source Code Pro font.
107+
pub static SEMIBOLD: &'static [u8] = include_bytes!("static/SourceCodePro-Semibold.woff");
108+
109+
/// The file `SourceCodePro-LICENSE.txt`, the license text of the Source Code Pro font.
110+
pub static LICENSE: &'static [u8] = include_bytes!("static/SourceCodePro-LICENSE.txt");
111+
}

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub mod html {
7878
crate mod layout;
7979
pub mod markdown;
8080
crate mod render;
81+
crate mod static_files;
8182
crate mod toc;
8283
}
8384
mod markdown;

0 commit comments

Comments
 (0)