From 3fe6b57b60962ec531fe775052d16f0baa8ef120 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 5 Apr 2014 15:51:04 -0700 Subject: 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. --- pandoc.hs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pandoc.hs b/pandoc.hs index 4a4e53bdd..6dd179ef9 100644 --- a/pandoc.hs +++ b/pandoc.hs @@ -48,7 +48,8 @@ import System.FilePath import System.Console.GetOpt import Data.Char ( toLower ) import Data.List ( intercalate, isPrefixOf, sort ) -import System.Directory ( getAppUserDataDirectory, findExecutable ) +import System.Directory ( getAppUserDataDirectory, findExecutable, + doesFileExist ) import System.IO ( stdout, stderr ) import System.IO.Error ( isDoesNotExistError ) import qualified Control.Exception as E @@ -97,8 +98,23 @@ isTextFormat s = takeWhile (`notElem` "+-") s `notElem` ["odt","docx","epub","ep externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc externalFilter f args' d = do + mbexe <- findExecutable f + (f', args'') <- case mbexe of + Just x -> return (x, args') + Nothing -> do + exists <- doesFileExist f + if exists + then return $ + case map toLower $ takeExtension f of + ".py" -> ("python", f:args') + ".hs" -> ("runhaskell", f:args') + ".pl" -> ("perl", f:args') + ".rb" -> ("ruby", f:args') + ".php" -> ("php", f:args') + _ -> (f, args') + else err 85 $ "Filter " ++ f ++ " not found" (exitcode, outbs, errbs) <- E.handle filterException $ - pipeProcess Nothing f args' $ encode d + pipeProcess Nothing f' args'' $ encode d when (not $ B.null errbs) $ B.hPutStr stderr errbs case exitcode of ExitSuccess -> return $ either error id $ eitherDecode' outbs -- cgit v1.2.3