2017-09-30 21:44:25 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use clap::{ArgMatches, SubCommand, App};
|
2017-06-26 06:53:58 +08:00
|
|
|
use mdbook::MDBook;
|
2017-06-27 05:17:46 +08:00
|
|
|
use mdbook::errors::Result;
|
2017-06-26 06:53:58 +08:00
|
|
|
use {get_book_dir, open};
|
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
// Create clap subcommand arguments
|
|
|
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("build")
|
|
|
|
.about("Build the book from the markdown files")
|
|
|
|
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage(
|
|
|
|
"-d, --dest-dir=[dest-dir] 'The output directory for your \
|
|
|
|
book{n}(Defaults to ./book when omitted)'",
|
|
|
|
)
|
|
|
|
.arg_from_usage(
|
2017-11-30 12:02:58 +08:00
|
|
|
"--no-create 'Will not create non-existent files linked from SUMMARY.md (deprecated: use book.toml instead)'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
|
|
|
.arg_from_usage(
|
|
|
|
"[dir] 'A directory for your book{n}(Defaults to Current Directory \
|
|
|
|
when omitted)'",
|
|
|
|
)
|
2017-06-27 13:59:50 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 06:53:58 +08:00
|
|
|
// Build command implementation
|
2017-06-27 13:59:50 +08:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-26 06:53:58 +08:00
|
|
|
let book_dir = get_book_dir(args);
|
2017-09-30 21:44:25 +08:00
|
|
|
let mut book = MDBook::new(&book_dir).read_config()?;
|
2017-06-26 06:53:58 +08:00
|
|
|
|
2017-09-30 21:44:25 +08:00
|
|
|
if let Some(dest_dir) = args.value_of("dest-dir") {
|
2017-11-30 23:26:30 +08:00
|
|
|
book.config.build.build_dir = PathBuf::from(dest_dir);
|
2017-09-30 21:44:25 +08:00
|
|
|
}
|
2017-06-26 06:53:58 +08:00
|
|
|
|
2017-11-30 23:26:30 +08:00
|
|
|
// This flag is deprecated in favor of being set via `book.toml`.
|
2017-06-26 06:53:58 +08:00
|
|
|
if args.is_present("no-create") {
|
2017-11-30 23:26:30 +08:00
|
|
|
book.config.build.create_missing = false;
|
2017-06-26 06:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
book.build()?;
|
|
|
|
|
2017-06-27 20:01:33 +08:00
|
|
|
if args.is_present("open") {
|
|
|
|
open(book.get_destination().join("index.html"));
|
2017-06-26 06:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|