git.iko.soy/typeable/pandoc
on master help sign in
Make it possible to run filters that aren't executable.

Pandoc first tries to find the executable (searching the path if path isn't given). If it fails, but the file exists and has a .py, .pl, .rb, .hs, or .php extension, pandoc runs the filter using the appropriate interpreter. This should make it easier to use filters on Windows, and make it more convenient for everyone. Closes #1096.

commit3fe6b57b60962ec531fe775052d16f0baa8ef120 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 5 Apr 2014, 15:51 (13 years ago) parentf2deb9d
1 file changed+18−2
pandoc.hs+18−2
pandoc.hs+18−2
@@ -48,7 +48,8 @@ import System.FilePath
4848import System.Console.GetOpt
4949import Data.Char ( toLower )
5050import Data.List ( intercalate, isPrefixOf, sort )
51−import System.Directory ( getAppUserDataDirectory, findExecutable )
51+import System.Directory ( getAppUserDataDirectory, findExecutable,
52+ doesFileExist )
5253import System.IO ( stdout, stderr )
5354import System.IO.Error ( isDoesNotExistError )
5455import qualified Control.Exception as E
@@ -97,8 +98,23 @@ isTextFormat s = takeWhile (`notElem` "+-") s `notElem` ["odt","docx","epub","ep
9798
9899externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
99100externalFilter f args' d = do
101+ mbexe <- findExecutable f
102+ (f', args'') <- case mbexe of
103+ Just x -> return (x, args')
104+ Nothing -> do
105+ exists <- doesFileExist f
106+ if exists
107+ then return $
108+ case map toLower $ takeExtension f of
109+ ".py" -> ("python", f:args')
110+ ".hs" -> ("runhaskell", f:args')
111+ ".pl" -> ("perl", f:args')
112+ ".rb" -> ("ruby", f:args')
113+ ".php" -> ("php", f:args')
114+ _ -> (f, args')
115+ else err 85 $ "Filter " ++ f ++ " not found"
100116 (exitcode, outbs, errbs) <- E.handle filterException $
101− pipeProcess Nothing f args' $ encode d
117+ pipeProcess Nothing f' args'' $ encode d
102118 when (not $ B.null errbs) $ B.hPutStr stderr errbs
103119 case exitcode of
104120 ExitSuccess -> return $ either error id $ eitherDecode' outbs