git.iko.soy/typeable/pandoc
on master help sign in
T.P.Filter: centralize filter path expansion
commit8ef995cfc0f9f53444fedf517c05fe02cb6311e7 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 27 Oct 2018, 09:38 (8 years ago) parentf612421
3 files changed+16−12
src/Text/Pandoc/Filter.hs+11−2 src/Text/Pandoc/Filter/JSON.hs+1−4 src/Text/Pandoc/Filter/Lua.hs+4−6
src/Text/Pandoc/Filter.hs+11−2
@@ -42,20 +42,29 @@ import Text.Pandoc.Definition (Pandoc)
4242import Text.Pandoc.Options (ReaderOptions)
4343import qualified Text.Pandoc.Filter.JSON as JSONFilter
4444import qualified Text.Pandoc.Filter.Lua as LuaFilter
45+import qualified Text.Pandoc.Filter.Path as Path
4546
47+-- | Type of filter and path to filter file.
4648data Filter = LuaFilter FilePath
4749 | JSONFilter FilePath
4850 deriving (Show)
4951
52+-- | Modify the given document using a filter.
5053applyFilters :: ReaderOptions
5154 -> [Filter]
5255 -> [String]
5356 -> Pandoc
5457 -> PandocIO Pandoc
55−applyFilters ropts filters args d =
56− foldrM ($) d $ map applyFilter filters
58+applyFilters ropts filters args d = do
59+ expandedFilters <- mapM expandFilterPath filters
60+ foldrM ($) d $ map applyFilter expandedFilters
5761 where
5862 applyFilter (JSONFilter f) = JSONFilter.apply ropts args f
5963 applyFilter (LuaFilter f) = LuaFilter.apply ropts args f
6064
65+-- | Expand paths of filters, searching the data directory.
66+expandFilterPath :: Filter -> PandocIO Filter
67+expandFilterPath (LuaFilter fp) = LuaFilter <$> Path.expandFilterPath fp
68+expandFilterPath (JSONFilter fp) = JSONFilter <$> Path.expandFilterPath fp
69+
6170$(deriveJSON defaultOptions ''Filter)
src/Text/Pandoc/Filter/JSON.hs+1−4
@@ -44,7 +44,6 @@ import System.FilePath ((</>), takeExtension)
4444import Text.Pandoc.Class (PandocIO)
4545import Text.Pandoc.Error (PandocError (PandocFilterError))
4646import Text.Pandoc.Definition (Pandoc)
47−import Text.Pandoc.Filter.Path (expandFilterPath)
4847import Text.Pandoc.Options (ReaderOptions)
4948import Text.Pandoc.Process (pipeProcess)
5049import Text.Pandoc.Shared (pandocVersion)
@@ -56,9 +55,7 @@ apply :: ReaderOptions
5655 -> FilePath
5756 -> Pandoc
5857 -> PandocIO Pandoc
59−apply ropts args f d = do
60− f' <- expandFilterPath f
61− liftIO $ externalFilter ropts f' args d
58+apply ropts args f = liftIO . externalFilter ropts f args
6259
6360externalFilter :: MonadIO m
6461 => ReaderOptions -> FilePath -> [String] -> Pandoc -> m Pandoc
src/Text/Pandoc/Filter/Lua.hs+4−6
@@ -36,7 +36,6 @@ import Control.Monad ((>=>))
3636import Text.Pandoc.Class (PandocIO)
3737import Text.Pandoc.Definition (Pandoc)
3838import Text.Pandoc.Error (PandocError (PandocFilterError))
39−import Text.Pandoc.Filter.Path (expandFilterPath)
4039import Text.Pandoc.Lua (Global (..), LuaException (..),
4140 runLua, runFilterFile, setGlobals)
4241import Text.Pandoc.Options (ReaderOptions)
@@ -49,17 +48,16 @@ apply :: ReaderOptions
4948 -> FilePath
5049 -> Pandoc
5150 -> PandocIO Pandoc
52−apply ropts args f doc = do
53− filterPath <- expandFilterPath f
51+apply ropts args fp doc = do
5452 let format = case args of
5553 (x:_) -> x
5654 _ -> error "Format not supplied for Lua filter"
57− runLua >=> forceResult filterPath $ do
55+ runLua >=> forceResult fp $ do
5856 setGlobals [ FORMAT format
5957 , PANDOC_READER_OPTIONS ropts
60− , PANDOC_SCRIPT_FILE filterPath
58+ , PANDOC_SCRIPT_FILE fp
6159 ]
62− runFilterFile filterPath doc
60+ runFilterFile fp doc
6361
6462forceResult :: FilePath -> Either LuaException Pandoc -> PandocIO Pandoc
6563forceResult fp eitherResult = case eitherResult of