@@ -19,17 +19,19 @@ pub static REDIRECT: &[u8] = include_bytes!("templates/redirect.hbs");
1919pub static HEADER : & [ u8 ] = include_bytes ! ( "templates/header.hbs" ) ;
2020pub static TOC_JS : & [ u8 ] = include_bytes ! ( "templates/toc.js.hbs" ) ;
2121pub static TOC_HTML : & [ u8 ] = include_bytes ! ( "templates/toc.html.hbs" ) ;
22- pub static CHROME_CSS : & [ u8 ] = include_bytes ! ( "css/chrome.css" ) ;
23- pub static GENERAL_CSS : & [ u8 ] = include_bytes ! ( "css/general.css" ) ;
24- pub static PRINT_CSS : & [ u8 ] = include_bytes ! ( "css/print.css" ) ;
25- pub static VARIABLES_CSS : & [ u8 ] = include_bytes ! ( "css/variables.css" ) ;
22+ pub static CHROME_CSS : ContentToMinify = ContentToMinify :: CSS ( include_str ! ( "css/chrome.css" ) ) ;
23+ pub static GENERAL_CSS : ContentToMinify = ContentToMinify :: CSS ( include_str ! ( "css/general.css" ) ) ;
24+ pub static PRINT_CSS : ContentToMinify = ContentToMinify :: CSS ( include_str ! ( "css/print.css" ) ) ;
25+ pub static VARIABLES_CSS : ContentToMinify = ContentToMinify :: CSS ( include_str ! ( "css/variables.css" ) ) ;
2626pub static FAVICON_PNG : & [ u8 ] = include_bytes ! ( "images/favicon.png" ) ;
2727pub static FAVICON_SVG : & [ u8 ] = include_bytes ! ( "images/favicon.svg" ) ;
28- pub static JS : & [ u8 ] = include_bytes ! ( "js/book.js" ) ;
28+ pub static JS : ContentToMinify = ContentToMinify :: JS ( include_str ! ( "js/book.js" ) ) ;
2929pub static HIGHLIGHT_JS : & [ u8 ] = include_bytes ! ( "js/highlight.js" ) ;
30- pub static TOMORROW_NIGHT_CSS : & [ u8 ] = include_bytes ! ( "css/tomorrow-night.css" ) ;
31- pub static HIGHLIGHT_CSS : & [ u8 ] = include_bytes ! ( "css/highlight.css" ) ;
32- pub static AYU_HIGHLIGHT_CSS : & [ u8 ] = include_bytes ! ( "css/ayu-highlight.css" ) ;
30+ pub static TOMORROW_NIGHT_CSS : ContentToMinify =
31+ ContentToMinify :: CSS ( include_str ! ( "css/tomorrow-night.css" ) ) ;
32+ pub static HIGHLIGHT_CSS : ContentToMinify = ContentToMinify :: CSS ( include_str ! ( "css/highlight.css" ) ) ;
33+ pub static AYU_HIGHLIGHT_CSS : ContentToMinify =
34+ ContentToMinify :: CSS ( include_str ! ( "css/ayu-highlight.css" ) ) ;
3335pub static CLIPBOARD_JS : & [ u8 ] = include_bytes ! ( "js/clipboard.min.js" ) ;
3436pub static FONT_AWESOME : & [ u8 ] = include_bytes ! ( "css/font-awesome.min.css" ) ;
3537pub static FONT_AWESOME_EOT : & [ u8 ] = include_bytes ! ( "fonts/fontawesome-webfont.eot" ) ;
@@ -39,6 +41,31 @@ pub static FONT_AWESOME_WOFF: &[u8] = include_bytes!("fonts/fontawesome-webfont.
3941pub static FONT_AWESOME_WOFF2 : & [ u8 ] = include_bytes ! ( "fonts/fontawesome-webfont.woff2" ) ;
4042pub static FONT_AWESOME_OTF : & [ u8 ] = include_bytes ! ( "fonts/FontAwesome.otf" ) ;
4143
44+ #[ derive( Clone , Copy ) ]
45+ pub enum ContentToMinify {
46+ CSS ( & ' static str ) ,
47+ JS ( & ' static str ) ,
48+ }
49+
50+ impl ContentToMinify {
51+ pub fn minified ( self ) -> Vec < u8 > {
52+ let mut out = Vec :: new ( ) ;
53+ self . write_into ( & mut out) . unwrap ( ) ;
54+ out
55+ }
56+
57+ pub fn write_into < W : std:: io:: Write > ( self , out : & mut W ) -> std:: io:: Result < ( ) > {
58+ match self {
59+ Self :: CSS ( data) => match minifier:: css:: minify ( data) {
60+ Ok ( data) => return data. write ( out) ,
61+ Err ( _) => out. write ( data. as_bytes ( ) ) ?,
62+ } ,
63+ Self :: JS ( data) => return minifier:: js:: minify ( data) . write ( out) ,
64+ } ;
65+ Ok ( ( ) )
66+ }
67+ }
68+
4269/// The `Theme` struct should be used instead of the static variables because
4370/// the `new()` method will look if the user has a theme directory in their
4471/// source folder and use the users theme instead of the default.
@@ -181,18 +208,18 @@ impl Default for Theme {
181208 header : HEADER . to_owned ( ) ,
182209 toc_js : TOC_JS . to_owned ( ) ,
183210 toc_html : TOC_HTML . to_owned ( ) ,
184- chrome_css : CHROME_CSS . to_owned ( ) ,
185- general_css : GENERAL_CSS . to_owned ( ) ,
186- print_css : PRINT_CSS . to_owned ( ) ,
187- variables_css : VARIABLES_CSS . to_owned ( ) ,
211+ chrome_css : CHROME_CSS . minified ( ) ,
212+ general_css : GENERAL_CSS . minified ( ) ,
213+ print_css : PRINT_CSS . minified ( ) ,
214+ variables_css : VARIABLES_CSS . minified ( ) ,
188215 fonts_css : None ,
189216 font_files : Vec :: new ( ) ,
190217 favicon_png : Some ( FAVICON_PNG . to_owned ( ) ) ,
191218 favicon_svg : Some ( FAVICON_SVG . to_owned ( ) ) ,
192- js : JS . to_owned ( ) ,
193- highlight_css : HIGHLIGHT_CSS . to_owned ( ) ,
194- tomorrow_night_css : TOMORROW_NIGHT_CSS . to_owned ( ) ,
195- ayu_highlight_css : AYU_HIGHLIGHT_CSS . to_owned ( ) ,
219+ js : JS . minified ( ) ,
220+ highlight_css : HIGHLIGHT_CSS . minified ( ) ,
221+ tomorrow_night_css : TOMORROW_NIGHT_CSS . minified ( ) ,
222+ ayu_highlight_css : AYU_HIGHLIGHT_CSS . minified ( ) ,
196223 highlight_js : HIGHLIGHT_JS . to_owned ( ) ,
197224 clipboard_js : CLIPBOARD_JS . to_owned ( ) ,
198225 }
0 commit comments