2018-09-16 22:49:52 +08:00
|
|
|
extern crate mdbook;
|
2018-09-16 23:28:01 +08:00
|
|
|
extern crate serde_json;
|
2018-09-16 22:49:52 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
|
|
|
|
2018-09-16 23:00:19 +08:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2018-09-16 23:28:01 +08:00
|
|
|
use mdbook::preprocess::CmdPreprocessor;
|
2018-09-16 23:44:52 +08:00
|
|
|
use mdbook::book::Book;
|
2018-09-16 23:00:19 +08:00
|
|
|
use std::process;
|
2018-09-16 23:23:03 +08:00
|
|
|
use std::io;
|
2018-09-16 22:49:52 +08:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let matches = app().get_matches();
|
|
|
|
|
|
|
|
if let Some(sub_args) = matches.subcommand_matches("supports") {
|
|
|
|
handle_supports(sub_args);
|
|
|
|
} else {
|
2018-09-16 23:44:52 +08:00
|
|
|
handle_preprocessing();
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 23:44:52 +08:00
|
|
|
fn handle_preprocessing() {
|
2018-09-16 23:23:03 +08:00
|
|
|
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())
|
|
|
|
.expect("Couldn't parse the input");
|
|
|
|
|
2018-09-16 23:44:52 +08:00
|
|
|
// You can inspect the calling mdbook's version to check for compatibility
|
|
|
|
if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
|
|
|
|
panic!("The version check failed!");
|
|
|
|
}
|
2018-09-16 23:28:01 +08:00
|
|
|
|
2018-09-16 23:44:52 +08:00
|
|
|
// In testing we want to tell the preprocessor to blow up by setting a
|
|
|
|
// particular config value
|
2018-09-16 23:23:03 +08:00
|
|
|
if let Some(table) = ctx.config.get_preprocessor("nop-preprocessor") {
|
|
|
|
let should_blow_up = table.get("blow-up").is_some();
|
|
|
|
|
|
|
|
if should_blow_up {
|
|
|
|
panic!("Boom!!!1!");
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 23:28:01 +08:00
|
|
|
|
2018-09-16 23:44:52 +08:00
|
|
|
let processed_book = do_processing(book);
|
|
|
|
|
|
|
|
serde_json::to_writer(io::stdout(), &processed_book).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_processing(book: Book) -> Book {
|
|
|
|
// We *are* a nop preprocessor after all...
|
|
|
|
book
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_supports(sub_args: &ArgMatches) {
|
2018-09-16 23:00:19 +08:00
|
|
|
let renderer = sub_args.value_of("renderer").expect("Required argument");
|
|
|
|
let supported = renderer_is_supported(&renderer);
|
|
|
|
|
2018-09-16 23:44:52 +08:00
|
|
|
// Signal whether the renderer is supported by exiting with 1 or 0.
|
2018-09-16 23:00:19 +08:00
|
|
|
if supported {
|
|
|
|
process::exit(0);
|
|
|
|
} else {
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn renderer_is_supported(renderer: &str) -> bool {
|
2018-09-16 23:00:19 +08:00
|
|
|
// We support everything except the `not-supported` renderer
|
|
|
|
renderer != "not-supported"
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn app() -> App<'static, 'static> {
|
|
|
|
app_from_crate!().subcommand(
|
|
|
|
SubCommand::with_name("supports")
|
|
|
|
.arg(Arg::with_name("renderer").required(true))
|
|
|
|
.about(
|
|
|
|
"Check whether a renderer is supported by this preprocessor",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|