git.iko.soy/typeable/pandoc
on master help sign in
Cleaned up filter-finding.

* Removed a hardcoded '/' that may have caused problems with Windows paths. * Cleaned up the logic.

commit77753747d111966610b9c3cdcbe75712540bc816 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 21 Nov 2016, 22:18 (10 years ago) parentac6dfe0
1 file changed+27−40
pandoc.hs+27−40
pandoc.hs+27−40
@@ -115,46 +115,33 @@ isTextFormat s = s `notElem` ["odt","docx","epub","epub3"]
115115
116116externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
117117externalFilter 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
131− if exists
132− then do
133− isExecutable <- executable `fmap`
134− getPermissions f
135− return $
136− case map toLower $ takeExtension f of
137− _ | isExecutable -> (f, args')
138− ".py" -> ("python", f:args')
139− ".hs" -> ("runhaskell", f:args')
140− ".pl" -> ("perl", f:args')
141− ".rb" -> ("ruby", f:args')
142− ".php" -> ("php", f:args')
143− ".js" -> ("node", f:args')
144− _ -> (f, args')
145− else err 85 $ "Filter " ++ f ++ " not found"
146− when (f' /= f) $ do
147− mbExe <- findExecutable f'
148− when (isNothing mbExe) $
149− err 83 $ "Error running filter " ++ f ++ "\n" ++
150− show f' ++ " not found in path."
151− (exitcode, outbs, errbs) <- E.handle filterException $
152− pipeProcess Nothing f' args'' $ encode d
153− unless (B.null errbs) $ B.hPutStr stderr errbs
154− case exitcode of
155− ExitSuccess -> return $ either error id $ eitherDecode' outbs
156− ExitFailure ec -> err 83 $ "Error running filter " ++ f ++ "\n" ++
157− "Filter returned error status " ++ show ec
118+ exists <- doesFileExist f
119+ isExecutable <- if exists
120+ then executable <$> getPermissions f
121+ else return True
122+ let (f', args'') = if exists
123+ then case map toLower (takeExtension f) of
124+ _ | isExecutable -> ("." </> f, args')
125+ ".py" -> ("python", f:args')
126+ ".hs" -> ("runhaskell", f:args')
127+ ".pl" -> ("perl", f:args')
128+ ".rb" -> ("ruby", f:args')
129+ ".php" -> ("php", f:args')
130+ ".js" -> ("node", f:args')
131+ _ -> (f, args')
132+ else (f, args')
133+ unless (exists && isExecutable) $ do
134+ mbExe <- findExecutable f'
135+ when (isNothing mbExe) $
136+ err 83 $ "Error running filter " ++ f ++ ":\n" ++
137+ "Could not find executable '" ++ f' ++ "'."
138+ (exitcode, outbs, errbs) <- E.handle filterException $
139+ pipeProcess Nothing f' args'' $ encode d
140+ unless (B.null errbs) $ B.hPutStr stderr errbs
141+ case exitcode of
142+ ExitSuccess -> return $ either error id $ eitherDecode' outbs
143+ ExitFailure ec -> err 83 $ "Error running filter " ++ f ++ "\n" ++
144+ "Filter returned error status " ++ show ec
158145 where filterException :: E.SomeException -> IO a
159146 filterException e = err 83 $ "Error running filter " ++ f ++ "\n" ++
160147 show e