It's now possible to use a custom theme. Add a theme directory in your src and selectively overwrite the files you want to customize. Closes #6

This commit is contained in:
Mathieu David 2015-08-04 17:58:09 +02:00
parent 4ead44457a
commit 2c22d11bfd
3 changed files with 81 additions and 7 deletions

View File

@ -2,10 +2,10 @@ extern crate handlebars;
extern crate rustc_serialize;
extern crate pulldown_cmark;
use renderer::html_handlebars::helpers;
use renderer::html_handlebars::{helpers, theme};
use renderer::Renderer;
use book::{BookItems, BookConfig};
use {theme, utils};
use utils;
use std::path::PathBuf;
use std::fs::{self, File};
@ -30,12 +30,12 @@ impl Renderer for HtmlHandlebars {
debug!("[fn]: render");
let mut handlebars = Handlebars::new();
// Load template
let t = theme::get_index_hbs();
// Load theme
let theme = theme::Theme::new(&config.src());
// Register template
debug!("[*]: Register handlebars template");
try!(handlebars.register_template_string("index", t.to_owned()));
try!(handlebars.register_template_string("index", theme.index.to_owned()));
// Register helpers
debug!("[*]: Register handlebars helpers");
@ -115,11 +115,11 @@ impl Renderer for HtmlHandlebars {
debug!("[*] Copy static files");
// JavaScript
let mut js_file = try!(File::create(config.dest().join("book.js")));
try!(js_file.write_all(theme::get_js()));
try!(js_file.write_all(&theme.js));
// Css
let mut css_file = try!(File::create(config.dest().join("book.css")));
try!(css_file.write_all(theme::get_css()));
try!(css_file.write_all(&theme.css));
Ok(())
}

View File

@ -2,3 +2,4 @@ pub use self::hbs_renderer::HtmlHandlebars;
mod hbs_renderer;
mod helpers;
mod theme;

View File

@ -0,0 +1,73 @@
use std::path::Path;
use std::fs::{File, metadata};
use std::io::Read;
use theme;
pub struct Theme {
pub index: String,
pub css: Vec<u8>,
pub js: Vec<u8>,
}
impl Theme {
pub fn new(src: &Path) -> Self{
// Default theme
let mut theme = Theme {
index: theme::get_index_hbs().to_owned(),
css: theme::get_css().to_owned(),
js: theme::get_js().to_owned(),
};
// Check if the given path exists
// Hacky way to check if the path exists... Until PathExt moves to stable
match metadata(&src) {
Err(_) => return theme,
Ok(f) => {
if !f.is_dir() {
return theme;
}
},
}
let src = src.join("theme");
// If src does exist, check if there is a theme directory in it
// Hacky way to check if the path exists... Until PathExt moves to stable
match metadata(&src) {
Err(_) => return theme,
Ok(f) => {
if !f.is_dir() {
return theme;
}
}
}
// Check for individual files if they exist
// index.hbs
match File::open(&src.join("index.hbs")) {
Ok(mut f) => {
f.read_to_string(&mut theme.index).unwrap();
},
_ => {},
}
// book.js
match File::open(&src.join("book.js")) {
Ok(mut f) => {
f.read_to_end(&mut theme.js).unwrap();
},
_ => {},
}
// book.css
match File::open(&src.join("book.css")) {
Ok(mut f) => {
f.read_to_end(&mut theme.css).unwrap();
},
_ => {},
}
theme
}
}