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-27 20:01:33 +08:00
|
|
|
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(),
|
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-06-27 20:01:33 +08:00
|
|
|
if !src.exists() || !src.is_dir() {
|
2016-03-18 05:31:28 +08:00
|
|
|
return theme;
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
2017-07-10 19:12:24 +08:00
|
|
|
// Check for individual files, if they exist copy them across
|
|
|
|
{
|
|
|
|
let files = vec![
|
|
|
|
(src.join("index.hbs"), &mut theme.index),
|
|
|
|
(src.join("book.js"), &mut theme.js),
|
|
|
|
(src.join("book.css"), &mut theme.css),
|
|
|
|
(src.join("favicon.png"), &mut theme.favicon),
|
|
|
|
(src.join("highlight.js"), &mut theme.highlight_js),
|
|
|
|
(src.join("clipboard.min.js"), &mut theme.clipboard_js),
|
|
|
|
(src.join("store.js"), &mut theme.store_js),
|
|
|
|
(src.join("highlight.css"), &mut theme.highlight_css),
|
|
|
|
(src.join("tomorrow-night.css"), &mut theme.tomorrow_night_css),
|
|
|
|
(src.join("ayu-highlight.css"), &mut theme.ayu_highlight_css),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (filename, dest) in files {
|
|
|
|
load_file_contents(filename, dest);
|
|
|
|
}
|
2017-06-01 03:51:19 +08:00
|
|
|
}
|
|
|
|
|
2017-07-10 19:12:24 +08:00
|
|
|
theme
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2017-07-10 19:12:24 +08:00
|
|
|
fn load_file_contents<P: AsRef<Path>>(filename: P, dest: &mut Vec<u8>) {
|
|
|
|
let filename = filename.as_ref();
|
2015-09-14 17:08:48 +08:00
|
|
|
|
2017-07-10 19:12:24 +08:00
|
|
|
if let Ok(mut f) = File::open(filename) {
|
|
|
|
dest.clear();
|
|
|
|
if let Err(e) = f.read_to_end(dest) {
|
|
|
|
warn!("Couldn't load custom file, {}: {}", filename.display(), e);
|
2017-06-07 04:35:44 +08:00
|
|
|
}
|
2015-08-06 04:35:26 +08:00
|
|
|
}
|
2017-07-10 19:12:24 +08:00
|
|
|
}
|