From 01369ea42f00f38643f33f1b05accd02e45ac283 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Wed, 5 Aug 2015 22:35:26 +0200 Subject: [PATCH] Checked the API, moved handlebars theme handling to the theme module to expose it to every renderer. Closes #28 --- src/book/bookconfig.rs | 2 +- src/book/bookitem.rs | 2 +- src/book/mdbook.rs | 2 +- src/lib.rs | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 4 +- src/renderer/html_handlebars/mod.rs | 1 - src/renderer/html_handlebars/theme.rs | 98 ---------------- src/renderer/mod.rs | 2 +- src/theme/mod.rs | 114 ++++++++++++++++--- 9 files changed, 103 insertions(+), 123 deletions(-) delete mode 100644 src/renderer/html_handlebars/theme.rs diff --git a/src/book/bookconfig.rs b/src/book/bookconfig.rs index d40fbaca..5dace266 100644 --- a/src/book/bookconfig.rs +++ b/src/book/bookconfig.rs @@ -10,7 +10,7 @@ pub struct BookConfig { pub author: String, dest: PathBuf, src: PathBuf, - indent_spaces: i32, + pub indent_spaces: i32, multilingual: bool, } diff --git a/src/book/bookitem.rs b/src/book/bookitem.rs index 232d9ad9..117e913e 100644 --- a/src/book/bookitem.rs +++ b/src/book/bookitem.rs @@ -32,7 +32,7 @@ impl BookItem { } } - pub fn spacer() -> Self { + fn _spacer() -> Self { BookItem { name: String::from("SPACER"), path: PathBuf::new(), diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index 9c0ba7b6..eb549e39 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -11,7 +11,7 @@ use renderer::HtmlHandlebars; pub struct MDBook { config: BookConfig, - root: PathBuf, + pub root: PathBuf, pub content: Vec, renderer: Box, } diff --git a/src/lib.rs b/src/lib.rs index 8cd77a85..b6834d2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,3 +9,4 @@ pub mod utils; pub use book::MDBook; pub use book::BookItem; pub use book::BookConfig; +pub use renderer::Renderer; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 91e62a7a..3079e168 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, theme}; +use renderer::html_handlebars::helpers; use renderer::Renderer; use book::{BookItems, BookConfig}; -use utils; +use {utils, theme}; use std::path::PathBuf; use std::fs::{self, File}; diff --git a/src/renderer/html_handlebars/mod.rs b/src/renderer/html_handlebars/mod.rs index 399fa523..f1df6d8d 100644 --- a/src/renderer/html_handlebars/mod.rs +++ b/src/renderer/html_handlebars/mod.rs @@ -2,4 +2,3 @@ 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 deleted file mode 100644 index b773930e..00000000 --- a/src/renderer/html_handlebars/theme.rs +++ /dev/null @@ -1,98 +0,0 @@ -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, - pub highlight_css: Vec, - pub highlight_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(), - highlight_css: theme::get_highlight_css().to_owned(), - highlight_js: theme::get_highlight_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) => { - theme.index = String::new(); // Reset the value, because read_to_string appends... - f.read_to_string(&mut theme.index).unwrap(); - }, - _ => {}, - } - - // book.js - match File::open(&src.join("book.js")) { - Ok(mut f) => { - theme.js.clear(); - f.read_to_end(&mut theme.js).unwrap(); - }, - _ => {}, - } - - // book.css - match File::open(&src.join("book.css")) { - Ok(mut f) => { - theme.css.clear(); - f.read_to_end(&mut theme.css).unwrap(); - }, - _ => {}, - } - - // highlight.js - match File::open(&src.join("highlight.js")) { - Ok(mut f) => { - theme.highlight_js.clear(); - f.read_to_end(&mut theme.highlight_js).unwrap(); - }, - _ => {}, - } - - // highlight.css - match File::open(&src.join("highlight.css")) { - Ok(mut f) => { - theme.highlight_css.clear(); - f.read_to_end(&mut theme.highlight_css).unwrap(); - }, - _ => {}, - } - - theme - } -} diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 1fbb507f..8216db27 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -2,4 +2,4 @@ pub use self::renderer::Renderer; pub use self::html_handlebars::HtmlHandlebars; pub mod renderer; -pub mod html_handlebars; +mod html_handlebars; diff --git a/src/theme/mod.rs b/src/theme/mod.rs index a3afc22a..0a3d0f85 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -1,25 +1,103 @@ +use std::path::Path; +use std::fs::{File, metadata}; +use std::io::Read; -pub fn get_index_hbs() -> &'static str { - let index = include_str!("index.hbs"); - index +static INDEX: &'static str = include_str!("index.hbs"); +static CSS: &'static [u8] = include_bytes!("book.css"); +static JS: &'static [u8] = include_bytes!("book.js"); +static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js"); +static HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css"); + +pub struct Theme { + pub index: String, + pub css: Vec, + pub js: Vec, + pub highlight_css: Vec, + pub highlight_js: Vec, } -pub fn get_css() -> &'static [u8] { - let css = include_bytes!("book.css"); - css -} +impl Theme { + pub fn new(src: &Path) -> Self{ -pub fn get_js() -> &'static [u8] { - let js = include_bytes!("book.js"); - js -} + // Default theme + let mut theme = Theme { + index: INDEX.to_owned(), + css: CSS.to_owned(), + js: JS.to_owned(), + highlight_css: HIGHLIGHT_CSS.to_owned(), + highlight_js: HIGHLIGHT_JS.to_owned(), + }; -pub fn get_highlight_js() -> &'static [u8] { - let highlight_js = include_bytes!("highlight.js"); - highlight_js -} + // 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; + } + }, + } -pub fn get_highlight_css() -> &'static [u8] { - let highlight_css = include_bytes!("highlight.css"); - highlight_css + 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) => { + theme.index = String::new(); // Reset the value, because read_to_string appends... + f.read_to_string(&mut theme.index).unwrap(); + }, + _ => {}, + } + + // book.js + match File::open(&src.join("book.js")) { + Ok(mut f) => { + theme.js.clear(); + f.read_to_end(&mut theme.js).unwrap(); + }, + _ => {}, + } + + // book.css + match File::open(&src.join("book.css")) { + Ok(mut f) => { + theme.css.clear(); + f.read_to_end(&mut theme.css).unwrap(); + }, + _ => {}, + } + + // highlight.js + match File::open(&src.join("highlight.js")) { + Ok(mut f) => { + theme.highlight_js.clear(); + f.read_to_end(&mut theme.highlight_js).unwrap(); + }, + _ => {}, + } + + // highlight.css + match File::open(&src.join("highlight.css")) { + Ok(mut f) => { + theme.highlight_css.clear(); + f.read_to_end(&mut theme.highlight_css).unwrap(); + }, + _ => {}, + } + + theme + } }