Skip to content

Add -m arg to watch/serve to only watch .md files #831

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

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 29 additions & 17 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use mdbook::errors::*;
use mdbook::utils;
use mdbook::MDBook;
use std;
use std::path::Path;
use {get_book_dir, open};

struct ErrorRecover;
Expand All @@ -29,6 +30,11 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
"[dir] 'Root directory for the book{n}\
(Defaults to the Current Directory when omitted)'",
)
.arg_from_usage(
"-m, --markdown-only 'Only watch for Markdown (.md) files{n}\
By default, any change in the source directory will trigger a rebuild.{n}\
However, sometimes this is undesirable (e.g. for swap files).'"
)
.arg(
Arg::with_name("hostname")
.short("n")
Expand Down Expand Up @@ -114,25 +120,31 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
open(serving_url);
}

let md_only = args.is_present("markdown-only");
let is_md_file = |file: &Path| file.extension()
.map_or(false, |ext| ext == "md");

#[cfg(feature = "watch")]
watch::trigger_on_change(&mut book, move |path, book_dir| {
info!("File changed: {:?}", path);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(

let result = MDBook::load(&book_dir)
.and_then(|mut b| {
b.config
.set("output.html.livereload-url", &livereload_url)?;
Ok(b)
}).and_then(|b| b.build());

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = broadcaster.send("reload");
if !md_only || is_md_file(path) {
info!("File changed: {:?}", path);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(

let result = MDBook::load(&book_dir)
.and_then(|mut b| {
b.config
.set("output.html.livereload-url", &livereload_url)?;
Ok(b)
}).and_then(|b| b.build());

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = broadcaster.send("reload");
}
}
});

Expand Down
21 changes: 16 additions & 5 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
"[dir] 'Root directory for the book{n}\
(Defaults to the Current Directory when omitted)'",
).arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
.arg_from_usage(
"-m, --markdown-only 'Only watch for Markdown (.md) files{n}\
By default, any change in the source directory will trigger a rebuild.{n}\
However, sometimes this is undesirable (e.g. for swap files).'"
)
}

// Watch command implementation
Expand All @@ -34,13 +39,19 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
open(book.build_dir_for("html").join("index.html"));
}

let md_only = args.is_present("markdown-only");
let is_md_file = |file: &Path| file.extension()
.map_or(false, |ext| ext == "md");

trigger_on_change(&book, |path, book_dir| {
info!("File changed: {:?}\nBuilding book...\n", path);
let result = MDBook::load(&book_dir).and_then(|b| b.build());
if !md_only || is_md_file(path) {
info!("File changed: {:?}\nBuilding book...\n", path);
let result = MDBook::load(&book_dir).and_then(|b| b.build());

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
}
});

Expand Down