@@ -54,8 +54,8 @@ pub struct Theme {
5454 pub general_css : Vec < u8 > ,
5555 pub print_css : Vec < u8 > ,
5656 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 > > ,
5959 pub js : Vec < u8 > ,
6060 pub highlight_css : Vec < u8 > ,
6161 pub tomorrow_night_css : Vec < u8 > ,
@@ -91,8 +91,6 @@ impl Theme {
9191 theme_dir. join( "css/variables.css" ) ,
9292 & mut theme. variables_css,
9393 ) ,
94- ( theme_dir. join( "favicon.png" ) , & mut theme. favicon_png) ,
95- ( theme_dir. join( "favicon.svg" ) , & mut theme. favicon_svg) ,
9694 ( theme_dir. join( "highlight.js" ) , & mut theme. highlight_js) ,
9795 ( theme_dir. join( "clipboard.min.js" ) , & mut theme. clipboard_js) ,
9896 ( theme_dir. join( "highlight.css" ) , & mut theme. highlight_css) ,
@@ -106,13 +104,36 @@ impl Theme {
106104 ) ,
107105 ] ;
108106
109- for ( filename, dest) in files {
107+ let load_with_warn = | filename : & Path , dest| {
110108 if !filename. exists ( ) {
111- continue ;
109+ // Don't warn if the file doesn't exist.
110+ return false ;
112111 }
113-
114- if let Err ( e) = load_file_contents ( & filename, dest) {
112+ if let Err ( e) = load_file_contents ( filename, dest) {
115113 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 ;
116137 }
117138 }
118139 }
@@ -132,8 +153,8 @@ impl Default for Theme {
132153 general_css : GENERAL_CSS . to_owned ( ) ,
133154 print_css : PRINT_CSS . to_owned ( ) ,
134155 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 ( ) ) ,
137158 js : JS . to_owned ( ) ,
138159 highlight_css : HIGHLIGHT_CSS . to_owned ( ) ,
139160 tomorrow_night_css : TOMORROW_NIGHT_CSS . to_owned ( ) ,
@@ -219,8 +240,8 @@ mod tests {
219240 general_css : Vec :: new ( ) ,
220241 print_css : Vec :: new ( ) ,
221242 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 ( ) ) ,
224245 js : Vec :: new ( ) ,
225246 highlight_css : Vec :: new ( ) ,
226247 tomorrow_night_css : Vec :: new ( ) ,
@@ -231,4 +252,19 @@ mod tests {
231252
232253 assert_eq ! ( got, empty) ;
233254 }
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+ }
234270}
0 commit comments