-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #792 from rust-lang-nursery/custom-preprocessor
WIP: Custom Preprocessors
- Loading branch information
Showing
10 changed files
with
555 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
extern crate clap; | ||
extern crate mdbook; | ||
extern crate serde_json; | ||
|
||
use clap::{App, Arg, ArgMatches, SubCommand}; | ||
use mdbook::book::Book; | ||
use mdbook::errors::Error; | ||
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext}; | ||
use std::io; | ||
use std::process; | ||
use nop_lib::Nop; | ||
|
||
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)) | ||
.about("Check whether a renderer is supported by this preprocessor")) | ||
} | ||
|
||
fn main() { | ||
let matches = make_app().get_matches(); | ||
|
||
// Users will want to construct their own preprocessor here | ||
let preprocessor = Nop::new(); | ||
|
||
if let Some(sub_args) = matches.subcommand_matches("supports") { | ||
handle_supports(&preprocessor, sub_args); | ||
} else { | ||
if let Err(e) = handle_preprocessing(&preprocessor) { | ||
eprintln!("{}", e); | ||
process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> { | ||
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?; | ||
|
||
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 | ||
); | ||
} | ||
|
||
let processed_book = pre.run(&ctx, book)?; | ||
serde_json::to_writer(io::stdout(), &processed_book)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! { | ||
let renderer = sub_args.value_of("renderer").expect("Required argument"); | ||
let supported = pre.supports_renderer(&renderer); | ||
|
||
// Signal whether the renderer is supported by exiting with 1 or 0. | ||
if supported { | ||
process::exit(0); | ||
} else { | ||
process::exit(1); | ||
} | ||
} | ||
|
||
/// 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" | ||
} | ||
|
||
fn run( | ||
&self, | ||
ctx: &PreprocessorContext, | ||
book: Book, | ||
) -> Result<Book, Error> { | ||
// 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" | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.