mdBook/src/cmd/build.rs

40 lines
1.3 KiB
Rust
Raw Normal View History

use clap::{App, ArgMatches, SubCommand};
use mdbook::errors::Result;
2018-07-24 01:45:01 +08:00
use mdbook::MDBook;
use {get_book_dir, open};
// 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")
.arg_from_usage(
2018-08-03 04:48:22 +08:00
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
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'")
}
// Build command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let mut book = MDBook::load(&book_dir)?;
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();
}
book.build()?;
if args.is_present("open") {
// FIXME: What's the right behaviour if we don't use the HTML renderer?
open(book.build_dir_for("html").join("index.html"));
}
Ok(())
}