use clap::{Arg, ArgMatches, Command};
use mdbook_preprocessor::{errors::Result, Preprocessor};
use mdbook_reading_time::ReadingTime;
use std::{io, process};
pub fn make_app() -> Command {
Command::new("reading-time-preprocessor")
.about("A mdbook preprocessor that calculates word count and reading time")
.subcommand(
Command::new("supports")
.arg(Arg::new("renderer").required(true))
.about("Check whether a renderer is supported by this preprocessor"),
)
}
fn main() {
let matches = make_app().get_matches();
let preprocessor = ReadingTime::new();
if let Some(sub_args) = matches.subcommand_matches("supports") {
handle_supports(&preprocessor, sub_args);
} else if let Err(e) = handle_preprocessing(&preprocessor) {
eprintln!("{e}");
process::exit(1);
}
}
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<()> {
let (ctx, book) = mdbook_preprocessor::parse_input(io::stdin())?;
let processed_book = pre.run(&ctx, book)?;
serde_json::to_writer(io::stdout(), &processed_book)?;
Ok(())
}
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
let renderer = sub_args
.get_one::<String>("renderer")
.expect("Required argument");
let supported = pre.supports_renderer(renderer).unwrap_or(false);
if supported {
process::exit(0);
} else {
process::exit(1);
}
}