From ac38f05bb6c81c773fff2e60f11c4133a76ffdc5 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Wed, 25 Jul 2018 15:51:09 -0500 Subject: [PATCH] Change template to use new CSS --- src/book/init.rs | 16 +++++- src/renderer/html_handlebars/hbs_renderer.rs | 5 +- src/theme/index.hbs | 18 +++--- src/theme/mod.rs | 60 ++++++++++++++------ 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/src/book/init.rs b/src/book/init.rs index ccae8e9a..5347759a 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -127,8 +127,20 @@ impl BookBuilder { let mut index = File::create(themedir.join("index.hbs"))?; index.write_all(theme::INDEX)?; - let mut css = File::create(themedir.join("book.css"))?; - css.write_all(theme::CSS)?; + let cssdir = themedir.join("css"); + fs::create_dir(&cssdir)?; + + let mut general_css = File::create(cssdir.join("general.css"))?; + general_css.write_all(theme::GENERAL_CSS)?; + + let mut chrome_css = File::create(cssdir.join("chrome.css"))?; + chrome_css.write_all(theme::CHROME_CSS)?; + + let mut print_css = File::create(cssdir.join("print.css"))?; + print_css.write_all(theme::PRINT_CSS)?; + + let mut variables_css = File::create(cssdir.join("variables.css"))?; + variables_css.write_all(theme::VARIABLES_CSS)?; let mut favicon = File::create(themedir.join("favicon.png"))?; favicon.write_all(theme::FAVICON)?; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 2367446f..469f8ef4 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -139,7 +139,10 @@ impl HtmlHandlebars { )?; write_file(destination, "book.js", &theme.js)?; - write_file(destination, "book.css", &theme.css)?; + write_file(destination, "css/general.css", &theme.general_css)?; + write_file(destination, "css/chrome.css", &theme.chrome_css)?; + write_file(destination, "css/print.css", &theme.print_css)?; + write_file(destination, "css/variables.css", &theme.variables_css)?; write_file(destination, "favicon.png", &theme.favicon)?; write_file(destination, "highlight.css", &theme.highlight_css)?; write_file(destination, "tomorrow-night.css", &theme.tomorrow_night_css)?; diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 45be723a..36d47754 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -9,29 +9,31 @@ - + + + + + + + + - - - - - + {{#each additional_css}} - + {{/each}} {{#if mathjax_support}} {{/if}} - diff --git a/src/theme/mod.rs b/src/theme/mod.rs index 5ebbc57f..37d373ef 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -13,7 +13,10 @@ use errors::*; pub static INDEX: &'static [u8] = include_bytes!("index.hbs"); pub static HEADER: &'static [u8] = include_bytes!("header.hbs"); -pub static CSS: &'static [u8] = include_bytes!("book.css"); +pub static CHROME_CSS: &'static [u8] = include_bytes!("css/chrome.css"); +pub static GENERAL_CSS: &'static [u8] = include_bytes!("css/general.css"); +pub static PRINT_CSS: &'static [u8] = include_bytes!("css/print.css"); +pub static VARIABLES_CSS: &'static [u8] = include_bytes!("css/variables.css"); pub static FAVICON: &'static [u8] = include_bytes!("favicon.png"); pub static JS: &'static [u8] = include_bytes!("book.js"); pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js"); @@ -44,7 +47,10 @@ pub static FONT_AWESOME_OTF: &'static [u8] = include_bytes!("FontAwesome/fonts/F pub struct Theme { pub index: Vec, pub header: Vec, - pub css: Vec, + pub chrome_css: Vec, + pub general_css: Vec, + pub print_css: Vec, + pub variables_css: Vec, pub favicon: Vec, pub js: Vec, pub highlight_css: Vec, @@ -72,7 +78,13 @@ impl Theme { (theme_dir.join("index.hbs"), &mut theme.index), (theme_dir.join("header.hbs"), &mut theme.header), (theme_dir.join("book.js"), &mut theme.js), - (theme_dir.join("book.css"), &mut theme.css), + (theme_dir.join("css/chrome.css"), &mut theme.chrome_css), + (theme_dir.join("css/general.css"), &mut theme.general_css), + (theme_dir.join("css/print.css"), &mut theme.print_css), + ( + theme_dir.join("css/variables.css"), + &mut theme.variables_css, + ), (theme_dir.join("favicon.png"), &mut theme.favicon), (theme_dir.join("highlight.js"), &mut theme.highlight_js), (theme_dir.join("clipboard.min.js"), &mut theme.clipboard_js), @@ -107,7 +119,10 @@ impl Default for Theme { Theme { index: INDEX.to_owned(), header: HEADER.to_owned(), - css: CSS.to_owned(), + chrome_css: CHROME_CSS.to_owned(), + general_css: GENERAL_CSS.to_owned(), + print_css: PRINT_CSS.to_owned(), + variables_css: VARIABLES_CSS.to_owned(), favicon: FAVICON.to_owned(), js: JS.to_owned(), highlight_css: HIGHLIGHT_CSS.to_owned(), @@ -138,6 +153,7 @@ fn load_file_contents>(filename: P, dest: &mut Vec) -> Result #[cfg(test)] mod tests { use super::*; + use std::fs; use std::path::PathBuf; use tempfile::Builder as TempFileBuilder; @@ -154,21 +170,28 @@ mod tests { #[test] fn theme_dir_overrides_defaults() { - // Get all the non-Rust files in the theme directory - let special_files = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("src/theme") - .read_dir() - .unwrap() - .filter_map(|f| f.ok()) - .map(|f| f.path()) - .filter(|p| p.is_file() && !p.ends_with(".rs")); + let files = [ + "index.hbs", + "header.hbs", + "favicon.png", + "css/chrome.css", + "css/general.css", + "css/print.css", + "css/variables.css", + "book.js", + "highlight.js", + "tomorrow-night.css", + "highlight.css", + "ayu-highlight.css", + "clipboard.min.js", + ]; - let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap(); + let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap(); + fs::create_dir(temp.path().join("css")).unwrap(); // "touch" all of the special files so we have empty copies - for special_file in special_files { - let filename = temp.path().join(special_file.file_name().unwrap()); - let _ = File::create(&filename); + for file in &files { + File::create(&temp.path().join(file)).unwrap(); } let got = Theme::new(temp.path()); @@ -176,7 +199,10 @@ mod tests { let empty = Theme { index: Vec::new(), header: Vec::new(), - css: Vec::new(), + chrome_css: Vec::new(), + general_css: Vec::new(), + print_css: Vec::new(), + variables_css: Vec::new(), favicon: Vec::new(), js: Vec::new(), highlight_css: Vec::new(),