2019-05-26 02:50:41 +08:00
|
|
|
use crate::{get_book_dir, open};
|
2017-11-18 20:41:04 +08:00
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2017-06-27 05:17:46 +08:00
|
|
|
use mdbook::errors::Result;
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::MDBook;
|
2017-06-26 06:53:58 +08:00
|
|
|
|
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")
|
2018-08-03 04:48:22 +08:00
|
|
|
.about("Builds a book from its markdown files")
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
|
2018-09-06 23:24:42 +08:00
|
|
|
Relative paths are interpreted relative to the book's root directory.{n}\
|
|
|
|
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
|
2019-05-05 22:57:43 +08:00
|
|
|
)
|
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"[dir] 'Root directory for the book{n}\
|
|
|
|
(Defaults to the Current Directory when omitted)'",
|
2019-05-05 22:57:43 +08:00
|
|
|
)
|
|
|
|
.arg_from_usage("-o, --open 'Opens the compiled book in a web browser'")
|
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-11-18 20:41:04 +08:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
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") {
|
2018-08-03 04:48:22 +08:00
|
|
|
book.config.build.build_dir = dest_dir.into();
|
2017-09-30 21:44:25 +08:00
|
|
|
}
|
2017-06-26 06:53:58 +08:00
|
|
|
|
|
|
|
book.build()?;
|
|
|
|
|
2017-06-27 20:01:33 +08:00
|
|
|
if args.is_present("open") {
|
2018-01-07 22:10:48 +08:00
|
|
|
// FIXME: What's the right behaviour if we don't use the HTML renderer?
|
|
|
|
open(book.build_dir_for("html").join("index.html"));
|
2017-06-26 06:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|