aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2014-04-05 15:51:04 -0700
committerJohn MacFarlane <fiddlosopher@gmail.com>2014-04-05 15:51:04 -0700
commit3fe6b57b60962ec531fe775052d16f0baa8ef120 (patch)
treed22e1d392f66baa0698b6cb9434934fdb42f882f
parentf2deb9d86d864c79c5ca18205b4ca565b7199413 (diff)
downloadpandoc-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.hs20
1 files 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