Skip to content
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

Ignore files listed in .mdbookignore during build #2183

Open
wants to merge 6 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
56 changes: 45 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
ignore = "0.4"
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
12 changes: 12 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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
18 changes: 18 additions & 0 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ fn preprocessor_should_run(
mod tests {
use super::*;
use std::str::FromStr;
use tempfile::Builder as TempFileBuilder;
use toml::value::{Table, Value};

#[test]
Expand Down Expand Up @@ -856,4 +857,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_ext(&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());
}
}
30 changes: 10 additions & 20 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use ignore::gitignore::Gitignore;
use mdbook::errors::Result;
use mdbook::utils;
use mdbook::MDBook;
Expand Down Expand Up @@ -75,16 +76,7 @@ fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
}

match find_gitignore(book_root) {
Some(gitignore_path) => {
match gitignore::File::new(gitignore_path.as_path()) {
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
Err(_) => {
// We're unable to read the .gitignore file, so we'll silently allow everything.
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}
Some(gitignore_path) => filter_ignored_files(&Gitignore::new(gitignore_path).0, paths),
None => {
// There is no .gitignore file.
paths.iter().map(|path| path.to_path_buf()).collect()
Expand All @@ -99,18 +91,16 @@ fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
.find(|p| p.exists())
}

fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec<PathBuf> {
fn filter_ignored_files(ignore: &Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
paths
.iter()
.filter(|path| match exclusion_checker.is_excluded(path) {
Ok(exclude) => !exclude,
Err(error) => {
warn!(
"Unable to determine if {:?} is excluded: {:?}. Including it.",
&path, error
);
true
}
.filter(|path| {
let canonicalized = path.canonicalize();
let p = canonicalized.as_ref().unwrap_or(path);

!ignore
.matched_path_or_any_parents(p, p.is_dir())
.is_ignore()
})
.map(|path| path.to_path_buf())
.collect()
Expand Down
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 @@ -14,6 +14,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 @@ -589,7 +590,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_ext(
&src_dir,
destination,
true,
Some(&build_dir),
Some(&ignore),
)?;

Ok(())
}
Expand Down
38 changes: 26 additions & 12 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;
use log::{debug, trace};
use std::convert::Into;
use std::fs::{self, File};
Expand Down Expand Up @@ -94,13 +95,13 @@ pub fn copy_files_except_ext(
to: &Path,
recursive: bool,
avoid_dir: Option<&PathBuf>,
ext_blacklist: &[&str],
ignore: Option<&Gitignore>,
) -> Result<()> {
debug!(
"Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}",
from.display(),
to.display(),
ext_blacklist,
ignore,
avoid_dir
);

Expand All @@ -116,6 +117,14 @@ pub fn copy_files_except_ext(
.metadata()
.with_context(|| format!("Failed to read {:?}", entry.path()))?;

// Check if it is in the blacklist
if let Some(ignore) = ignore {
let path = entry.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.path() == to.to_path_buf() {
Expand All @@ -138,15 +147,9 @@ pub fn copy_files_except_ext(
&to.join(entry.file_name()),
true,
avoid_dir,
ext_blacklist,
ignore,
)?;
} else if metadata.is_file() {
// Check if it is in the blacklist
if let Some(ext) = entry.path().extension() {
if ext_blacklist.contains(&ext.to_str().unwrap()) {
continue;
}
}
debug!(
"creating path for file: {:?}",
&to.join(
Expand Down Expand Up @@ -191,6 +194,7 @@ pub fn get_404_output_file(input_404: &Option<String>) -> String {
#[cfg(test)]
mod tests {
use super::copy_files_except_ext;
use ignore::gitignore::GitignoreBuilder;
use std::{fs, io::Result, path::Path};

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -247,9 +251,19 @@ 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"])
{
let ignore = GitignoreBuilder::new(tmp.path())
.add_line(None, "*.md")
.expect("Unable to add '*.md' to gitignore builder")
.build()
.expect("Unable to build gitignore");

if let Err(e) = copy_files_except_ext(
tmp.path(),
&tmp.path().join("output"),
true,
None,
Some(&ignore),
) {
panic!("Error while executing the function:\n{:?}", e);
}

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.