diff options
author | John MacFarlane <fiddlosopher@gmail.com> | 2014-04-05 15:51:04 -0700 |
---|---|---|
committer | John MacFarlane <fiddlosopher@gmail.com> | 2014-04-05 15:51:04 -0700 |
commit | 3fe6b57b60962ec531fe775052d16f0baa8ef120 (patch) | |
tree | d22e1d392f66baa0698b6cb9434934fdb42f882f | |
parent | f2deb9d86d864c79c5ca18205b4ca565b7199413 (diff) | |
download | pandoc-3fe6b57b60962ec531fe775052d16f0baa8ef120.tar.gz |
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.
-rw-r--r-- | pandoc.hs | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -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 |