1- use anyhow:: { Context , Result , bail , ensure} ;
1+ use anyhow:: { Context , Result , ensure} ;
22use log:: { debug, trace, warn} ;
33use mdbook_core:: book:: Book ;
44use mdbook_preprocessor:: { Preprocessor , PreprocessorContext } ;
5- use shlex:: Shlex ;
65use std:: io:: { self , Write } ;
7- use std:: process:: { Child , Command , Stdio } ;
6+ use std:: path:: PathBuf ;
7+ use std:: process:: { Child , Stdio } ;
88
99/// A custom preprocessor which will shell out to a 3rd-party program.
1010///
@@ -33,12 +33,13 @@ use std::process::{Child, Command, Stdio};
3333pub struct CmdPreprocessor {
3434 name : String ,
3535 cmd : String ,
36+ root : PathBuf ,
3637}
3738
3839impl CmdPreprocessor {
3940 /// Create a new `CmdPreprocessor`.
40- pub fn new ( name : String , cmd : String ) -> CmdPreprocessor {
41- CmdPreprocessor { name, cmd }
41+ pub fn new ( name : String , cmd : String , root : PathBuf ) -> CmdPreprocessor {
42+ CmdPreprocessor { name, cmd, root }
4243 }
4344
4445 fn write_input_to_child ( & self , child : & mut Child , book : & Book , ctx : & PreprocessorContext ) {
@@ -64,22 +65,6 @@ impl CmdPreprocessor {
6465 pub fn cmd ( & self ) -> & str {
6566 & self . cmd
6667 }
67-
68- fn command ( & self ) -> Result < Command > {
69- let mut words = Shlex :: new ( & self . cmd ) ;
70- let executable = match words. next ( ) {
71- Some ( e) => e,
72- None => bail ! ( "Command string was empty" ) ,
73- } ;
74-
75- let mut cmd = Command :: new ( executable) ;
76-
77- for arg in words {
78- cmd. arg ( arg) ;
79- }
80-
81- Ok ( cmd)
82- }
8368}
8469
8570impl Preprocessor for CmdPreprocessor {
@@ -88,12 +73,13 @@ impl Preprocessor for CmdPreprocessor {
8873 }
8974
9075 fn run ( & self , ctx : & PreprocessorContext , book : Book ) -> Result < Book > {
91- let mut cmd = self . command ( ) ?;
76+ let mut cmd = crate :: compose_command ( & self . cmd , & ctx . root ) ?;
9277
9378 let mut child = cmd
9479 . stdin ( Stdio :: piped ( ) )
9580 . stdout ( Stdio :: piped ( ) )
9681 . stderr ( Stdio :: inherit ( ) )
82+ . current_dir ( & self . root )
9783 . spawn ( )
9884 . with_context ( || {
9985 format ! (
@@ -135,7 +121,7 @@ impl Preprocessor for CmdPreprocessor {
135121 renderer
136122 ) ;
137123
138- let mut cmd = match self . command ( ) {
124+ let mut cmd = match crate :: compose_command ( & self . cmd , & self . root ) {
139125 Ok ( c) => c,
140126 Err ( e) => {
141127 warn ! (
@@ -153,6 +139,7 @@ impl Preprocessor for CmdPreprocessor {
153139 . stdin ( Stdio :: null ( ) )
154140 . stdout ( Stdio :: inherit ( ) )
155141 . stderr ( Stdio :: inherit ( ) )
142+ . current_dir ( & self . root )
156143 . status ( )
157144 . map ( |status| status. code ( ) == Some ( 0 ) ) ;
158145
@@ -183,8 +170,8 @@ mod tests {
183170
184171 #[ test]
185172 fn round_trip_write_and_parse_input ( ) {
186- let cmd = CmdPreprocessor :: new ( "test" . to_string ( ) , "test" . to_string ( ) ) ;
187173 let md = guide ( ) ;
174+ let cmd = CmdPreprocessor :: new ( "test" . to_string ( ) , "test" . to_string ( ) , md. root . clone ( ) ) ;
188175 let ctx = PreprocessorContext :: new (
189176 md. root . clone ( ) ,
190177 md. config . clone ( ) ,
0 commit comments