Skip to content

Commit

Permalink
Ignore files listed in .mdbookignore during build
Browse files Browse the repository at this point in the history
  • Loading branch information
Bergmann89 committed Oct 13, 2022
1 parent f8df8ed commit 92b89b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }
Expand All @@ -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"]

Expand Down
23 changes: 22 additions & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
26 changes: 23 additions & 3 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {:?}",
Expand Down Expand Up @@ -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()))?;
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 92b89b5

Please sign in to comment.