2015-08-06 04:35:26 +08:00
|
|
|
use std::path::Path;
|
2015-08-13 16:46:56 +08:00
|
|
|
use std::fs::File;
|
2015-08-06 04:35:26 +08:00
|
|
|
use std::io::Read;
|
2015-07-19 06:08:38 +08:00
|
|
|
|
2015-08-13 16:46:56 +08:00
|
|
|
use utils::{PathExt};
|
|
|
|
|
2015-08-11 22:13:41 +08:00
|
|
|
pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
|
|
|
|
pub static CSS: &'static [u8] = include_bytes!("book.css");
|
|
|
|
pub static JS: &'static [u8] = include_bytes!("book.js");
|
|
|
|
pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js");
|
|
|
|
pub static HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css");
|
2015-07-19 20:02:21 +08:00
|
|
|
|
2015-08-11 22:13:41 +08:00
|
|
|
/// The `Theme` struct should be used instead of the static variables because the `new()` method
|
|
|
|
/// will look if the user has a theme directory in his source folder and use the users theme instead
|
|
|
|
/// of the default.
|
|
|
|
///
|
|
|
|
/// You should exceptionnaly use the static variables only if you need the default theme even if the
|
|
|
|
/// user has specified another theme.
|
2015-08-06 04:35:26 +08:00
|
|
|
pub struct Theme {
|
2015-08-11 22:13:41 +08:00
|
|
|
pub index: Vec<u8>,
|
2015-08-06 04:35:26 +08:00
|
|
|
pub css: Vec<u8>,
|
|
|
|
pub js: Vec<u8>,
|
|
|
|
pub highlight_css: Vec<u8>,
|
|
|
|
pub highlight_js: Vec<u8>,
|
2015-07-19 20:02:21 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
impl Theme {
|
|
|
|
pub fn new(src: &Path) -> Self{
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
// 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
|
2015-08-13 16:46:56 +08:00
|
|
|
if !src.exists() || !src.is_dir() {
|
|
|
|
return theme
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let src = src.join("theme");
|
|
|
|
// If src does exist, check if there is a theme directory in it
|
2015-08-13 16:46:56 +08:00
|
|
|
if !src.exists() || !src.is_dir() {
|
|
|
|
return theme
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for individual files if they exist
|
|
|
|
|
|
|
|
// index.hbs
|
|
|
|
match File::open(&src.join("index.hbs")) {
|
|
|
|
Ok(mut f) => {
|
2015-08-11 22:13:41 +08:00
|
|
|
theme.index.clear(); // Reset the value, because read_to_string appends...
|
2015-08-12 04:55:51 +08:00
|
|
|
match f.read_to_end(&mut theme.index) {
|
|
|
|
_ => {}
|
|
|
|
};
|
2015-08-06 04:35:26 +08:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// book.js
|
|
|
|
match File::open(&src.join("book.js")) {
|
|
|
|
Ok(mut f) => {
|
|
|
|
theme.js.clear();
|
2015-08-12 04:55:51 +08:00
|
|
|
match f.read_to_end(&mut theme.js){
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-08-06 04:35:26 +08:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// book.css
|
|
|
|
match File::open(&src.join("book.css")) {
|
|
|
|
Ok(mut f) => {
|
|
|
|
theme.css.clear();
|
2015-08-12 04:55:51 +08:00
|
|
|
match f.read_to_end(&mut theme.css) {
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-08-06 04:35:26 +08:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// highlight.js
|
|
|
|
match File::open(&src.join("highlight.js")) {
|
|
|
|
Ok(mut f) => {
|
|
|
|
theme.highlight_js.clear();
|
2015-08-12 04:55:51 +08:00
|
|
|
match f.read_to_end(&mut theme.highlight_js) {
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-08-06 04:35:26 +08:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// highlight.css
|
|
|
|
match File::open(&src.join("highlight.css")) {
|
|
|
|
Ok(mut f) => {
|
|
|
|
theme.highlight_css.clear();
|
2015-08-12 04:55:51 +08:00
|
|
|
match f.read_to_end(&mut theme.highlight_css) {
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-08-06 04:35:26 +08:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
theme
|
|
|
|
}
|
2015-08-06 00:28:59 +08:00
|
|
|
}
|