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

Added support for gitignore files. #1044

Merged
merged 3 commits into from
Oct 4, 2019
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ book-example/book
.vscode
tests/dummy_book/book/

# Ignore Jetbrains specific files.
.idea/
16 changes: 16 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ toml-query = "0.9"

# Watch feature
notify = { version = "4.0", optional = true }
gitignore = { version = "1.0", optional = true }

# Serve feature
iron = { version = "0.6", optional = true }
Expand All @@ -57,7 +58,7 @@ walkdir = "2.0"
default = ["output", "watch", "serve", "search"]
debug = []
output = []
watch = ["notify"]
watch = ["notify", "gitignore"]
serve = ["iron", "staticfile", "ws"]
search = ["elasticlunr-rs", "ammonia"]

Expand Down
40 changes: 38 additions & 2 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
Ok(())
}

fn remove_ignored_files(book_root: &PathBuf, paths: &[PathBuf]) -> Vec<PathBuf> {
if paths.is_empty() {
return vec![];
}

let gitignore_path = book_root.with_file_name(".gitignore");

match gitignore::File::new(gitignore_path.as_path()) {
Ok(exclusion_checker) => 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
}
})
.map(|path| path.to_path_buf())
.collect(),
Err(error) => {
warn!(
"Unable to read gitignore file at {:?} file: {:?}. All files will be allowed.",
gitignore_path, error
);
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}

/// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
where
Expand Down Expand Up @@ -96,8 +128,12 @@ where
_ => None,
}
})
.collect();
.collect::<Vec<_>>();

closure(paths, &book.root);
let paths = remove_ignored_files(&book.root, &paths[..]);

if !paths.is_empty() {
closure(paths, &book.root);
}
}
}
10 changes: 2 additions & 8 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,12 @@ more text with spaces

#[test]
fn it_converts_single_quotes() {
assert_eq!(
convert_quotes_to_curly("'one', 'two'"),
"‘one’, ‘two’"
);
assert_eq!(convert_quotes_to_curly("'one', 'two'"), "‘one’, ‘two’");
}

#[test]
fn it_converts_double_quotes() {
assert_eq!(
convert_quotes_to_curly(r#""one", "two""#),
"“one”, “two”"
);
assert_eq!(convert_quotes_to_curly(r#""one", "two""#), "“one”, “two”");
}

#[test]
Expand Down