2019-05-25 18:50:41 +00:00
|
|
|
use crate::nop_lib::Nop;
|
2018-09-16 15:00:19 +00:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2018-09-16 15:44:52 +00:00
|
|
|
use mdbook::book::Book;
|
2018-09-25 11:41:38 +00:00
|
|
|
use mdbook::errors::Error;
|
|
|
|
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
|
2018-09-16 15:23:03 +00:00
|
|
|
use std::io;
|
2018-09-25 11:41:38 +00:00
|
|
|
use std::process;
|
|
|
|
|
|
|
|
pub fn make_app() -> App<'static, 'static> {
|
|
|
|
App::new("nop-preprocessor")
|
|
|
|
.about("A mdbook preprocessor which does precisely nothing")
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("supports")
|
|
|
|
.arg(Arg::with_name("renderer").required(true))
|
2018-12-03 23:11:41 +00:00
|
|
|
.about("Check whether a renderer is supported by this preprocessor"),
|
|
|
|
)
|
2018-09-25 11:41:38 +00:00
|
|
|
}
|
2018-09-16 14:49:52 +00:00
|
|
|
|
|
|
|
fn main() {
|
2018-09-25 11:41:38 +00:00
|
|
|
let matches = make_app().get_matches();
|
|
|
|
|
|
|
|
// Users will want to construct their own preprocessor here
|
|
|
|
let preprocessor = Nop::new();
|
2018-09-16 14:49:52 +00:00
|
|
|
|
|
|
|
if let Some(sub_args) = matches.subcommand_matches("supports") {
|
2018-09-25 11:41:38 +00:00
|
|
|
handle_supports(&preprocessor, sub_args);
|
2019-05-06 18:20:58 +00:00
|
|
|
} else if let Err(e) = handle_preprocessing(&preprocessor) {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
process::exit(1);
|
2018-09-16 14:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 11:41:38 +00:00
|
|
|
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
|
|
|
|
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
|
2018-09-16 15:23:03 +00:00
|
|
|
|
2018-09-25 11:41:38 +00:00
|
|
|
if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
|
|
|
|
// We should probably use the `semver` crate to check compatibility
|
|
|
|
// here...
|
|
|
|
eprintln!(
|
|
|
|
"Warning: The {} plugin was built against version {} of mdbook, \
|
|
|
|
but we're being called from version {}",
|
|
|
|
pre.name(),
|
|
|
|
mdbook::MDBOOK_VERSION,
|
|
|
|
ctx.mdbook_version
|
|
|
|
);
|
2018-09-16 15:44:52 +00:00
|
|
|
}
|
2018-09-16 15:28:01 +00:00
|
|
|
|
2018-09-25 11:41:38 +00:00
|
|
|
let processed_book = pre.run(&ctx, book)?;
|
|
|
|
serde_json::to_writer(io::stdout(), &processed_book)?;
|
2018-09-16 15:44:52 +00:00
|
|
|
|
2018-09-25 11:41:38 +00:00
|
|
|
Ok(())
|
2018-09-16 14:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 11:41:38 +00:00
|
|
|
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
|
2018-09-16 15:00:19 +00:00
|
|
|
let renderer = sub_args.value_of("renderer").expect("Required argument");
|
2018-09-25 11:41:38 +00:00
|
|
|
let supported = pre.supports_renderer(&renderer);
|
2018-09-16 15:00:19 +00:00
|
|
|
|
2018-09-16 15:44:52 +00:00
|
|
|
// Signal whether the renderer is supported by exiting with 1 or 0.
|
2018-09-16 15:00:19 +00:00
|
|
|
if supported {
|
|
|
|
process::exit(0);
|
|
|
|
} else {
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2018-09-16 14:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 11:41:38 +00:00
|
|
|
/// The actual implementation of the `Nop` preprocessor. This would usually go
|
|
|
|
/// in your main `lib.rs` file.
|
|
|
|
mod nop_lib {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// A no-op preprocessor.
|
|
|
|
pub struct Nop;
|
|
|
|
|
|
|
|
impl Nop {
|
|
|
|
pub fn new() -> Nop {
|
|
|
|
Nop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Preprocessor for Nop {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"nop-preprocessor"
|
|
|
|
}
|
2018-09-16 14:49:52 +00:00
|
|
|
|
2018-12-03 23:11:41 +00:00
|
|
|
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
|
2018-09-25 11:41:38 +00:00
|
|
|
// In testing we want to tell the preprocessor to blow up by setting a
|
|
|
|
// particular config value
|
|
|
|
if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
|
|
|
|
if nop_cfg.contains_key("blow-up") {
|
|
|
|
return Err("Boom!!1!".into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we *are* a no-op preprocessor after all
|
|
|
|
Ok(book)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supports_renderer(&self, renderer: &str) -> bool {
|
|
|
|
renderer != "not-supported"
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 14:49:52 +00:00
|
|
|
}
|