@@ -54,8 +54,8 @@ pub struct Theme {
54
54
pub general_css : Vec < u8 > ,
55
55
pub print_css : Vec < u8 > ,
56
56
pub variables_css : Vec < u8 > ,
57
- pub favicon_png : Vec < u8 > ,
58
- pub favicon_svg : Vec < u8 > ,
57
+ pub favicon_png : Option < Vec < u8 > > ,
58
+ pub favicon_svg : Option < Vec < u8 > > ,
59
59
pub js : Vec < u8 > ,
60
60
pub highlight_css : Vec < u8 > ,
61
61
pub tomorrow_night_css : Vec < u8 > ,
@@ -91,8 +91,6 @@ impl Theme {
91
91
theme_dir. join( "css/variables.css" ) ,
92
92
& mut theme. variables_css,
93
93
) ,
94
- ( theme_dir. join( "favicon.png" ) , & mut theme. favicon_png) ,
95
- ( theme_dir. join( "favicon.svg" ) , & mut theme. favicon_svg) ,
96
94
( theme_dir. join( "highlight.js" ) , & mut theme. highlight_js) ,
97
95
( theme_dir. join( "clipboard.min.js" ) , & mut theme. clipboard_js) ,
98
96
( theme_dir. join( "highlight.css" ) , & mut theme. highlight_css) ,
@@ -106,13 +104,36 @@ impl Theme {
106
104
) ,
107
105
] ;
108
106
109
- for ( filename, dest) in files {
107
+ let load_with_warn = | filename : & Path , dest| {
110
108
if !filename. exists ( ) {
111
- continue ;
109
+ // Don't warn if the file doesn't exist.
110
+ return false ;
112
111
}
113
-
114
- if let Err ( e) = load_file_contents ( & filename, dest) {
112
+ if let Err ( e) = load_file_contents ( filename, dest) {
115
113
warn ! ( "Couldn't load custom file, {}: {}" , filename. display( ) , e) ;
114
+ false
115
+ } else {
116
+ true
117
+ }
118
+ } ;
119
+
120
+ for ( filename, dest) in files {
121
+ load_with_warn ( & filename, dest) ;
122
+ }
123
+
124
+ // If the user overrides one favicon, but not the other, do not
125
+ // copy the default for the other.
126
+ let favicon_png = & mut theme. favicon_png . as_mut ( ) . unwrap ( ) ;
127
+ let png = load_with_warn ( & theme_dir. join ( "favicon.png" ) , favicon_png) ;
128
+ let favicon_svg = & mut theme. favicon_svg . as_mut ( ) . unwrap ( ) ;
129
+ let svg = load_with_warn ( & theme_dir. join ( "favicon.svg" ) , favicon_svg) ;
130
+ match ( png, svg) {
131
+ ( true , true ) | ( false , false ) => { }
132
+ ( true , false ) => {
133
+ theme. favicon_svg = None ;
134
+ }
135
+ ( false , true ) => {
136
+ theme. favicon_png = None ;
116
137
}
117
138
}
118
139
}
@@ -132,8 +153,8 @@ impl Default for Theme {
132
153
general_css : GENERAL_CSS . to_owned ( ) ,
133
154
print_css : PRINT_CSS . to_owned ( ) ,
134
155
variables_css : VARIABLES_CSS . to_owned ( ) ,
135
- favicon_png : FAVICON_PNG . to_owned ( ) ,
136
- favicon_svg : FAVICON_SVG . to_owned ( ) ,
156
+ favicon_png : Some ( FAVICON_PNG . to_owned ( ) ) ,
157
+ favicon_svg : Some ( FAVICON_SVG . to_owned ( ) ) ,
137
158
js : JS . to_owned ( ) ,
138
159
highlight_css : HIGHLIGHT_CSS . to_owned ( ) ,
139
160
tomorrow_night_css : TOMORROW_NIGHT_CSS . to_owned ( ) ,
@@ -219,8 +240,8 @@ mod tests {
219
240
general_css : Vec :: new ( ) ,
220
241
print_css : Vec :: new ( ) ,
221
242
variables_css : Vec :: new ( ) ,
222
- favicon_png : Vec :: new ( ) ,
223
- favicon_svg : Vec :: new ( ) ,
243
+ favicon_png : Some ( Vec :: new ( ) ) ,
244
+ favicon_svg : Some ( Vec :: new ( ) ) ,
224
245
js : Vec :: new ( ) ,
225
246
highlight_css : Vec :: new ( ) ,
226
247
tomorrow_night_css : Vec :: new ( ) ,
@@ -231,4 +252,19 @@ mod tests {
231
252
232
253
assert_eq ! ( got, empty) ;
233
254
}
255
+
256
+ #[ test]
257
+ fn favicon_override ( ) {
258
+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) . unwrap ( ) ;
259
+ fs:: write ( temp. path ( ) . join ( "favicon.png" ) , "1234" ) . unwrap ( ) ;
260
+ let got = Theme :: new ( temp. path ( ) ) ;
261
+ assert_eq ! ( got. favicon_png. as_ref( ) . unwrap( ) , b"1234" ) ;
262
+ assert_eq ! ( got. favicon_svg, None ) ;
263
+
264
+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) . unwrap ( ) ;
265
+ fs:: write ( temp. path ( ) . join ( "favicon.svg" ) , "4567" ) . unwrap ( ) ;
266
+ let got = Theme :: new ( temp. path ( ) ) ;
267
+ assert_eq ! ( got. favicon_png, None ) ;
268
+ assert_eq ! ( got. favicon_svg. as_ref( ) . unwrap( ) , b"4567" ) ;
269
+ }
234
270
}
0 commit comments