@@ -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,9 @@ 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 = Self :: new_with_set_fields ( minification ) ;
110
115
111
116
// If the theme directory doesn't exist there's no point continuing...
112
117
if !theme_dir. exists ( ) || !theme_dir. is_dir ( ) {
@@ -202,29 +207,27 @@ impl Theme {
202
207
203
208
theme
204
209
}
205
- }
206
210
207
- impl Default for Theme {
208
- fn default ( ) -> Theme {
211
+ fn new_with_set_fields ( minification : bool ) -> Self {
209
212
Theme {
210
213
index : INDEX . to_owned ( ) ,
211
214
head : HEAD . to_owned ( ) ,
212
215
redirect : REDIRECT . to_owned ( ) ,
213
216
header : HEADER . to_owned ( ) ,
214
217
toc_js : TOC_JS . to_owned ( ) ,
215
218
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 ( ) ,
219
+ chrome_css : CHROME_CSS . minified ( minification ) ,
220
+ general_css : GENERAL_CSS . minified ( minification ) ,
221
+ print_css : PRINT_CSS . minified ( minification ) ,
222
+ variables_css : VARIABLES_CSS . minified ( minification ) ,
220
223
fonts_css : None ,
221
224
font_files : Vec :: new ( ) ,
222
225
favicon_png : Some ( FAVICON_PNG . to_owned ( ) ) ,
223
226
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 ( ) ,
227
+ js : JS . minified ( minification ) ,
228
+ highlight_css : HIGHLIGHT_CSS . minified ( minification ) ,
229
+ tomorrow_night_css : TOMORROW_NIGHT_CSS . minified ( minification ) ,
230
+ ayu_highlight_css : AYU_HIGHLIGHT_CSS . minified ( minification ) ,
228
231
highlight_js : HIGHLIGHT_JS . to_owned ( ) ,
229
232
clipboard_js : CLIPBOARD_JS . to_owned ( ) ,
230
233
}
@@ -258,8 +261,9 @@ mod tests {
258
261
let non_existent = PathBuf :: from ( "/non/existent/directory/" ) ;
259
262
assert ! ( !non_existent. exists( ) ) ;
260
263
261
- let should_be = Theme :: default ( ) ;
262
- let got = Theme :: new ( & non_existent) ;
264
+ let minification = false ;
265
+ let should_be = Theme :: new_with_set_fields ( minification) ;
266
+ let got = Theme :: new ( & non_existent, minification) ;
263
267
264
268
assert_eq ! ( got, should_be) ;
265
269
}
@@ -297,7 +301,7 @@ mod tests {
297
301
File :: create ( & temp. path ( ) . join ( file) ) . unwrap ( ) ;
298
302
}
299
303
300
- let got = Theme :: new ( temp. path ( ) ) ;
304
+ let got = Theme :: new ( temp. path ( ) , false ) ;
301
305
302
306
let empty = Theme {
303
307
index : Vec :: new ( ) ,
@@ -329,13 +333,13 @@ mod tests {
329
333
fn favicon_override ( ) {
330
334
let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) . unwrap ( ) ;
331
335
fs:: write ( temp. path ( ) . join ( "favicon.png" ) , "1234" ) . unwrap ( ) ;
332
- let got = Theme :: new ( temp. path ( ) ) ;
336
+ let got = Theme :: new ( temp. path ( ) , false ) ;
333
337
assert_eq ! ( got. favicon_png. as_ref( ) . unwrap( ) , b"1234" ) ;
334
338
assert_eq ! ( got. favicon_svg, None ) ;
335
339
336
340
let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) . unwrap ( ) ;
337
341
fs:: write ( temp. path ( ) . join ( "favicon.svg" ) , "4567" ) . unwrap ( ) ;
338
- let got = Theme :: new ( temp. path ( ) ) ;
342
+ let got = Theme :: new ( temp. path ( ) , false ) ;
339
343
assert_eq ! ( got. favicon_png, None ) ;
340
344
assert_eq ! ( got. favicon_svg. as_ref( ) . unwrap( ) , b"4567" ) ;
341
345
}
0 commit comments