The example can now tell mdbook if renderers are supported
This commit is contained in:
parent
729c94a7e4
commit
304234c122
|
@ -2,8 +2,9 @@ extern crate mdbook;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
|
||||||
use clap::{App, Arg, SubCommand, ArgMatches};
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||||
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
|
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
|
||||||
|
use std::process;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = app().get_matches();
|
let matches = app().get_matches();
|
||||||
|
@ -16,16 +17,23 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_preprocessing(args: &ArgMatches) {
|
fn handle_preprocessing(args: &ArgMatches) {
|
||||||
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_supports(sub_args: &ArgMatches) {
|
fn handle_supports(sub_args: &ArgMatches) {
|
||||||
let renderer = sub_args.value_of("renderer").expect("Required argument");
|
let renderer = sub_args.value_of("renderer").expect("Required argument");
|
||||||
let supported = renderer_is_supported(&renderer);
|
let supported = renderer_is_supported(&renderer);
|
||||||
|
|
||||||
|
if supported {
|
||||||
|
process::exit(0);
|
||||||
|
} else {
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn renderer_is_supported(renderer: &str) -> bool {
|
fn renderer_is_supported(renderer: &str) -> bool {
|
||||||
true
|
// We support everything except the `not-supported` renderer
|
||||||
|
renderer != "not-supported"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app() -> App<'static, 'static> {
|
fn app() -> App<'static, 'static> {
|
||||||
|
@ -37,4 +45,3 @@ fn app() -> App<'static, 'static> {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use super::{Preprocessor, PreprocessorContext};
|
use super::{Preprocessor, PreprocessorContext};
|
||||||
use book::Book;
|
use book::Book;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
use serde_json;
|
||||||
use shlex::Shlex;
|
use shlex::Shlex;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use serde_json;
|
use std::process::{Child, Command, Stdio};
|
||||||
use std::process::{Command, Stdio, Child};
|
|
||||||
|
|
||||||
/// A custom preprocessor which will shell out to a 3rd-party program.
|
/// A custom preprocessor which will shell out to a 3rd-party program.
|
||||||
///
|
///
|
||||||
|
@ -31,21 +31,27 @@ impl CmdPreprocessor {
|
||||||
pub fn parse_input<R: Read>(
|
pub fn parse_input<R: Read>(
|
||||||
reader: R,
|
reader: R,
|
||||||
) -> Result<(PreprocessorContext, Book)> {
|
) -> 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(
|
||||||
let mut stdin = child.stdin.take().expect("Child has stdin");
|
&self,
|
||||||
let input = (ctx, book);
|
child: &mut Child,
|
||||||
|
book: Book,
|
||||||
|
ctx: PreprocessorContext,
|
||||||
|
) {
|
||||||
|
let mut stdin = child.stdin.take().expect("Child has stdin");
|
||||||
|
let input = (ctx, book);
|
||||||
|
|
||||||
if let Err(e) = serde_json::to_writer(&mut stdin, &input) {
|
if let Err(e) = serde_json::to_writer(&mut stdin, &input) {
|
||||||
// Looks like the backend hung up before we could finish
|
// Looks like the backend hung up before we could finish
|
||||||
// sending it the render context. Log the error and keep going
|
// sending it the render context. Log the error and keep going
|
||||||
warn!("Error writing the RenderContext to the backend, {}", e);
|
warn!("Error writing the RenderContext to the backend, {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// explicitly close the `stdin` file handle
|
// explicitly close the `stdin` file handle
|
||||||
drop(stdin);
|
drop(stdin);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn command(&self) -> Result<Command> {
|
fn command(&self) -> Result<Command> {
|
||||||
|
@ -75,6 +81,8 @@ impl Preprocessor for CmdPreprocessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn supports_renderer(&self, renderer: &str) -> bool {
|
fn supports_renderer(&self, renderer: &str) -> bool {
|
||||||
|
debug!("Checking if the \"{}\" preprocessor supports \"{}\"", self.name(), renderer);
|
||||||
|
|
||||||
let mut cmd = match self.command() {
|
let mut cmd = match self.command() {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -83,29 +91,25 @@ impl Preprocessor for CmdPreprocessor {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
cmd
|
let outcome = cmd
|
||||||
.arg("supports")
|
.arg("supports")
|
||||||
.arg(renderer)
|
.arg(renderer)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::null())
|
||||||
.stdout(Stdio::inherit())
|
.stdout(Stdio::inherit())
|
||||||
.stderr(Stdio::inherit());
|
.stderr(Stdio::inherit())
|
||||||
|
.status()
|
||||||
|
.map(|status| status.code() == Some(0));
|
||||||
|
|
||||||
let child = match cmd.spawn() {
|
if let Err(ref e) = outcome {
|
||||||
Ok(c) => c,
|
if e.kind() == io::ErrorKind::NotFound {
|
||||||
Err(e) => {
|
warn!(
|
||||||
if e.kind() == io::ErrorKind::NotFound {
|
|
||||||
warn!(
|
|
||||||
"The command wasn't found, is the \"{}\" preprocessor installed?",
|
"The command wasn't found, is the \"{}\" preprocessor installed?",
|
||||||
self.name
|
self.name
|
||||||
);
|
);
|
||||||
warn!("\tCommand: {}", self.cmd);
|
warn!("\tCommand: {}", self.cmd);
|
||||||
}
|
|
||||||
|
|
||||||
// give it the benefit of the doubt
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
unimplemented!()
|
outcome.unwrap_or(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,19 @@ fn example() -> CmdPreprocessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_if_renderer_is_supported() {
|
fn example_supports_whatever() {
|
||||||
let cmd = example();
|
let cmd = example();
|
||||||
|
|
||||||
let got = cmd.supports_renderer("whatever");
|
let got = cmd.supports_renderer("whatever");
|
||||||
|
|
||||||
assert_eq!(got, true);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue