| 1 |
//! A basic example of a preprocessor that does nothing. |
| 2 |
|
| 3 |
use crate::nop_lib::Nop; |
| 4 |
use clap::{Arg, ArgMatches, Command}; |
| 5 |
use mdbook_preprocessor::book::Book; |
| 6 |
use mdbook_preprocessor::errors::Result; |
| 7 |
use mdbook_preprocessor::{Preprocessor, PreprocessorContext}; |
| 8 |
use semver::{Version, VersionReq}; |
| 9 |
use std::io; |
| 10 |
use std::process; |
| 11 |
|
| 12 |
fn make_app() -> Command { |
| 13 |
Command::new("nop-preprocessor") |
| 14 |
.about("A mdbook preprocessor which does precisely nothing") |
| 15 |
.subcommand( |
| 16 |
Command::new("supports") |
| 17 |
.arg(Arg::new("renderer").required(true)) |
| 18 |
.about("Check whether a renderer is supported by this preprocessor"), |
| 19 |
) |
| 20 |
} |
| 21 |
|
| 22 |
fn main() { |
| 23 |
let matches = make_app().get_matches(); |
| 24 |
|
| 25 |
// Users will want to construct their own preprocessor here |
| 26 |
let preprocessor = Nop::new(); |
| 27 |
|
| 28 |
if let Some(sub_args) = matches.subcommand_matches("supports") { |
| 29 |
handle_supports(&preprocessor, sub_args); |
| 30 |
} else if let Err(e) = handle_preprocessing(&preprocessor) { |
| 31 |
eprintln!("{e:?}"); |
| 32 |
process::exit(1); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<()> { |
| 37 |
let (ctx, book) = mdbook_preprocessor::parse_input(io::stdin())?; |
| 38 |
|
| 39 |
let book_version = Version::parse(&ctx.mdbook_version)?; |
| 40 |
let version_req = VersionReq::parse(mdbook_preprocessor::MDBOOK_VERSION)?; |
| 41 |
|
| 42 |
if !version_req.matches(&book_version) { |
| 43 |
eprintln!( |
| 44 |
"Warning: The {} plugin was built against version {} of mdbook, \ |
| 45 |
but we're being called from version {}", |
| 46 |
pre.name(), |
| 47 |
mdbook_preprocessor::MDBOOK_VERSION, |
| 48 |
ctx.mdbook_version |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
let processed_book = pre.run(&ctx, book)?; |
| 53 |
serde_json::to_writer(io::stdout(), &processed_book)?; |
| 54 |
|
| 55 |
Ok(()) |
| 56 |
} |
| 57 |
|
| 58 |
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! { |
| 59 |
let renderer = sub_args |
| 60 |
.get_one::<String>("renderer") |
| 61 |
.expect("Required argument"); |
| 62 |
let supported = pre.supports_renderer(renderer).unwrap(); |
| 63 |
|
| 64 |
// Signal whether the renderer is supported by exiting with 1 or 0. |
| 65 |
if supported { |
| 66 |
process::exit(0); |
| 67 |
} else { |
| 68 |
process::exit(1); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/// The actual implementation of the `Nop` preprocessor. This would usually go |
| 73 |
/// in your main `lib.rs` file. |
| 74 |
#[allow(unreachable_pub, reason = "wouldn't be a problem in a proper lib.rs")] |
| 75 |
mod nop_lib { |
| 76 |
use super::*; |
| 77 |
|
| 78 |
/// A no-op preprocessor. |
| 79 |
pub struct Nop; |
| 80 |
|
| 81 |
impl Nop { |
| 82 |
pub fn new() -> Nop { |
| 83 |
Nop |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl Preprocessor for Nop { |
| 88 |
fn name(&self) -> &str { |
| 89 |
"nop-preprocessor" |
| 90 |
} |
| 91 |
|
| 92 |
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book> { |
| 93 |
// In testing we want to tell the preprocessor to blow up by setting a |
| 94 |
// particular config value |
| 95 |
match ctx |
| 96 |
.config |
| 97 |
.get::<bool>("preprocessor.nop-preprocessor.blow-up") |
| 98 |
{ |
| 99 |
Ok(Some(true)) => anyhow::bail!("Boom!!1!"), |
| 100 |
Ok(_) => {} |
| 101 |
Err(e) => anyhow::bail!("expect bool for blow-up: {e}"), |
| 102 |
} |
| 103 |
|
| 104 |
// we *are* a no-op preprocessor after all |
| 105 |
Ok(book) |
| 106 |
} |
| 107 |
|
| 108 |
fn supports_renderer(&self, renderer: &str) -> Result<bool> { |
| 109 |
Ok(renderer != "not-supported") |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[cfg(test)] |
| 114 |
mod test { |
| 115 |
use super::*; |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn nop_preprocessor_run() { |
| 119 |
let input_json = r##"[ |
| 120 |
{ |
| 121 |
"root": "/path/to/book", |
| 122 |
"config": { |
| 123 |
"book": { |
| 124 |
"authors": ["AUTHOR"], |
| 125 |
"language": "en", |
| 126 |
"src": "src", |
| 127 |
"title": "TITLE" |
| 128 |
}, |
| 129 |
"preprocessor": { |
| 130 |
"nop": {} |
| 131 |
} |
| 132 |
}, |
| 133 |
"renderer": "html", |
| 134 |
"mdbook_version": "0.4.21" |
| 135 |
}, |
| 136 |
{ |
| 137 |
"items": [ |
| 138 |
{ |
| 139 |
"Chapter": { |
| 140 |
"name": "Chapter 1", |
| 141 |
"content": "# Chapter 1\n", |
| 142 |
"number": [1], |
| 143 |
"sub_items": [], |
| 144 |
"path": "chapter_1.md", |
| 145 |
"source_path": "chapter_1.md", |
| 146 |
"parent_names": [] |
| 147 |
} |
| 148 |
} |
| 149 |
] |
| 150 |
} |
| 151 |
]"##; |
| 152 |
let input_json = input_json.as_bytes(); |
| 153 |
|
| 154 |
let (ctx, book) = mdbook_preprocessor::parse_input(input_json).unwrap(); |
| 155 |
let expected_book = book.clone(); |
| 156 |
let result = Nop::new().run(&ctx, book); |
| 157 |
assert!(result.is_ok()); |
| 158 |
|
| 159 |
// The nop-preprocessor should not have made any changes to the book content. |
| 160 |
let actual_book = result.unwrap(); |
| 161 |
assert_eq!(actual_book, expected_book); |
| 162 |
} |
| 163 |
} |
| 164 |
} |