The example can now tell mdbook if renderers are supported

This commit is contained in:
Michael Bryan 2018-09-16 23:00:19 +08:00
parent 729c94a7e4
commit 304234c122
No known key found for this signature in database
GPG Key ID: E9C602B0D9A998DC
3 changed files with 55 additions and 35 deletions

View File

@ -2,8 +2,9 @@ extern crate mdbook;
#[macro_use]
extern crate clap;
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{App, Arg, ArgMatches, SubCommand};
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use std::process;
fn main() {
let matches = app().get_matches();
@ -16,16 +17,23 @@ fn main() {
}
fn handle_preprocessing(args: &ArgMatches) {
unimplemented!()
}
fn handle_supports(sub_args: &ArgMatches) {
let renderer = sub_args.value_of("renderer").expect("Required argument");
let supported = renderer_is_supported(&renderer);
if supported {
process::exit(0);
} else {
process::exit(1);
}
}
fn renderer_is_supported(renderer: &str) -> bool {
true
// We support everything except the `not-supported` renderer
renderer != "not-supported"
}
fn app() -> App<'static, 'static> {
@ -37,4 +45,3 @@ fn app() -> App<'static, 'static> {
),
)
}

View File

@ -1,10 +1,10 @@
use super::{Preprocessor, PreprocessorContext};
use book::Book;
use errors::*;
use serde_json;
use shlex::Shlex;
use std::io::{self, Read};
use serde_json;
use std::process::{Command, Stdio, Child};
use std::process::{Child, Command, Stdio};
/// A custom preprocessor which will shell out to a 3rd-party program.
///
@ -31,10 +31,16 @@ impl CmdPreprocessor {
pub fn parse_input<R: Read>(
reader: R,
) -> Result<(PreprocessorContext, Book)> {
serde_json::from_reader(reader).chain_err(|| "Unable to parse the input")
serde_json::from_reader(reader)
.chain_err(|| "Unable to parse the input")
}
fn write_input(&self, child: &mut Child, book: Book, ctx: PreprocessorContext) {
fn write_input(
&self,
child: &mut Child,
book: Book,
ctx: PreprocessorContext,
) {
let mut stdin = child.stdin.take().expect("Child has stdin");
let input = (ctx, book);
@ -75,6 +81,8 @@ impl Preprocessor for CmdPreprocessor {
}
fn supports_renderer(&self, renderer: &str) -> bool {
debug!("Checking if the \"{}\" preprocessor supports \"{}\"", self.name(), renderer);
let mut cmd = match self.command() {
Ok(c) => c,
Err(e) => {
@ -83,16 +91,16 @@ impl Preprocessor for CmdPreprocessor {
}
};
cmd
let outcome = cmd
.arg("supports")
.arg(renderer)
.stdin(Stdio::piped())
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
.stderr(Stdio::inherit())
.status()
.map(|status| status.code() == Some(0));
let child = match cmd.spawn() {
Ok(c) => c,
Err(e) => {
if let Err(ref e) = outcome {
if e.kind() == io::ErrorKind::NotFound {
warn!(
"The command wasn't found, is the \"{}\" preprocessor installed?",
@ -100,12 +108,8 @@ impl Preprocessor for CmdPreprocessor {
);
warn!("\tCommand: {}", self.cmd);
}
// give it the benefit of the doubt
return true;
}
};
unimplemented!()
outcome.unwrap_or(false)
}
}

View File

@ -7,10 +7,19 @@ fn example() -> CmdPreprocessor {
}
#[test]
fn check_if_renderer_is_supported() {
fn example_supports_whatever() {
let cmd = example();
let got = cmd.supports_renderer("whatever");
assert_eq!(got, true);
}
#[test]
fn example_doesnt_support_not_supported() {
let cmd = example();
let got = cmd.supports_renderer("not-supported");
assert_eq!(got, false);
}