@@ -2,7 +2,7 @@ use anyhow::{Context, Result, ensure};
22use log:: { debug, trace, warn} ;
33use mdbook_core:: book:: Book ;
44use mdbook_preprocessor:: { Preprocessor , PreprocessorContext } ;
5- use std:: io:: { self , Write } ;
5+ use std:: io:: Write ;
66use std:: path:: PathBuf ;
77use std:: process:: { Child , Stdio } ;
88
@@ -34,12 +34,18 @@ pub struct CmdPreprocessor {
3434 name : String ,
3535 cmd : String ,
3636 root : PathBuf ,
37+ optional : bool ,
3738}
3839
3940impl CmdPreprocessor {
4041 /// Create a new `CmdPreprocessor`.
41- pub fn new ( name : String , cmd : String , root : PathBuf ) -> CmdPreprocessor {
42- CmdPreprocessor { name, cmd, root }
42+ pub fn new ( name : String , cmd : String , root : PathBuf , optional : bool ) -> CmdPreprocessor {
43+ CmdPreprocessor {
44+ name,
45+ cmd,
46+ root,
47+ optional,
48+ }
4349 }
4450
4551 fn write_input_to_child ( & self , child : & mut Child , book : & Book , ctx : & PreprocessorContext ) {
@@ -75,18 +81,29 @@ impl Preprocessor for CmdPreprocessor {
7581 fn run ( & self , ctx : & PreprocessorContext , book : Book ) -> Result < Book > {
7682 let mut cmd = crate :: compose_command ( & self . cmd , & ctx. root ) ?;
7783
78- let mut child = cmd
84+ let mut child = match cmd
7985 . stdin ( Stdio :: piped ( ) )
8086 . stdout ( Stdio :: piped ( ) )
8187 . stderr ( Stdio :: inherit ( ) )
8288 . current_dir ( & self . root )
8389 . spawn ( )
84- . with_context ( || {
85- format ! (
86- "Unable to start the \" {}\" preprocessor. Is it installed?" ,
87- self . name( )
88- )
89- } ) ?;
90+ {
91+ Ok ( c) => c,
92+ Err ( e) => {
93+ crate :: handle_command_error (
94+ e,
95+ self . optional ,
96+ "preprocessor" ,
97+ "preprocessor" ,
98+ & self . name ,
99+ & self . cmd ,
100+ ) ?;
101+ // This should normally not be reached, since the validation
102+ // for NotFound should have already happened when running the
103+ // "supports" command.
104+ return Ok ( book) ;
105+ }
106+ } ;
90107
91108 self . write_input_to_child ( & mut child, & book, ctx) ;
92109
@@ -114,46 +131,37 @@ impl Preprocessor for CmdPreprocessor {
114131 } )
115132 }
116133
117- fn supports_renderer ( & self , renderer : & str ) -> bool {
134+ fn supports_renderer ( & self , renderer : & str ) -> Result < bool > {
118135 debug ! (
119136 "Checking if the \" {}\" preprocessor supports \" {}\" " ,
120137 self . name( ) ,
121138 renderer
122139 ) ;
123140
124- let mut cmd = match crate :: compose_command ( & self . cmd , & self . root ) {
125- Ok ( c) => c,
126- Err ( e) => {
127- warn ! (
128- "Unable to create the command for the \" {}\" preprocessor, {}" ,
129- self . name( ) ,
130- e
131- ) ;
132- return false ;
133- }
134- } ;
141+ let mut cmd = crate :: compose_command ( & self . cmd , & self . root ) ?;
135142
136- let outcome = cmd
143+ match cmd
137144 . arg ( "supports" )
138145 . arg ( renderer)
139146 . stdin ( Stdio :: null ( ) )
140147 . stdout ( Stdio :: inherit ( ) )
141148 . stderr ( Stdio :: inherit ( ) )
142149 . current_dir ( & self . root )
143150 . status ( )
144- . map ( |status| status. code ( ) == Some ( 0 ) ) ;
145-
146- if let Err ( ref e) = outcome {
147- if e. kind ( ) == io:: ErrorKind :: NotFound {
148- warn ! (
149- "The command wasn't found, is the \" {}\" preprocessor installed?" ,
150- self . name
151- ) ;
152- warn ! ( "\t Command: {}" , self . cmd) ;
151+ {
152+ Ok ( status) => Ok ( status. code ( ) == Some ( 0 ) ) ,
153+ Err ( e) => {
154+ crate :: handle_command_error (
155+ e,
156+ self . optional ,
157+ "preprocessor" ,
158+ "preprocessor" ,
159+ & self . name ,
160+ & self . cmd ,
161+ ) ?;
162+ Ok ( false )
153163 }
154164 }
155-
156- outcome. unwrap_or ( false )
157165 }
158166}
159167
@@ -171,7 +179,12 @@ mod tests {
171179 #[ test]
172180 fn round_trip_write_and_parse_input ( ) {
173181 let md = guide ( ) ;
174- let cmd = CmdPreprocessor :: new ( "test" . to_string ( ) , "test" . to_string ( ) , md. root . clone ( ) ) ;
182+ let cmd = CmdPreprocessor :: new (
183+ "test" . to_string ( ) ,
184+ "test" . to_string ( ) ,
185+ md. root . clone ( ) ,
186+ false ,
187+ ) ;
175188 let ctx = PreprocessorContext :: new (
176189 md. root . clone ( ) ,
177190 md. config . clone ( ) ,
0 commit comments