diff --git a/Cargo.toml b/Cargo.toml index 5b564ec900..0f33435b67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,11 +20,12 @@ anyhow = "1.0.28" chrono = "0.4" clap = { version = "3.0", features = ["cargo"] } clap_complete = "3.0" -once_cell = "1" env_logger = "0.9.0" +gitignore = "1.0" handlebars = "4.0" log = "0.4" memchr = "2.0" +once_cell = "1" opener = "0.5" pulldown-cmark = { version = "0.9.1", default-features = false } regex = "1.5.5" @@ -37,7 +38,6 @@ topological-sort = "0.1.0" # Watch feature notify = { version = "4.0", optional = true } -gitignore = { version = "1.0", optional = true } # Serve feature futures-util = { version = "0.3.4", optional = true } @@ -58,7 +58,7 @@ walkdir = "2.0" [features] default = ["watch", "serve", "search"] -watch = ["notify", "gitignore"] +watch = ["notify"] serve = ["futures-util", "tokio", "warp"] search = ["elasticlunr-rs", "ammonia"] diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 710449af0d..c3dc2ea4b7 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -589,7 +589,28 @@ impl Renderer for HtmlHandlebars { .context("Unable to emit redirects")?; // Copy all remaining files, avoid a recursive copy from/to the book build dir - utils::fs::copy_files_except_ext(&src_dir, destination, true, Some(&build_dir), &["md"])?; + let mdbook_ignore = src_dir.join(".mdbookignore"); + let exclusion_checker = if mdbook_ignore.exists() { + match gitignore::File::new(&mdbook_ignore) { + Ok(exclusion_checker) => Some(exclusion_checker), + Err(err) => { + warn!("Unable to load '.mdbookignore' file: {}", err); + + None + } + } + } else { + None + }; + + utils::fs::copy_files_except_ext( + &src_dir, + destination, + true, + Some(&build_dir), + &["md"], + exclusion_checker.as_ref(), + )?; Ok(()) } diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 0d6f383746..0f68361082 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -95,6 +95,7 @@ pub fn copy_files_except_ext( recursive: bool, avoid_dir: Option<&PathBuf>, ext_blacklist: &[&str], + exclusion_checker: Option<&gitignore::File<'_>>, ) -> Result<()> { debug!( "Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}", @@ -128,6 +129,12 @@ pub fn copy_files_except_ext( } } + if let Some(exclusion_checker) = exclusion_checker { + if let Ok(true) = exclusion_checker.is_excluded(&entry.path()) { + continue; + } + } + // check if output dir already exists if !to.join(entry.file_name()).exists() { fs::create_dir(&to.join(entry.file_name()))?; @@ -139,6 +146,7 @@ pub fn copy_files_except_ext( true, avoid_dir, ext_blacklist, + exclusion_checker, )?; } else if metadata.is_file() { // Check if it is in the blacklist @@ -147,6 +155,13 @@ pub fn copy_files_except_ext( continue; } } + + if let Some(exclusion_checker) = exclusion_checker { + if let Ok(true) = exclusion_checker.is_excluded(&entry.path()) { + continue; + } + } + debug!( "creating path for file: {:?}", &to.join( @@ -247,9 +262,14 @@ mod tests { panic!("Could not create output/sub_dir_exists: {}", err); } - if let Err(e) = - copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"]) - { + if let Err(e) = copy_files_except_ext( + tmp.path(), + &tmp.path().join("output"), + true, + None, + &["md"], + None, + ) { panic!("Error while executing the function:\n{:?}", e); }