From 77e7cfd22bda843e00f851c4cf8a2eed5bd190f0 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 6 Jan 2021 16:29:38 -0800 Subject: [PATCH] Change `init --theme` to place theme in root. (#1432) --- src/book/init.rs | 9 ++++----- src/cmd/init.rs | 12 ++++-------- tests/init.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/book/init.rs b/src/book/init.rs index ea1911db..a1043c7f 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -110,10 +110,7 @@ impl BookBuilder { debug!("Copying theme"); let html_config = self.config.html_config().unwrap_or_default(); - let themedir = html_config - .theme - .unwrap_or_else(|| self.config.book.src.join("theme")); - let themedir = self.root.join(themedir); + let themedir = html_config.theme_dir(&self.root); if !themedir.exists() { debug!( @@ -127,7 +124,9 @@ impl BookBuilder { index.write_all(theme::INDEX)?; let cssdir = themedir.join("css"); - fs::create_dir(&cssdir)?; + if !cssdir.exists() { + fs::create_dir(&cssdir)?; + } let mut general_css = File::create(cssdir.join("general.css"))?; general_css.write_all(theme::GENERAL_CSS)?; diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 1a49f0d3..cccc1671 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -28,15 +28,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> { // If flag `--theme` is present, copy theme to src if args.is_present("theme") { - config.set("output.html.theme", "src/theme")?; + let theme_dir = book_dir.join("theme"); + println!(); + println!("Copying the default theme to {}", theme_dir.display()); // Skip this if `--force` is present - if !args.is_present("force") { - // Print warning - println!(); - println!( - "Copying the default theme to {}", - builder.config().book.src.display() - ); + if !args.is_present("force") && theme_dir.exists() { println!("This could potentially overwrite files already present in that directory."); print!("\nAre you sure you want to continue? (y/n) "); diff --git a/tests/init.rs b/tests/init.rs index 92dc91c7..4deb8401 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -108,3 +108,37 @@ fn book_toml_isnt_required() { md.build().unwrap(); } + +#[test] +fn copy_theme() { + let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap(); + MDBook::init(temp.path()).copy_theme(true).build().unwrap(); + let expected = vec![ + "book.js", + "css/chrome.css", + "css/general.css", + "css/print.css", + "css/variables.css", + "favicon.png", + "favicon.svg", + "highlight.css", + "highlight.js", + "index.hbs", + ]; + let theme_dir = temp.path().join("theme"); + let mut actual: Vec<_> = walkdir::WalkDir::new(&theme_dir) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| !e.file_type().is_dir()) + .map(|e| { + e.path() + .strip_prefix(&theme_dir) + .unwrap() + .to_str() + .unwrap() + .replace('\\', "/") + }) + .collect(); + actual.sort(); + assert_eq!(actual, expected); +}