Skip to content

Commit 649f355

Browse files
authored
Merge pull request #1266 from ehuss/favicon-no-default
Don't copy default favicon if at least one is overridden.
2 parents 8432df1 + b0c6f2d commit 649f355

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,12 @@ impl HtmlHandlebars {
192192
write_file(destination, "css/chrome.css", &theme.chrome_css)?;
193193
write_file(destination, "css/print.css", &theme.print_css)?;
194194
write_file(destination, "css/variables.css", &theme.variables_css)?;
195-
write_file(destination, "favicon.png", &theme.favicon_png)?;
196-
write_file(destination, "favicon.svg", &theme.favicon_svg)?;
195+
if let Some(contents) = &theme.favicon_png {
196+
write_file(destination, "favicon.png", &contents)?;
197+
}
198+
if let Some(contents) = &theme.favicon_svg {
199+
write_file(destination, "favicon.svg", &contents)?;
200+
}
197201
write_file(destination, "highlight.css", &theme.highlight_css)?;
198202
write_file(destination, "tomorrow-night.css", &theme.tomorrow_night_css)?;
199203
write_file(destination, "ayu-highlight.css", &theme.ayu_highlight_css)?;

src/theme/mod.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)