git.iko.soy/typeable/pandoc
on master help sign in
Cleaned up filter-finding.
7775374John MacFarlane
pandoc.hs+27−40
@@ -115,46 +115,33 @@ isTextFormat s = s `notElem` ["odt","docx","epub","epub3"]
115
115
116externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
116externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
117externalFilter f args' d = do
117externalFilter f args' d = do
118− mbexe <- if '/' `elem` f
119− -- don't check PATH if filter name has a path
120− then return Nothing
121− -- we catch isDoesNotExistError because this will
122− -- be triggered if PATH not set:
123− else E.catch (findExecutable f)
124− (\e -> if isDoesNotExistError e
125− then return Nothing
126− else throwIO e)
127− (f', args'') <- case mbexe of
128− Just x -> return (x, args')
129− Nothing -> do
130− exists <- doesFileExist f
118+ exists <- doesFileExist f
131− if exists
119+ isExecutable <- if exists
132− then do
133− isExecutable <- executable `fmap`
134− getPermissions f
120+ then executable <$> getPermissions f
135− return $
121+ else return True
122+ let (f', args'') = if exists
136− case map toLower $ takeExtension f of
123+ then case map toLower (takeExtension f) of
137− _ | isExecutable -> (f, args')
124+ _ | isExecutable -> ("." </> f, args')
138− ".py" -> ("python", f:args')
125+ ".py" -> ("python", f:args')
139− ".hs" -> ("runhaskell", f:args')
126+ ".hs" -> ("runhaskell", f:args')
140− ".pl" -> ("perl", f:args')
127+ ".pl" -> ("perl", f:args')
141− ".rb" -> ("ruby", f:args')
128+ ".rb" -> ("ruby", f:args')
142− ".php" -> ("php", f:args')
129+ ".php" -> ("php", f:args')
143− ".js" -> ("node", f:args')
130+ ".js" -> ("node", f:args')
144− _ -> (f, args')
131+ _ -> (f, args')
145− else err 85 $ "Filter " ++ f ++ " not found"
132+ else (f, args')
146− when (f' /= f) $ do
133+ unless (exists && isExecutable) $ do
147− mbExe <- findExecutable f'
134+ mbExe <- findExecutable f'
148− when (isNothing mbExe) $
135+ when (isNothing mbExe) $
149− err 83 $ "Error running filter " ++ f ++ "\n" ++
136+ err 83 $ "Error running filter " ++ f ++ ":\n" ++
150− show f' ++ " not found in path."
137+ "Could not find executable '" ++ f' ++ "'."
151− (exitcode, outbs, errbs) <- E.handle filterException $
138+ (exitcode, outbs, errbs) <- E.handle filterException $
152− pipeProcess Nothing f' args'' $ encode d
139+ pipeProcess Nothing f' args'' $ encode d
153− unless (B.null errbs) $ B.hPutStr stderr errbs
140+ unless (B.null errbs) $ B.hPutStr stderr errbs
154− case exitcode of
141+ case exitcode of
155− ExitSuccess -> return $ either error id $ eitherDecode' outbs
142+ ExitSuccess -> return $ either error id $ eitherDecode' outbs
156− ExitFailure ec -> err 83 $ "Error running filter " ++ f ++ "\n" ++
143+ ExitFailure ec -> err 83 $ "Error running filter " ++ f ++ "\n" ++
157− "Filter returned error status " ++ show ec
144+ "Filter returned error status " ++ show ec
158 where filterException :: E.SomeException -> IO a
145 where filterException :: E.SomeException -> IO a
159 filterException e = err 83 $ "Error running filter " ++ f ++ "\n" ++
146 filterException e = err 83 $ "Error running filter " ++ f ++ "\n" ++
160 show e
147 show e