summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Huss <eric@huss.org>2021-01-06 16:29:38 -0800
committerGitHub <noreply@github.com>2021-01-07 01:29:38 +0100
commit77e7cfd22bda843e00f851c4cf8a2eed5bd190f0 (patch)
tree781132b8de2bf1ecd50929b918aceaf61613b56c
parent76cd39e5e28aab7de37b5d4ed9b189f5491c3313 (diff)
Change `init --theme` to place theme in root. (#1432)
-rw-r--r--src/book/init.rs9
-rw-r--r--src/cmd/init.rs12
-rw-r--r--tests/init.rs34
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);
+}