]> git.proxmox.com Git - rustc.git/blame - vendor/mdbook/examples/nop-preprocessor.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / vendor / mdbook / examples / nop-preprocessor.rs
CommitLineData
dc9dc135 1use crate::nop_lib::Nop;
9fa01778
XL
2use clap::{App, Arg, ArgMatches, SubCommand};
3use mdbook::book::Book;
4use mdbook::errors::Error;
5use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
94222f64 6use semver::{Version, VersionReq};
9fa01778
XL
7use std::io;
8use std::process;
9
10pub fn make_app() -> App<'static, 'static> {
11 App::new("nop-preprocessor")
12 .about("A mdbook preprocessor which does precisely nothing")
13 .subcommand(
14 SubCommand::with_name("supports")
15 .arg(Arg::with_name("renderer").required(true))
16 .about("Check whether a renderer is supported by this preprocessor"),
17 )
18}
19
20fn main() {
21 let matches = make_app().get_matches();
22
23 // Users will want to construct their own preprocessor here
24 let preprocessor = Nop::new();
25
26 if let Some(sub_args) = matches.subcommand_matches("supports") {
27 handle_supports(&preprocessor, sub_args);
dc9dc135
XL
28 } else if let Err(e) = handle_preprocessing(&preprocessor) {
29 eprintln!("{}", e);
30 process::exit(1);
9fa01778
XL
31 }
32}
33
34fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
35 let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
36
94222f64
XL
37 let book_version = Version::parse(&ctx.mdbook_version)?;
38 let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;
39
40 if version_req.matches(&book_version) != true {
9fa01778
XL
41 eprintln!(
42 "Warning: The {} plugin was built against version {} of mdbook, \
43 but we're being called from version {}",
44 pre.name(),
45 mdbook::MDBOOK_VERSION,
46 ctx.mdbook_version
47 );
48 }
49
50 let processed_book = pre.run(&ctx, book)?;
51 serde_json::to_writer(io::stdout(), &processed_book)?;
52
53 Ok(())
54}
55
56fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
57 let renderer = sub_args.value_of("renderer").expect("Required argument");
58 let supported = pre.supports_renderer(&renderer);
59
60 // Signal whether the renderer is supported by exiting with 1 or 0.
61 if supported {
62 process::exit(0);
63 } else {
64 process::exit(1);
65 }
66}
67
68/// The actual implementation of the `Nop` preprocessor. This would usually go
69/// in your main `lib.rs` file.
70mod nop_lib {
71 use super::*;
72
73 /// A no-op preprocessor.
74 pub struct Nop;
75
76 impl Nop {
77 pub fn new() -> Nop {
78 Nop
79 }
80 }
81
82 impl Preprocessor for Nop {
83 fn name(&self) -> &str {
84 "nop-preprocessor"
85 }
86
87 fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
88 // In testing we want to tell the preprocessor to blow up by setting a
89 // particular config value
90 if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
91 if nop_cfg.contains_key("blow-up") {
f035d41b 92 anyhow::bail!("Boom!!1!");
9fa01778
XL
93 }
94 }
95
96 // we *are* a no-op preprocessor after all
97 Ok(book)
98 }
99
100 fn supports_renderer(&self, renderer: &str) -> bool {
101 renderer != "not-supported"
102 }
103 }
104}