use anyhow::Result;
use clap::{Parser, Subcommand};
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use mdbook_include_rs::IncludeDocPreprocessor;
use std::io;
use std::path::PathBuf;
use std::process;
#[derive(Parser)]
#[command(author, version, about)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Supports {
renderer: String,
},
#[command(name = "pre-process")]
PreProcess {
#[arg(long)]
dir: PathBuf,
},
}
fn main() -> Result<()> {
let args = Cli::parse();
let preprocessor = IncludeDocPreprocessor;
match args.command {
Some(Commands::Supports { renderer }) => {
if preprocessor.supports_renderer(&renderer) {
process::exit(0);
} else {
process::exit(1);
}
}
Some(Commands::PreProcess { dir: _ }) | None => {
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
let processed_book = preprocessor.run(&ctx, book)?;
serde_json::to_writer(io::stdout(), &processed_book)?;
}
}
Ok(())
}