mdbook/src/cmd/build.rs

51 lines
1.6 KiB
Rust
Raw Normal View History

use crate::{get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use mdbook::errors::Result;
2018-07-23 12:45:01 -05:00
use mdbook::MDBook;
// Create clap subcommand arguments
2022-01-18 15:56:45 -06:00
pub fn make_subcommand<'help>() -> App<'help> {
App::new("build")
2018-08-02 15:48:22 -05:00
.about("Builds a book from its markdown files")
.arg(
Arg::new("dest-dir")
.short('d')
.long("dest-dir")
.value_name("dest-dir")
.help(
"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 21:57:43 +07:00
)
.arg(arg!([dir]
"Root directory for the book{n}\
(Defaults to the Current Directory when omitted)"
))
.arg(arg!(-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-02 15:48:22 -05: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?
let path = book.build_dir_for("html").join("index.html");
if !path.exists() {
error!("No chapter available to open");
std::process::exit(1)
2021-12-28 21:00:06 -08:00
}
open(path);
}
Ok(())
}