Skip to content

Ignore files listed in .mdbookignore during build #1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ once_cell = "1.17.1"
env_logger = "0.11.1"
handlebars = "6.0"
hex = "0.4.3"
ignore = "0.4.23"
log = "0.4.17"
memchr = "2.5.0"
opener = "0.7.0"
Expand All @@ -44,7 +45,6 @@ topological-sort = "0.2.2"
# Watch feature
notify = { version = "8.0.0", optional = true }
notify-debouncer-mini = { version = "0.6.0", optional = true }
ignore = { version = "0.4.20", optional = true }
pathdiff = { version = "0.2.1", optional = true }
walkdir = { version = "2.3.3", optional = true }

Expand All @@ -67,7 +67,7 @@ walkdir = "2.3.3"

[features]
default = ["watch", "serve", "search"]
watch = ["dep:notify", "dep:notify-debouncer-mini", "dep:ignore", "dep:pathdiff", "dep:walkdir"]
watch = ["dep:notify", "dep:notify-debouncer-mini", "dep:pathdiff", "dep:walkdir"]
serve = ["dep:futures-util", "dep:tokio", "dep:warp"]
search = ["dep:elasticlunr-rs", "dep:ammonia"]

Expand Down
12 changes: 12 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ The value can be any valid URI the browser should navigate to (e.g. `https://rus
This will generate an HTML page which will automatically redirect to the given location.
Note that the source location does not support `#` anchor redirects.

### `.mdbookignore`

You can use a `.mdbookignore` file to exclude files from the build process.
The file is placed in the `src` directory of your book and has the same format as
[`.gitignore`](https://git-scm.com/docs/gitignore) files.

For example:
```
*.rs
/target/
```

## Markdown Renderer

The Markdown renderer will run preprocessors and then output the resulting
Expand Down
20 changes: 19 additions & 1 deletion src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ fn preprocessor_should_run(
mod tests {
use super::*;
use std::str::FromStr;
use toml::value::Table;
use tempfile::Builder as TempFileBuilder;
use toml::value::{Table, Value};

#[test]
fn config_defaults_to_html_renderer_if_empty() {
Expand Down Expand Up @@ -895,4 +896,21 @@ mod tests {
let got = preprocessor_should_run(&BoolPreprocessor(should_be), &html, &cfg);
assert_eq!(got, should_be);
}

#[test]
fn mdbookignore_ignores_file() {
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap();
let test_book_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_book");

utils::fs::copy_files_except_ignored(&test_book_dir, temp_dir.path(), true, None, None)
.expect("Error while copying test book to temp dir");

let book = MDBook::load(temp_dir.path()).expect("Unable to load book");
book.build().expect("Error while building book");

let book_dir = temp_dir.path().join("book");
assert!(book_dir.join("index.html").exists());
assert!(book_dir.join(".mdbookignore").exists());
assert!(!book_dir.join("ignored_file").exists());
}
}
19 changes: 18 additions & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::{Path, PathBuf};

use crate::utils::fs::get_404_output_file;
use handlebars::Handlebars;
use ignore::gitignore::GitignoreBuilder;
use log::{debug, trace, warn};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
Expand Down Expand Up @@ -479,7 +480,23 @@ 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 mut builder = GitignoreBuilder::new(&src_dir);
let mdbook_ignore = src_dir.join(".mdbookignore");
if mdbook_ignore.exists() {
if let Some(err) = builder.add(mdbook_ignore) {
warn!("Unable to load '.mdbookignore' file: {}", err);
}
}
builder.add_line(None, "*.md")?;
let ignore = builder.build()?;

utils::fs::copy_files_except_ignored(
&src_dir,
destination,
true,
Some(&build_dir),
Some(&ignore),
)?;

Ok(())
}
Expand Down
46 changes: 37 additions & 9 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errors::*;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use log::{debug, trace};
use std::fs::{self, File};
use std::io::Write;
Expand Down Expand Up @@ -91,12 +92,30 @@ pub fn copy_files_except_ext(
recursive: bool,
avoid_dir: Option<&PathBuf>,
ext_blacklist: &[&str],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a public API, the API can't change. I think you will need to create a new function. Also, the naming would no longer be appropriate (_ext was filtering on extensions, and this is now filtering on ignore).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I've added a new public function copy_files_except_ignored that is called by copy_files_except_ext to not have code duplication.

) -> Result<()> {
let mut builder = GitignoreBuilder::new(from);
for ext in ext_blacklist {
builder.add_line(None, &format!("*.{ext}"))?;
}
let ignore = builder.build()?;

copy_files_except_ignored(from, to, recursive, avoid_dir, Some(&ignore))
}

/// Copies all files of a directory to another one except the files that are
/// ignored by the passed [`Gitignore`]
pub fn copy_files_except_ignored(
from: &Path,
to: &Path,
recursive: bool,
avoid_dir: Option<&PathBuf>,
ignore: Option<&Gitignore>,
) -> Result<()> {
debug!(
"Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}",
from.display(),
to.display(),
ext_blacklist,
ignore,
avoid_dir
);

Expand All @@ -114,6 +133,14 @@ pub fn copy_files_except_ext(
let entry_file_name = entry.file_name().unwrap();
let target_file_path = to.join(entry_file_name);

// Check if it is in the blacklist
if let Some(ignore) = ignore {
let path = entry.as_path();
if ignore.matched(path, path.is_dir()).is_ignore() {
continue;
}
}

// If the entry is a dir and the recursive option is enabled, call itself
if metadata.is_dir() && recursive {
if entry == to.as_os_str() {
Expand All @@ -126,19 +153,20 @@ pub fn copy_files_except_ext(
}
}

if let Some(ignore) = ignore {
let path = entry.as_path();
if ignore.matched(path, path.is_dir()).is_ignore() {
continue;
}
}

// check if output dir already exists
if !target_file_path.exists() {
fs::create_dir(&target_file_path)?;
}

copy_files_except_ext(&entry, &target_file_path, true, avoid_dir, ext_blacklist)?;
copy_files_except_ignored(&entry, &target_file_path, true, avoid_dir, ignore)?;
} else if metadata.is_file() {
// Check if it is in the blacklist
if let Some(ext) = entry.extension() {
if ext_blacklist.contains(&ext.to_str().unwrap()) {
continue;
}
}
debug!("Copying {entry:?} to {target_file_path:?}");
copy(&entry, &target_file_path)?;
}
Expand Down Expand Up @@ -268,7 +296,7 @@ mod tests {
if let Err(e) =
copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"])
{
panic!("Error while executing the function:\n{e:?}");
panic!("Error while executing the function:\n{:?}", e);
}

// Check if the correct files where created
Expand Down
1 change: 1 addition & 0 deletions test_book/src/.mdbookignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored_file
1 change: 1 addition & 0 deletions test_book/src/ignored_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This will not be copied to the book directory.