Skip to content

Commit 9f17be2

Browse files
committed
Merge pull request #109 from jessestricker/feature-favicon
Add theme support for favicon
2 parents fc86b96 + 88fabd7 commit 9f17be2

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

src/book/mdbook.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ impl MDBook {
198198
let mut css = try!(File::create(&theme_dir.join("book.css")));
199199
try!(css.write_all(theme::CSS));
200200

201+
// favicon.png
202+
let mut favicon = try!(File::create(&theme_dir.join("favicon.png")));
203+
try!(favicon.write_all(theme::FAVICON));
204+
201205
// book.js
202206
let mut js = try!(File::create(&theme_dir.join("book.js")));
203207
try!(js.write_all(theme::JS));

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ impl Renderer for HtmlHandlebars {
170170
};
171171
try!(css_file.write_all(&theme.css));
172172

173+
// Favicon
174+
let mut favicon_file = if let Ok(f) = File::create(book.get_dest().join("favicon.png")) { f } else {
175+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create favicon.png")))
176+
};
177+
try!(favicon_file.write_all(&theme.favicon));
178+
173179
// JQuery local fallback
174180
let mut jquery = if let Ok(f) = File::create(book.get_dest().join("jquery.js")) { f } else {
175181
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<BTreeMap<String,Json>, Box<Error>> {
235241
let mut data = BTreeMap::new();
236242
data.insert("language".to_owned(), "en".to_json());
237243
data.insert("title".to_owned(), book.get_title().to_json());
244+
data.insert("favicon".to_owned(), "favicon.png".to_json());
238245

239246
let mut chapters = vec![];
240247

src/theme/favicon.png

5.55 KB
Loading

src/theme/index.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<link rel="stylesheet" href="book.css">
1313
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
1414

15+
<link rel="shortcut icon" href="{{ favicon }}">
16+
1517
<!-- Font Awesome -->
1618
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
1719

src/theme/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::Read;
55

66
pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
77
pub static CSS: &'static [u8] = include_bytes!("book.css");
8+
pub static FAVICON: &'static [u8] = include_bytes!("favicon.png");
89
pub static JS: &'static [u8] = include_bytes!("book.js");
910
pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js");
1011
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/
2728
pub struct Theme {
2829
pub index: Vec<u8>,
2930
pub css: Vec<u8>,
31+
pub favicon: Vec<u8>,
3032
pub js: Vec<u8>,
3133
pub highlight_css: Vec<u8>,
3234
pub tomorrow_night_css: Vec<u8>,
@@ -41,6 +43,7 @@ impl Theme {
4143
let mut theme = Theme {
4244
index: INDEX.to_owned(),
4345
css: CSS.to_owned(),
46+
favicon: FAVICON.to_owned(),
4447
js: JS.to_owned(),
4548
highlight_css: HIGHLIGHT_CSS.to_owned(),
4649
tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(),
@@ -79,6 +82,12 @@ impl Theme {
7982
let _ = f.read_to_end(&mut theme.css);
8083
}
8184

85+
// favicon.png
86+
if let Ok(mut f) = File::open(&src.join("favicon.png")) {
87+
theme.favicon.clear();
88+
let _ = f.read_to_end(&mut theme.favicon);
89+
}
90+
8291
// highlight.js
8392
if let Ok(mut f) = File::open(&src.join("highlight.js")) {
8493
theme.highlight_js.clear();

0 commit comments

Comments
 (0)