mdBook/examples/nop-preprocessor.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

extern crate mdbook;
#[macro_use]
extern crate clap;
use clap::{App, Arg, ArgMatches, SubCommand};
2018-09-16 23:23:03 +08:00
use mdbook::preprocess::{Preprocessor, PreprocessorContext, CmdPreprocessor};
use std::process;
2018-09-16 23:23:03 +08:00
use std::io;
fn main() {
let matches = app().get_matches();
if let Some(sub_args) = matches.subcommand_matches("supports") {
handle_supports(sub_args);
} else {
handle_preprocessing(&matches);
}
}
fn handle_preprocessing(args: &ArgMatches) {
2018-09-16 23:23:03 +08:00
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())
.expect("Couldn't parse the input");
// You can tell the preprocessor to blow up by setting a particular
// config value
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!");
}
}
}
fn handle_supports(sub_args: &ArgMatches) {
let renderer = sub_args.value_of("renderer").expect("Required argument");
let supported = renderer_is_supported(&renderer);
if supported {
process::exit(0);
} else {
process::exit(1);
}
}
fn renderer_is_supported(renderer: &str) -> bool {
// We support everything except the `not-supported` renderer
renderer != "not-supported"
}
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",
),
)
}