@@ -53,7 +53,12 @@ pub enum ContentToMinify<'a> {
53
53
}
54
54
55
55
impl < ' a > ContentToMinify < ' a > {
56
- pub fn minified ( self ) -> Vec < u8 > {
56
+ /// If `minification` is false, it simply returns the inner data converted into a `Vec`.
57
+ pub fn minified ( self , minification : bool ) -> Vec < u8 > {
58
+ if !minification {
59
+ let ( Self :: CSS ( data) | Self :: JS ( data) ) = self ;
60
+ return data. as_bytes ( ) . to_owned ( ) ;
61
+ }
57
62
let mut out = Vec :: new ( ) ;
58
63
self . write_into ( & mut out) . unwrap ( ) ;
59
64
out
@@ -104,9 +109,30 @@ pub struct Theme {
104
109
impl Theme {
105
110
/// Creates a `Theme` from the given `theme_dir`.
106
111
/// If a file is found in the theme dir, it will override the default version.
107
- pub fn new < P : AsRef < Path > > ( theme_dir : P ) -> Self {
112
+ pub fn new < P : AsRef < Path > > ( theme_dir : P , minification : bool ) -> Self {
108
113
let theme_dir = theme_dir. as_ref ( ) ;
109
- let mut theme = Theme :: default ( ) ;
114
+ let mut theme = Theme {
115
+ index : INDEX . to_owned ( ) ,
116
+ head : HEAD . to_owned ( ) ,
117
+ redirect : REDIRECT . to_owned ( ) ,
118
+ header : HEADER . to_owned ( ) ,
119
+ toc_js : TOC_JS . to_owned ( ) ,
120
+ toc_html : TOC_HTML . to_owned ( ) ,
121
+ chrome_css : CHROME_CSS . minified ( minification) ,
122
+ general_css : GENERAL_CSS . minified ( minification) ,
123
+ print_css : PRINT_CSS . minified ( minification) ,
124
+ variables_css : VARIABLES_CSS . minified ( minification) ,
125
+ fonts_css : None ,
126
+ font_files : Vec :: new ( ) ,
127
+ favicon_png : Some ( FAVICON_PNG . to_owned ( ) ) ,
128
+ favicon_svg : Some ( FAVICON_SVG . to_owned ( ) ) ,
129
+ js : JS . minified ( minification) ,
130
+ highlight_css : HIGHLIGHT_CSS . minified ( minification) ,
131
+ tomorrow_night_css : TOMORROW_NIGHT_CSS . minified ( minification) ,
132
+ ayu_highlight_css : AYU_HIGHLIGHT_CSS . minified ( minification) ,
133
+ highlight_js : HIGHLIGHT_JS . to_owned ( ) ,
134
+ clipboard_js : CLIPBOARD_JS . to_owned ( ) ,
135
+ } ;
110
136
111
137
// If the theme directory doesn't exist there's no point continuing...
112
138
if !theme_dir. exists ( ) || !theme_dir. is_dir ( ) {
@@ -204,33 +230,6 @@ impl Theme {
204
230
}
205
231
}
206
232
207
- impl Default for Theme {
208
- fn default ( ) -> Theme {
209
- Theme {
210
- index : INDEX . to_owned ( ) ,
211
- head : HEAD . to_owned ( ) ,
212
- redirect : REDIRECT . to_owned ( ) ,
213
- header : HEADER . to_owned ( ) ,
214
- toc_js : TOC_JS . to_owned ( ) ,
215
- toc_html : TOC_HTML . 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 ( ) ,
220
- fonts_css : None ,
221
- font_files : Vec :: new ( ) ,
222
- favicon_png : Some ( FAVICON_PNG . to_owned ( ) ) ,
223
- favicon_svg : Some ( FAVICON_SVG . 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 ( ) ,
228
- highlight_js : HIGHLIGHT_JS . to_owned ( ) ,
229
- clipboard_js : CLIPBOARD_JS . to_owned ( ) ,
230
- }
231
- }
232
- }
233
-
234
233
/// Checks if a file exists, if so, the destination buffer will be filled with
235
234
/// its contents.
236
235
fn load_file_contents < P : AsRef < Path > > ( filename : P , dest : & mut Vec < u8 > ) -> Result < ( ) > {
@@ -329,13 +328,13 @@ mod tests {
329
328
fn favicon_override ( ) {
330
329
let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) . unwrap ( ) ;
331
330
fs:: write ( temp. path ( ) . join ( "favicon.png" ) , "1234" ) . unwrap ( ) ;
332
- let got = Theme :: new ( temp. path ( ) ) ;
331
+ let got = Theme :: new ( temp. path ( ) , false ) ;
333
332
assert_eq ! ( got. favicon_png. as_ref( ) . unwrap( ) , b"1234" ) ;
334
333
assert_eq ! ( got. favicon_svg, None ) ;
335
334
336
335
let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) . unwrap ( ) ;
337
336
fs:: write ( temp. path ( ) . join ( "favicon.svg" ) , "4567" ) . unwrap ( ) ;
338
- let got = Theme :: new ( temp. path ( ) ) ;
337
+ let got = Theme :: new ( temp. path ( ) , false ) ;
339
338
assert_eq ! ( got. favicon_png, None ) ;
340
339
assert_eq ! ( got. favicon_svg. as_ref( ) . unwrap( ) , b"4567" ) ;
341
340
}
0 commit comments