2022-07-04 15:16:31 +00:00
|
|
|
use super::command_prelude::*;
|
2022-06-22 22:05:56 +00:00
|
|
|
use crate::{get_book_dir, open};
|
2017-06-26 21:17:46 +00:00
|
|
|
use mdbook::errors::Result;
|
2018-07-23 17:45:01 +00:00
|
|
|
use mdbook::MDBook;
|
2022-07-04 15:16:31 +00:00
|
|
|
use std::path::PathBuf;
|
2017-06-25 22:53:58 +00:00
|
|
|
|
2017-06-27 05:59:50 +00:00
|
|
|
// Create clap subcommand arguments
|
2022-07-04 15:16:31 +00:00
|
|
|
pub fn make_subcommand() -> Command {
|
|
|
|
Command::new("build")
|
2018-08-02 20:48:22 +00:00
|
|
|
.about("Builds a book from its markdown files")
|
2022-07-04 15:16:31 +00:00
|
|
|
.arg_dest_dir()
|
|
|
|
.arg_root_dir()
|
|
|
|
.arg_open()
|
2017-06-27 05:59:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 22:53:58 +00:00
|
|
|
// Build command implementation
|
2017-06-27 05:59:50 +00:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-25 22:53:58 +00:00
|
|
|
let book_dir = get_book_dir(args);
|
2017-11-18 12:41:04 +00:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
2017-06-25 22:53:58 +00:00
|
|
|
|
2022-07-04 15:16:31 +00:00
|
|
|
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
|
2018-08-02 20:48:22 +00:00
|
|
|
book.config.build.build_dir = dest_dir.into();
|
2017-09-30 13:44:25 +00:00
|
|
|
}
|
2017-06-25 22:53:58 +00:00
|
|
|
|
|
|
|
book.build()?;
|
|
|
|
|
2022-07-04 15:16:31 +00:00
|
|
|
if args.get_flag("open") {
|
2018-01-07 14:10:48 +00:00
|
|
|
// FIXME: What's the right behaviour if we don't use the HTML renderer?
|
2022-06-22 22:05:56 +00:00
|
|
|
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-29 05:00:06 +00:00
|
|
|
}
|
2022-06-22 22:05:56 +00:00
|
|
|
open(path);
|
2017-06-25 22:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|