@@ -19,17 +19,24 @@ pub static REDIRECT: &[u8] = include_bytes!("templates/redirect.hbs");
19
19
pub static HEADER : & [ u8 ] = include_bytes ! ( "templates/header.hbs" ) ;
20
20
pub static TOC_JS : & [ u8 ] = include_bytes ! ( "templates/toc.js.hbs" ) ;
21
21
pub 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 < ' static > =
23
+ ContentToMinify :: CSS ( include_str ! ( "css/chrome.css" ) ) ;
24
+ pub static GENERAL_CSS : ContentToMinify < ' static > =
25
+ ContentToMinify :: CSS ( include_str ! ( "css/general.css" ) ) ;
26
+ pub static PRINT_CSS : ContentToMinify < ' static > =
27
+ ContentToMinify :: CSS ( include_str ! ( "css/print.css" ) ) ;
28
+ pub static VARIABLES_CSS : ContentToMinify < ' static > =
29
+ ContentToMinify :: CSS ( include_str ! ( "css/variables.css" ) ) ;
26
30
pub static FAVICON_PNG : & [ u8 ] = include_bytes ! ( "images/favicon.png" ) ;
27
31
pub static FAVICON_SVG : & [ u8 ] = include_bytes ! ( "images/favicon.svg" ) ;
28
- pub static JS : & [ u8 ] = include_bytes ! ( "js/book.js" ) ;
32
+ pub static JS : ContentToMinify < ' static > = ContentToMinify :: JS ( include_str ! ( "js/book.js" ) ) ;
29
33
pub 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" ) ;
34
+ pub static TOMORROW_NIGHT_CSS : ContentToMinify < ' static > =
35
+ ContentToMinify :: CSS ( include_str ! ( "css/tomorrow-night.css" ) ) ;
36
+ pub static HIGHLIGHT_CSS : ContentToMinify < ' static > =
37
+ ContentToMinify :: CSS ( include_str ! ( "css/highlight.css" ) ) ;
38
+ pub static AYU_HIGHLIGHT_CSS : ContentToMinify < ' static > =
39
+ ContentToMinify :: CSS ( include_str ! ( "css/ayu-highlight.css" ) ) ;
33
40
pub static CLIPBOARD_JS : & [ u8 ] = include_bytes ! ( "js/clipboard.min.js" ) ;
34
41
pub static FONT_AWESOME : & [ u8 ] = include_bytes ! ( "css/font-awesome.min.css" ) ;
35
42
pub static FONT_AWESOME_EOT : & [ u8 ] = include_bytes ! ( "fonts/fontawesome-webfont.eot" ) ;
@@ -39,6 +46,31 @@ pub static FONT_AWESOME_WOFF: &[u8] = include_bytes!("fonts/fontawesome-webfont.
39
46
pub static FONT_AWESOME_WOFF2 : & [ u8 ] = include_bytes ! ( "fonts/fontawesome-webfont.woff2" ) ;
40
47
pub static FONT_AWESOME_OTF : & [ u8 ] = include_bytes ! ( "fonts/FontAwesome.otf" ) ;
41
48
49
+ #[ derive( Clone , Copy ) ]
50
+ pub enum ContentToMinify < ' a > {
51
+ CSS ( & ' a str ) ,
52
+ JS ( & ' a str ) ,
53
+ }
54
+
55
+ impl < ' a > ContentToMinify < ' a > {
56
+ pub fn minified ( self ) -> Vec < u8 > {
57
+ let mut out = Vec :: new ( ) ;
58
+ self . write_into ( & mut out) . unwrap ( ) ;
59
+ out
60
+ }
61
+
62
+ pub fn write_into < W : std:: io:: Write > ( self , out : & mut W ) -> std:: io:: Result < ( ) > {
63
+ match self {
64
+ Self :: CSS ( data) => match minifier:: css:: minify ( data) {
65
+ Ok ( data) => return data. write ( out) ,
66
+ Err ( _) => out. write ( data. as_bytes ( ) ) ?,
67
+ } ,
68
+ Self :: JS ( data) => return minifier:: js:: minify ( data) . write ( out) ,
69
+ } ;
70
+ Ok ( ( ) )
71
+ }
72
+ }
73
+
42
74
/// The `Theme` struct should be used instead of the static variables because
43
75
/// the `new()` method will look if the user has a theme directory in their
44
76
/// source folder and use the users theme instead of the default.
@@ -181,18 +213,18 @@ impl Default for Theme {
181
213
header : HEADER . to_owned ( ) ,
182
214
toc_js : TOC_JS . to_owned ( ) ,
183
215
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 ( ) ,
216
+ chrome_css : CHROME_CSS . minified ( ) ,
217
+ general_css : GENERAL_CSS . minified ( ) ,
218
+ print_css : PRINT_CSS . minified ( ) ,
219
+ variables_css : VARIABLES_CSS . minified ( ) ,
188
220
fonts_css : None ,
189
221
font_files : Vec :: new ( ) ,
190
222
favicon_png : Some ( FAVICON_PNG . to_owned ( ) ) ,
191
223
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 ( ) ,
224
+ js : JS . minified ( ) ,
225
+ highlight_css : HIGHLIGHT_CSS . minified ( ) ,
226
+ tomorrow_night_css : TOMORROW_NIGHT_CSS . minified ( ) ,
227
+ ayu_highlight_css : AYU_HIGHLIGHT_CSS . minified ( ) ,
196
228
highlight_js : HIGHLIGHT_JS . to_owned ( ) ,
197
229
clipboard_js : CLIPBOARD_JS . to_owned ( ) ,
198
230
}
0 commit comments