2019-05-26 02:50:41 +08:00
|
|
|
use crate::nop_lib::Nop;
|
2022-07-04 23:16:31 +08:00
|
|
|
use clap::{Arg, ArgMatches, Command};
|
2018-09-16 23:44:52 +08:00
|
|
|
use mdbook::book::Book;
|
2018-09-25 19:41:38 +08:00
|
|
|
use mdbook::errors::Error;
|
|
|
|
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
|
2021-04-26 23:08:24 +08:00
|
|
|
use semver::{Version, VersionReq};
|
2018-09-16 23:23:03 +08:00
|
|
|
use std::io;
|
2018-09-25 19:41:38 +08:00
|
|
|
use std::process;
|
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
pub fn make_app() -> Command {
|
|
|
|
Command::new("nop-preprocessor")
|
2018-09-25 19:41:38 +08:00
|
|
|
.about("A mdbook preprocessor which does precisely nothing")
|
|
|
|
.subcommand(
|
2022-07-04 23:16:31 +08:00
|
|
|
Command::new("supports")
|
2022-01-19 06:17:36 +08:00
|
|
|
.arg(Arg::new("renderer").required(true))
|
2018-12-04 07:11:41 +08:00
|
|
|
.about("Check whether a renderer is supported by this preprocessor"),
|
|
|
|
)
|
2018-09-25 19:41:38 +08:00
|
|
|
}
|
2018-09-16 22:49:52 +08:00
|
|
|
|
|
|
|
fn main() {
|
2018-09-25 19:41:38 +08:00
|
|
|
let matches = make_app().get_matches();
|
|
|
|
|
|
|
|
// Users will want to construct their own preprocessor here
|
|
|
|
let preprocessor = Nop::new();
|
2018-09-16 22:49:52 +08:00
|
|
|
|
|
|
|
if let Some(sub_args) = matches.subcommand_matches("supports") {
|
2018-09-25 19:41:38 +08:00
|
|
|
handle_supports(&preprocessor, sub_args);
|
2019-05-07 02:20:58 +08:00
|
|
|
} else if let Err(e) = handle_preprocessing(&preprocessor) {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
process::exit(1);
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:41:38 +08:00
|
|
|
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
|
|
|
|
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
|
2018-09-16 23:23:03 +08:00
|
|
|
|
2021-04-26 23:08:24 +08:00
|
|
|
let book_version = Version::parse(&ctx.mdbook_version)?;
|
|
|
|
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;
|
|
|
|
|
2021-08-24 21:04:32 +08:00
|
|
|
if !version_req.matches(&book_version) {
|
2018-09-25 19:41:38 +08:00
|
|
|
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 23:44:52 +08:00
|
|
|
}
|
2018-09-16 23:28:01 +08:00
|
|
|
|
2018-09-25 19:41:38 +08:00
|
|
|
let processed_book = pre.run(&ctx, book)?;
|
|
|
|
serde_json::to_writer(io::stdout(), &processed_book)?;
|
2018-09-16 23:44:52 +08:00
|
|
|
|
2018-09-25 19:41:38 +08:00
|
|
|
Ok(())
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:41:38 +08:00
|
|
|
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
|
2022-07-04 23:16:31 +08:00
|
|
|
let renderer = sub_args
|
|
|
|
.get_one::<String>("renderer")
|
|
|
|
.expect("Required argument");
|
2021-08-24 21:04:32 +08:00
|
|
|
let supported = pre.supports_renderer(renderer);
|
2018-09-16 23:00:19 +08:00
|
|
|
|
2018-09-16 23:44:52 +08:00
|
|
|
// Signal whether the renderer is supported by exiting with 1 or 0.
|
2018-09-16 23:00:19 +08:00
|
|
|
if supported {
|
|
|
|
process::exit(0);
|
|
|
|
} else {
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:41:38 +08: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 22:49:52 +08:00
|
|
|
|
2018-12-04 07:11:41 +08:00
|
|
|
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
|
2018-09-25 19:41:38 +08: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") {
|
2020-05-21 05:32:00 +08:00
|
|
|
anyhow::bail!("Boom!!1!");
|
2018-09-25 19:41:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we *are* a no-op preprocessor after all
|
|
|
|
Ok(book)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supports_renderer(&self, renderer: &str) -> bool {
|
|
|
|
renderer != "not-supported"
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 05:32:41 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nop_preprocessor_run() {
|
|
|
|
let input_json = r##"[
|
|
|
|
{
|
|
|
|
"root": "/path/to/book",
|
|
|
|
"config": {
|
|
|
|
"book": {
|
|
|
|
"authors": ["AUTHOR"],
|
|
|
|
"language": "en",
|
|
|
|
"multilingual": false,
|
|
|
|
"src": "src",
|
|
|
|
"title": "TITLE"
|
|
|
|
},
|
|
|
|
"preprocessor": {
|
|
|
|
"nop": {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"renderer": "html",
|
|
|
|
"mdbook_version": "0.4.21"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"sections": [
|
|
|
|
{
|
|
|
|
"Chapter": {
|
|
|
|
"name": "Chapter 1",
|
|
|
|
"content": "# Chapter 1\n",
|
|
|
|
"number": [1],
|
|
|
|
"sub_items": [],
|
|
|
|
"path": "chapter_1.md",
|
|
|
|
"source_path": "chapter_1.md",
|
2023-08-01 05:52:00 +08:00
|
|
|
"parent_names": [],
|
|
|
|
"data": {}
|
2022-09-25 05:32:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"__non_exhaustive": null
|
|
|
|
}
|
|
|
|
]"##;
|
|
|
|
let input_json = input_json.as_bytes();
|
|
|
|
|
|
|
|
let (ctx, book) = mdbook::preprocess::CmdPreprocessor::parse_input(input_json).unwrap();
|
|
|
|
let expected_book = book.clone();
|
|
|
|
let result = Nop::new().run(&ctx, book);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
// The nop-preprocessor should not have made any changes to the book content.
|
|
|
|
let actual_book = result.unwrap();
|
|
|
|
assert_eq!(actual_book, expected_book);
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 22:49:52 +08:00
|
|
|
}
|