Skip to content

Commit 7f6adc7

Browse files
committed
Ignore files listed in .mdbookignore during build
1 parent f8df8ed commit 7f6adc7

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ anyhow = "1.0.28"
2020
chrono = "0.4"
2121
clap = { version = "3.0", features = ["cargo"] }
2222
clap_complete = "3.0"
23-
once_cell = "1"
2423
env_logger = "0.9.0"
24+
gitignore = "1.0"
2525
handlebars = "4.0"
2626
log = "0.4"
2727
memchr = "2.0"
28+
once_cell = "1"
2829
opener = "0.5"
2930
pulldown-cmark = { version = "0.9.1", default-features = false }
3031
regex = "1.5.5"
@@ -37,7 +38,6 @@ topological-sort = "0.1.0"
3738

3839
# Watch feature
3940
notify = { version = "4.0", optional = true }
40-
gitignore = { version = "1.0", optional = true }
4141

4242
# Serve feature
4343
futures-util = { version = "0.3.4", optional = true }
@@ -58,7 +58,7 @@ walkdir = "2.0"
5858

5959
[features]
6060
default = ["watch", "serve", "search"]
61-
watch = ["notify", "gitignore"]
61+
watch = ["notify"]
6262
serve = ["futures-util", "tokio", "warp"]
6363
search = ["elasticlunr-rs", "ammonia"]
6464

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,28 @@ impl Renderer for HtmlHandlebars {
589589
.context("Unable to emit redirects")?;
590590

591591
// Copy all remaining files, avoid a recursive copy from/to the book build dir
592-
utils::fs::copy_files_except_ext(&src_dir, destination, true, Some(&build_dir), &["md"])?;
592+
let mdbook_ignore = src_dir.join(".mdbookignore");
593+
let exclusion_checker = if mdbook_ignore.exists() {
594+
match gitignore::File::new(&mdbook_ignore) {
595+
Ok(exclusion_checker) => Some(exclusion_checker),
596+
Err(err) => {
597+
warn!("Unable to load '.mdbookignore' file: {}", err);
598+
599+
None
600+
}
601+
}
602+
} else {
603+
None
604+
};
605+
606+
utils::fs::copy_files_except_ext(
607+
&src_dir,
608+
destination,
609+
true,
610+
Some(&build_dir),
611+
&["md"],
612+
exclusion_checker.as_ref(),
613+
)?;
593614

594615
Ok(())
595616
}

src/utils/fs.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub fn copy_files_except_ext(
9595
recursive: bool,
9696
avoid_dir: Option<&PathBuf>,
9797
ext_blacklist: &[&str],
98+
exclusion_checker: Option<&gitignore::File<'_>>,
9899
) -> Result<()> {
99100
debug!(
100101
"Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}",
@@ -139,6 +140,7 @@ pub fn copy_files_except_ext(
139140
true,
140141
avoid_dir,
141142
ext_blacklist,
143+
exclusion_checker,
142144
)?;
143145
} else if metadata.is_file() {
144146
// Check if it is in the blacklist
@@ -147,6 +149,13 @@ pub fn copy_files_except_ext(
147149
continue;
148150
}
149151
}
152+
153+
if let Some(exclusion_checker) = exclusion_checker {
154+
if let Ok(true) = exclusion_checker.is_excluded(&entry.path()) {
155+
continue;
156+
}
157+
}
158+
150159
debug!(
151160
"creating path for file: {:?}",
152161
&to.join(
@@ -247,9 +256,14 @@ mod tests {
247256
panic!("Could not create output/sub_dir_exists: {}", err);
248257
}
249258

250-
if let Err(e) =
251-
copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"])
252-
{
259+
if let Err(e) = copy_files_except_ext(
260+
tmp.path(),
261+
&tmp.path().join("output"),
262+
true,
263+
None,
264+
&["md"],
265+
None,
266+
) {
253267
panic!("Error while executing the function:\n{:?}", e);
254268
}
255269

0 commit comments

Comments
 (0)