Checked the API, moved handlebars theme handling to the theme module to expose it to every renderer. Closes #28

This commit is contained in:
Mathieu David 2015-08-05 22:35:26 +02:00
parent f222134ee6
commit 01369ea42f
9 changed files with 103 additions and 123 deletions

View File

@ -10,7 +10,7 @@ pub struct BookConfig {
pub author: String, pub author: String,
dest: PathBuf, dest: PathBuf,
src: PathBuf, src: PathBuf,
indent_spaces: i32, pub indent_spaces: i32,
multilingual: bool, multilingual: bool,
} }

View File

@ -32,7 +32,7 @@ impl BookItem {
} }
} }
pub fn spacer() -> Self { fn _spacer() -> Self {
BookItem { BookItem {
name: String::from("SPACER"), name: String::from("SPACER"),
path: PathBuf::new(), path: PathBuf::new(),

View File

@ -11,7 +11,7 @@ use renderer::HtmlHandlebars;
pub struct MDBook { pub struct MDBook {
config: BookConfig, config: BookConfig,
root: PathBuf, pub root: PathBuf,
pub content: Vec<BookItem>, pub content: Vec<BookItem>,
renderer: Box<Renderer>, renderer: Box<Renderer>,
} }

View File

@ -9,3 +9,4 @@ pub mod utils;
pub use book::MDBook; pub use book::MDBook;
pub use book::BookItem; pub use book::BookItem;
pub use book::BookConfig; pub use book::BookConfig;
pub use renderer::Renderer;

View File

@ -2,10 +2,10 @@ extern crate handlebars;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate pulldown_cmark; extern crate pulldown_cmark;
use renderer::html_handlebars::{helpers, theme}; use renderer::html_handlebars::helpers;
use renderer::Renderer; use renderer::Renderer;
use book::{BookItems, BookConfig}; use book::{BookItems, BookConfig};
use utils; use {utils, theme};
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::{self, File}; use std::fs::{self, File};

View File

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

View File

@ -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<u8>,
pub js: Vec<u8>,
pub highlight_css: Vec<u8>,
pub highlight_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(),
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
}
}

View File

@ -2,4 +2,4 @@ pub use self::renderer::Renderer;
pub use self::html_handlebars::HtmlHandlebars; pub use self::html_handlebars::HtmlHandlebars;
pub mod renderer; pub mod renderer;
pub mod html_handlebars; mod html_handlebars;

View File

@ -1,25 +1,103 @@
use std::path::Path;
use std::fs::{File, metadata};
use std::io::Read;
pub fn get_index_hbs() -> &'static str { static INDEX: &'static str = include_str!("index.hbs");
let index = include_str!("index.hbs"); static CSS: &'static [u8] = include_bytes!("book.css");
index 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<u8>,
pub js: Vec<u8>,
pub highlight_css: Vec<u8>,
pub highlight_js: Vec<u8>,
} }
pub fn get_css() -> &'static [u8] { impl Theme {
let css = include_bytes!("book.css"); pub fn new(src: &Path) -> Self{
css
// 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(),
};
// 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_js() -> &'static [u8] { let src = src.join("theme");
let js = include_bytes!("book.js"); // If src does exist, check if there is a theme directory in it
js // 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_js() -> &'static [u8] { // Check for individual files if they exist
let highlight_js = include_bytes!("highlight.js");
highlight_js // 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();
},
_ => {},
} }
pub fn get_highlight_css() -> &'static [u8] { // book.js
let highlight_css = include_bytes!("highlight.css"); match File::open(&src.join("book.js")) {
highlight_css 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
}
} }