From 2c22d11bfddb1d1baf78622e95b8d3efd76b28c0 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 4 Aug 2015 17:58:09 +0200 Subject: [PATCH] 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 --- src/renderer/html_handlebars/hbs_renderer.rs | 14 ++-- src/renderer/html_handlebars/mod.rs | 1 + src/renderer/html_handlebars/theme.rs | 73 ++++++++++++++++++++ 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 src/renderer/html_handlebars/theme.rs diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 093ee1bf..baec2ffd 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -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(()) } diff --git a/src/renderer/html_handlebars/mod.rs b/src/renderer/html_handlebars/mod.rs index f1df6d8d..399fa523 100644 --- a/src/renderer/html_handlebars/mod.rs +++ b/src/renderer/html_handlebars/mod.rs @@ -2,3 +2,4 @@ pub use self::hbs_renderer::HtmlHandlebars; mod hbs_renderer; mod helpers; +mod theme; diff --git a/src/renderer/html_handlebars/theme.rs b/src/renderer/html_handlebars/theme.rs new file mode 100644 index 00000000..ab72b4ba --- /dev/null +++ b/src/renderer/html_handlebars/theme.rs @@ -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, + pub js: Vec, +} + +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 + } +}