2017-06-23 18:57:58 +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
|
|
|
|
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");
|
2016-02-23 00:17:07 +08:00
|
|
|
pub static FAVICON: &'static [u8] = include_bytes!("favicon.png");
|
2015-08-11 22:13:41 +08:00
|
|
|
pub static JS: &'static [u8] = include_bytes!("book.js");
|
|
|
|
pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js");
|
2015-09-14 17:08:48 +08:00
|
|
|
pub static TOMORROW_NIGHT_CSS: &'static [u8] = include_bytes!("tomorrow-night.css");
|
2015-08-11 22:13:41 +08:00
|
|
|
pub static HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css");
|
2017-06-07 04:35:44 +08:00
|
|
|
pub static AYU_HIGHLIGHT_CSS: &'static [u8] = include_bytes!("ayu-highlight.css");
|
2015-09-14 01:16:11 +08:00
|
|
|
pub static JQUERY: &'static [u8] = include_bytes!("jquery-2.1.4.min.js");
|
2017-06-01 03:51:19 +08:00
|
|
|
pub static CLIPBOARD_JS: &'static [u8] = include_bytes!("clipboard.min.js");
|
2017-06-12 03:54:09 +08:00
|
|
|
pub static STORE_JS: &'static [u8] = include_bytes!("store.js");
|
2015-09-14 02:03:34 +08:00
|
|
|
pub static FONT_AWESOME: &'static [u8] = include_bytes!("_FontAwesome/css/font-awesome.min.css");
|
|
|
|
pub static FONT_AWESOME_EOT: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.eot");
|
|
|
|
pub static FONT_AWESOME_SVG: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.svg");
|
|
|
|
pub static FONT_AWESOME_TTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.ttf");
|
|
|
|
pub static FONT_AWESOME_WOFF: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.woff");
|
|
|
|
pub static FONT_AWESOME_WOFF2: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.woff2");
|
|
|
|
pub static FONT_AWESOME_OTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/FontAwesome.otf");
|
2015-07-19 20:02:21 +08:00
|
|
|
|
2017-05-19 19:04:37 +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
|
2015-08-11 22:13:41 +08:00
|
|
|
/// of the default.
|
|
|
|
///
|
2017-05-19 19:04:37 +08:00
|
|
|
/// You should exceptionnaly use the static variables only if you need the
|
|
|
|
/// default theme even if the
|
2015-08-11 22:13:41 +08:00
|
|
|
/// 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>,
|
2016-02-23 00:17:07 +08:00
|
|
|
pub favicon: Vec<u8>,
|
2015-08-06 04:35:26 +08:00
|
|
|
pub js: Vec<u8>,
|
|
|
|
pub highlight_css: Vec<u8>,
|
2015-09-14 17:08:48 +08:00
|
|
|
pub tomorrow_night_css: Vec<u8>,
|
2017-06-07 04:35:44 +08:00
|
|
|
pub ayu_highlight_css: Vec<u8>,
|
2015-08-06 04:35:26 +08:00
|
|
|
pub highlight_js: Vec<u8>,
|
2017-06-01 03:51:19 +08:00
|
|
|
pub clipboard_js: Vec<u8>,
|
2017-06-12 03:54:09 +08:00
|
|
|
pub store_js: Vec<u8>,
|
2015-09-14 01:16:11 +08:00
|
|
|
pub jquery: Vec<u8>,
|
2015-07-19 20:02:21 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
impl Theme {
|
2017-06-23 18:57:58 +08:00
|
|
|
pub fn new(src: Option<&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(),
|
2016-02-23 00:17:07 +08:00
|
|
|
favicon: FAVICON.to_owned(),
|
2015-08-06 04:35:26 +08:00
|
|
|
js: JS.to_owned(),
|
|
|
|
highlight_css: HIGHLIGHT_CSS.to_owned(),
|
2015-09-14 17:08:48 +08:00
|
|
|
tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(),
|
2017-06-07 04:35:44 +08:00
|
|
|
ayu_highlight_css: AYU_HIGHLIGHT_CSS.to_owned(),
|
2015-08-06 04:35:26 +08:00
|
|
|
highlight_js: HIGHLIGHT_JS.to_owned(),
|
2017-06-01 03:51:19 +08:00
|
|
|
clipboard_js: CLIPBOARD_JS.to_owned(),
|
2017-06-12 03:54:09 +08:00
|
|
|
store_js: STORE_JS.to_owned(),
|
2015-09-14 01:16:11 +08:00
|
|
|
jquery: JQUERY.to_owned(),
|
2015-08-06 04:35:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the given path exists
|
2017-05-19 06:56:37 +08:00
|
|
|
if src.is_none() || !src.unwrap().exists() || !src.unwrap().is_dir() {
|
2016-03-18 05:31:28 +08:00
|
|
|
return theme;
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 06:56:37 +08:00
|
|
|
let src = src.unwrap();
|
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
// Check for individual files if they exist
|
|
|
|
|
|
|
|
// index.hbs
|
2015-09-13 10:51:28 +08:00
|
|
|
if let Ok(mut f) = File::open(&src.join("index.hbs")) {
|
|
|
|
theme.index.clear(); // Reset the value, because read_to_string appends...
|
|
|
|
let _ = f.read_to_end(&mut theme.index);
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// book.js
|
2015-09-13 10:51:28 +08:00
|
|
|
if let Ok(mut f) = File::open(&src.join("book.js")) {
|
|
|
|
theme.js.clear();
|
2016-03-18 05:31:28 +08:00
|
|
|
let _ = f.read_to_end(&mut theme.js);
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// book.css
|
2015-09-13 10:51:28 +08:00
|
|
|
if let Ok(mut f) = File::open(&src.join("book.css")) {
|
|
|
|
theme.css.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.css);
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 00:17:07 +08:00
|
|
|
// favicon.png
|
|
|
|
if let Ok(mut f) = File::open(&src.join("favicon.png")) {
|
|
|
|
theme.favicon.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.favicon);
|
|
|
|
}
|
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
// highlight.js
|
2015-09-13 10:51:28 +08:00
|
|
|
if let Ok(mut f) = File::open(&src.join("highlight.js")) {
|
|
|
|
theme.highlight_js.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.highlight_js);
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
2017-06-01 03:51:19 +08:00
|
|
|
// clipboard.js
|
|
|
|
if let Ok(mut f) = File::open(&src.join("clipboard.min.js")) {
|
|
|
|
theme.clipboard_js.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.clipboard_js);
|
|
|
|
}
|
|
|
|
|
2017-06-12 03:54:09 +08:00
|
|
|
// store.js
|
|
|
|
if let Ok(mut f) = File::open(&src.join("store.js")) {
|
|
|
|
theme.store_js.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.store_js);
|
|
|
|
}
|
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
// highlight.css
|
2015-09-13 10:51:28 +08:00
|
|
|
if let Ok(mut f) = File::open(&src.join("highlight.css")) {
|
|
|
|
theme.highlight_css.clear();
|
|
|
|
let _ = 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-09-14 17:08:48 +08:00
|
|
|
// tomorrow-night.css
|
|
|
|
if let Ok(mut f) = File::open(&src.join("tomorrow-night.css")) {
|
|
|
|
theme.tomorrow_night_css.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.tomorrow_night_css);
|
|
|
|
}
|
|
|
|
|
2017-06-07 04:35:44 +08:00
|
|
|
// ayu-highlight.css
|
|
|
|
if let Ok(mut f) = File::open(&src.join("ayu-highlight.css")) {
|
|
|
|
theme.ayu_highlight_css.clear();
|
|
|
|
let _ = f.read_to_end(&mut theme.ayu_highlight_css);
|
|
|
|
}
|
|
|
|
|
2015-08-06 04:35:26 +08:00
|
|
|
theme
|
|
|
|
}
|
2015-08-06 00:28:59 +08:00
|
|
|
}
|