2022-06-23 00:05:56 +02:00
|
|
|
use crate::{get_book_dir, open};
|
2022-01-19 10:10:53 -06:00
|
|
|
use clap::{arg, App, Arg, ArgMatches};
|
2017-06-26 23:17:46 +02:00
|
|
|
use mdbook::errors::Result;
|
2018-07-23 12:45:01 -05:00
|
|
|
use mdbook::MDBook;
|
2017-06-26 00:53:58 +02:00
|
|
|
|
2017-06-27 07:59:50 +02:00
|
|
|
// Create clap subcommand arguments
|
2022-01-18 15:56:45 -06:00
|
|
|
pub fn make_subcommand<'help>() -> App<'help> {
|
2022-01-18 16:19:40 -06:00
|
|
|
App::new("build")
|
2018-08-02 15:48:22 -05:00
|
|
|
.about("Builds a book from its markdown files")
|
2022-01-19 10:10:53 -06:00
|
|
|
.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
|
|
|
)
|
2022-01-19 10:10:53 -06: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"))
|
2017-06-27 07:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-26 00:53:58 +02:00
|
|
|
// Build command implementation
|
2017-06-27 07:59:50 +02:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-26 00:53:58 +02: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 00:53:58 +02:00
|
|
|
|
2017-09-30 21:44:25 +08:00
|
|
|
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();
|
2017-09-30 21:44:25 +08:00
|
|
|
}
|
2017-06-26 00:53:58 +02:00
|
|
|
|
|
|
|
|
book.build()?;
|
|
|
|
|
|
2017-06-27 14:01:33 +02: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?
|
2022-06-23 00:05:56 +02: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-28 21:00:06 -08:00
|
|
|
}
|
2022-06-23 00:05:56 +02:00
|
|
|
open(path);
|
2017-06-26 00:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|