Skip to content

Commit 08466df

Browse files
committed
Ignore files listed in .mdbookignore during build
1 parent c608cce commit 08466df

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
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+
ignore = "0.4"
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-
ignore = { version = "0.4", 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", "ignore"]
61+
watch = ["notify"]
6262
serve = ["futures-util", "tokio", "warp"]
6363
search = ["elasticlunr-rs", "ammonia"]
6464

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
1414

1515
use crate::utils::fs::get_404_output_file;
1616
use handlebars::Handlebars;
17+
use ignore::gitignore::GitignoreBuilder;
1718
use log::{debug, trace, warn};
1819
use once_cell::sync::Lazy;
1920
use regex::{Captures, Regex};
@@ -589,7 +590,23 @@ impl Renderer for HtmlHandlebars {
589590
.context("Unable to emit redirects")?;
590591

591592
// 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"])?;
593+
let mut builder = GitignoreBuilder::new(&src_dir);
594+
let mdbook_ignore = src_dir.join(".mdbookignore");
595+
if mdbook_ignore.exists() {
596+
if let Some(err) = builder.add(mdbook_ignore) {
597+
warn!("Unable to load '.mdbookignore' file: {}", err);
598+
}
599+
}
600+
builder.add_line(None, "*.md")?;
601+
let ignore = builder.build()?;
602+
603+
utils::fs::copy_files_except_ext(
604+
&src_dir,
605+
destination,
606+
true,
607+
Some(&build_dir),
608+
Some(&ignore),
609+
)?;
593610

594611
Ok(())
595612
}

src/utils/fs.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::errors::*;
2+
use ignore::gitignore::Gitignore;
23
use log::{debug, trace};
34
use std::convert::Into;
45
use std::fs::{self, File};
@@ -94,13 +95,13 @@ pub fn copy_files_except_ext(
9495
to: &Path,
9596
recursive: bool,
9697
avoid_dir: Option<&PathBuf>,
97-
ext_blacklist: &[&str],
98+
ignore: Option<&Gitignore>,
9899
) -> Result<()> {
99100
debug!(
100101
"Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}",
101102
from.display(),
102103
to.display(),
103-
ext_blacklist,
104+
ignore,
104105
avoid_dir
105106
);
106107

@@ -128,6 +129,13 @@ pub fn copy_files_except_ext(
128129
}
129130
}
130131

132+
if let Some(ignore) = ignore {
133+
let path = entry.path();
134+
if ignore.matched(&path, path.is_dir()).is_ignore() {
135+
continue;
136+
}
137+
}
138+
131139
// check if output dir already exists
132140
if !to.join(entry.file_name()).exists() {
133141
fs::create_dir(&to.join(entry.file_name()))?;
@@ -138,15 +146,17 @@ pub fn copy_files_except_ext(
138146
&to.join(entry.file_name()),
139147
true,
140148
avoid_dir,
141-
ext_blacklist,
149+
ignore,
142150
)?;
143151
} else if metadata.is_file() {
144152
// Check if it is in the blacklist
145-
if let Some(ext) = entry.path().extension() {
146-
if ext_blacklist.contains(&ext.to_str().unwrap()) {
153+
if let Some(ignore) = ignore {
154+
let path = entry.path();
155+
if ignore.matched(&path, path.is_dir()).is_ignore() {
147156
continue;
148157
}
149158
}
159+
150160
debug!(
151161
"creating path for file: {:?}",
152162
&to.join(
@@ -191,6 +201,7 @@ pub fn get_404_output_file(input_404: &Option<String>) -> String {
191201
#[cfg(test)]
192202
mod tests {
193203
use super::copy_files_except_ext;
204+
use ignore::gitignore::GitignoreBuilder;
194205
use std::{fs, io::Result, path::Path};
195206

196207
#[cfg(target_os = "windows")]
@@ -247,9 +258,19 @@ mod tests {
247258
panic!("Could not create output/sub_dir_exists: {}", err);
248259
}
249260

250-
if let Err(e) =
251-
copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"])
252-
{
261+
let ignore = GitignoreBuilder::new(tmp.path())
262+
.add_line(None, "*.md")
263+
.expect("Unable to add '*.md' to gitignore builder")
264+
.build()
265+
.expect("Unable to build gitignore");
266+
267+
if let Err(e) = copy_files_except_ext(
268+
tmp.path(),
269+
&tmp.path().join("output"),
270+
true,
271+
None,
272+
Some(&ignore),
273+
) {
253274
panic!("Error while executing the function:\n{:?}", e);
254275
}
255276

0 commit comments

Comments
 (0)