From f508db61130e12015ec7358179b29179ca0a11c0 Mon Sep 17 00:00:00 2001 From: Jesse Stricker Date: Mon, 22 Feb 2016 17:17:07 +0100 Subject: [PATCH] Add favicon support to theme --- src/renderer/html_handlebars/hbs_renderer.rs | 7 +++++++ src/theme/index.hbs | 2 ++ src/theme/mod.rs | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 68753ceb..613972fe 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -170,6 +170,12 @@ impl Renderer for HtmlHandlebars { }; try!(css_file.write_all(&theme.css)); + // Favicon + let mut favicon_file = if let Ok(f) = File::create(book.get_dest().join("favicon.png")) { f } else { + return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create favicon.png"))) + }; + try!(favicon_file.write_all(&theme.favicon)); + // JQuery local fallback let mut jquery = if let Ok(f) = File::create(book.get_dest().join("jquery.js")) { f } else { return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create jquery.js"))) @@ -235,6 +241,7 @@ fn make_data(book: &MDBook) -> Result, Box> { let mut data = BTreeMap::new(); data.insert("language".to_owned(), "en".to_json()); data.insert("title".to_owned(), book.get_title().to_json()); + data.insert("favicon".to_owned(), "favicon.png".to_json()); let mut chapters = vec![]; diff --git a/src/theme/index.hbs b/src/theme/index.hbs index e4340673..98c959cf 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -12,6 +12,8 @@ + + diff --git a/src/theme/mod.rs b/src/theme/mod.rs index 5fca68fd..e02aca3c 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -5,6 +5,7 @@ use std::io::Read; pub static INDEX: &'static [u8] = include_bytes!("index.hbs"); pub static CSS: &'static [u8] = include_bytes!("book.css"); +pub static FAVICON: &'static [u8] = include_bytes!("favicon.png"); pub static JS: &'static [u8] = include_bytes!("book.js"); pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js"); pub static TOMORROW_NIGHT_CSS: &'static [u8] = include_bytes!("tomorrow-night.css"); @@ -27,6 +28,7 @@ pub static FONT_AWESOME_OTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/ pub struct Theme { pub index: Vec, pub css: Vec, + pub favicon: Vec, pub js: Vec, pub highlight_css: Vec, pub tomorrow_night_css: Vec, @@ -41,6 +43,7 @@ impl Theme { let mut theme = Theme { index: INDEX.to_owned(), css: CSS.to_owned(), + favicon: FAVICON.to_owned(), js: JS.to_owned(), highlight_css: HIGHLIGHT_CSS.to_owned(), tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(), @@ -79,6 +82,12 @@ impl Theme { let _ = f.read_to_end(&mut theme.css); } + // favicon.png + if let Ok(mut f) = File::open(&src.join("favicon.png")) { + theme.favicon.clear(); + let _ = f.read_to_end(&mut theme.favicon); + } + // highlight.js if let Ok(mut f) = File::open(&src.join("highlight.js")) { theme.highlight_js.clear();