]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static_files.rs
Rollup merge of #107559 - WaffleLapkin:is_it_2015¿, r=davidtwco
[rust.git] / src / librustdoc / html / static_files.rs
1 //! Static files bundled with documentation output.
2 //!
3 //! All the static files are included here for centralized access in case anything other than the
4 //! HTML rendering code (say, the theme checker) needs to access one of these files.
5
6 use rustc_data_structures::fx::FxHasher;
7 use std::hash::Hasher;
8 use std::path::{Path, PathBuf};
9 use std::{fmt, str};
10
11 pub(crate) struct StaticFile {
12     pub(crate) filename: PathBuf,
13     pub(crate) bytes: &'static [u8],
14 }
15
16 impl StaticFile {
17     fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
18         Self { filename: static_filename(filename, bytes), bytes }
19     }
20
21     pub(crate) fn minified(&self) -> Vec<u8> {
22         let extension = match self.filename.extension() {
23             Some(e) => e,
24             None => return self.bytes.to_owned(),
25         };
26         if extension == "css" {
27             minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into()
28         } else if extension == "js" {
29             minifier::js::minify(str::from_utf8(self.bytes).unwrap()).to_string().into()
30         } else {
31             self.bytes.to_owned()
32         }
33     }
34
35     pub(crate) fn output_filename(&self) -> &Path {
36         &self.filename
37     }
38 }
39
40 /// The Display implementation for a StaticFile outputs its filename. This makes it
41 /// convenient to interpolate static files into HTML templates.
42 impl fmt::Display for StaticFile {
43     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44         write!(f, "{}", self.output_filename().display())
45     }
46 }
47
48 /// Insert the provided suffix into a filename just before the extension.
49 pub(crate) fn suffix_path(filename: &str, suffix: &str) -> PathBuf {
50     // We use splitn vs Path::extension here because we might get a filename
51     // like `style.min.css` and we want to process that into
52     // `style-suffix.min.css`.  Path::extension would just return `css`
53     // which would result in `style.min-suffix.css` which isn't what we
54     // want.
55     let (base, ext) = filename.split_once('.').unwrap();
56     let filename = format!("{}{}.{}", base, suffix, ext);
57     filename.into()
58 }
59
60 pub(crate) fn static_filename(filename: &str, contents: &[u8]) -> PathBuf {
61     let filename = filename.rsplit('/').next().unwrap();
62     suffix_path(filename, &static_suffix(contents))
63 }
64
65 fn static_suffix(bytes: &[u8]) -> String {
66     let mut hasher = FxHasher::default();
67     hasher.write(bytes);
68     format!("-{:016x}", hasher.finish())
69 }
70
71 macro_rules! static_files {
72     ($($field:ident => $file_path:literal,)+) => {
73         pub(crate) struct StaticFiles {
74             $(pub $field: StaticFile,)+
75         }
76
77         pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
78             $($field: StaticFile::new($file_path, include_bytes!($file_path)),)+
79         });
80
81         pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {
82             for sf in [
83             $(&STATIC_FILES.$field,)+
84             ] {
85                 f(sf)?
86             }
87             Ok(())
88         }
89     }
90 }
91
92 static_files! {
93     rustdoc_css => "static/css/rustdoc.css",
94     settings_css => "static/css/settings.css",
95     noscript_css => "static/css/noscript.css",
96     normalize_css => "static/css/normalize.css",
97     main_js => "static/js/main.js",
98     search_js => "static/js/search.js",
99     settings_js => "static/js/settings.js",
100     source_script_js => "static/js/source-script.js",
101     storage_js => "static/js/storage.js",
102     scrape_examples_js => "static/js/scrape-examples.js",
103     wheel_svg => "static/images/wheel.svg",
104     clipboard_svg => "static/images/clipboard.svg",
105     copyright => "static/COPYRIGHT.txt",
106     license_apache => "static/LICENSE-APACHE.txt",
107     license_mit => "static/LICENSE-MIT.txt",
108     rust_logo_svg => "static/images/rust-logo.svg",
109     rust_favicon_svg => "static/images/favicon.svg",
110     rust_favicon_png_16 => "static/images/favicon-16x16.png",
111     rust_favicon_png_32 => "static/images/favicon-32x32.png",
112     theme_light_css => "static/css/themes/light.css",
113     theme_dark_css => "static/css/themes/dark.css",
114     theme_ayu_css => "static/css/themes/ayu.css",
115     fira_sans_regular => "static/fonts/FiraSans-Regular.woff2",
116     fira_sans_medium => "static/fonts/FiraSans-Medium.woff2",
117     fira_sans_license => "static/fonts/FiraSans-LICENSE.txt",
118     source_serif_4_regular => "static/fonts/SourceSerif4-Regular.ttf.woff2",
119     source_serif_4_bold => "static/fonts/SourceSerif4-Bold.ttf.woff2",
120     source_serif_4_italic => "static/fonts/SourceSerif4-It.ttf.woff2",
121     source_serif_4_license => "static/fonts/SourceSerif4-LICENSE.md",
122     source_code_pro_regular => "static/fonts/SourceCodePro-Regular.ttf.woff2",
123     source_code_pro_semibold => "static/fonts/SourceCodePro-Semibold.ttf.woff2",
124     source_code_pro_italic => "static/fonts/SourceCodePro-It.ttf.woff2",
125     source_code_pro_license => "static/fonts/SourceCodePro-LICENSE.txt",
126     nanum_barun_gothic_regular => "static/fonts/NanumBarunGothic.ttf.woff2",
127     nanum_barun_gothic_license => "static/fonts/NanumBarunGothic-LICENSE.txt",
128 }
129
130 pub(crate) static SCRAPE_EXAMPLES_HELP_MD: &str = include_str!("static/scrape-examples-help.md");