diff options
author | Michael Bryan <michaelfbryan@gmail.com> | 2018-09-19 22:27:02 +0800 |
---|---|---|
committer | Jan-Erik Rediger <janerik@fnordig.de> | 2019-07-09 11:11:09 +0200 |
commit | 98f74423f55f29a2fd75d41e695b0bc8c58040ea (patch) | |
tree | 2e2ae958aabd711d3572358f9df98049f74d784f /src | |
parent | 3cea2671f3fe14875939c6da5253d0de2cc53c23 (diff) |
Hacked the main binary about a bit
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 80 |
1 files changed, 39 insertions, 41 deletions
diff --git a/src/main.rs b/src/main.rs index ce4d361..8762c79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,64 +1,62 @@ -extern crate env_logger; +extern crate clap; extern crate mdbook; extern crate mdbook_mermaid; -extern crate clap; +extern crate serde_json; +use clap::{App, ArgMatches}; +use mdbook::errors::Error; +use mdbook::preprocessor::CmdPreprocessor; use mdbook::MDBook; -use mdbook::errors::Result; use mdbook_mermaid::Mermaid; -use clap::{App, ArgMatches}; -use std::env; +use std::io; use std::process; -use std::path::{Path, PathBuf}; - -fn get_book_dir(args: &ArgMatches) -> PathBuf { - if let Some(dir) = args.value_of("dir") { - // Check if path is relative from current dir, or absolute... - let p = Path::new(dir); - if p.is_relative() { - env::current_dir().unwrap().join(dir) - } else { - p.to_path_buf() - } - } else { - env::current_dir().expect("Unable to determine the current directory") - } -} -pub fn make_app<'a, 'b>() -> App<'a, 'b> { +pub fn make_app() -> App<'static, 'static> { App::new("mdbook-mermaid") .about("Build the book from the markdown files with mermaid support") - .arg_from_usage( - "-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book \ - when omitted)'", - ) - .arg_from_usage( - "[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'", - ) + .subcommand( + SubCommand::with_name("supports") + .arg(Arg::with_name("renderer").required(true)) + .about("Check whether a renderer is supported by this preprocessor")) } -pub fn execute(args: &ArgMatches) -> Result<()> { - let book_dir = get_book_dir(args); - let mut book = MDBook::load(&book_dir)?; +fn main() { + let matches = make_app().get_matches(); + + let result = match matches.subcommand_matches("supports") { + Some(sub_args) => handle_supports(sub_args), + None => handle_preprocessing(), + }; - if let Some(dest_dir) = args.value_of("dest-dir") { - book.config.build.build_dir = PathBuf::from(dest_dir); + if let Err(e) = result { + eprintln!("{}", e); + process::exit(1); } +} - book.with_preprecessor(Mermaid); - book.build()?; +fn handle_preprocessing() -> Result<(), Error> { + let (ctx, book) = CmdPreprocessor::parse_input(io::stdin()) + .expect("Couldn't parse the input"); + + if ctx.mdbook_version != mdbook::MDBOOK_VERSION { + return Err(Error::from("The version check failed!")); + } + + let processed_book = Mermaid.run(&ctx, &book)?; + serde_json::to_writer(io::stdout(), &processed_book)?; Ok(()) } -fn main() { - env_logger::init(); - let app = make_app(); - let matches = app.get_matches(); +fn handle_supports(sub_args: &ArgMatches) { + let renderer = sub_args.value_of("renderer").expect("Required argument"); + let supported = Mermaid.supports_renderer(&renderer); - if let Err(e) = execute(&matches) { - eprintln!("{}", e); + // Signal whether the renderer is supported by exiting with 1 or 0. + if supported { + process::exit(0); + } else { process::exit(1); } } |