git.iko.soy/typeable/pandoc
on master help sign in
Filter changes.

* Previously we ran all lua filters before JSON filters. * Now we run filters in the order they are presented on the command line, whether lua or JSON. * The type of `applyFilters` has changed (incompatible API change). * `applyLuaFilters` has been removed (incompatible API change). * Bump version to 2.1. See #4196.

commit8b575dbf845f82c6750a71f7372bae7067502554 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 26 Dec 2017, 10:11 (9 years ago) parent9029206
2 files changed+47−35
pandoc.cabal+1−1 src/Text/Pandoc/App.hs+46−34
pandoc.cabal+1−1
@@ -1,5 +1,5 @@
11name: pandoc
2−version: 2.0.6
2+version: 2.1
33cabal-version: >= 1.10
44build-type: Custom
55license: GPL
src/Text/Pandoc/App.hs+46−34
@@ -35,11 +35,12 @@ Does a pandoc conversion based on command-line options.
3535module Text.Pandoc.App (
3636 convertWithOpts
3737 , Opt(..)
38+ , LineEnding(..)
39+ , Filter(..)
3840 , defaultOpts
3941 , parseOptions
4042 , options
4143 , applyFilters
42− , applyLuaFilters
4344 ) where
4445import qualified Control.Exception as E
4546import Control.Monad
@@ -184,11 +185,13 @@ convertWithOpts opts = do
184185 Nothing -> return Nothing
185186 Just fp -> Just <$> UTF8.readFile fp
186187
188+ let isPandocCiteproc (JSONFilter f) = takeBaseName f == "pandoc-citeproc"
189+ isPandocCiteproc _ = False
187190 -- --bibliography implies -F pandoc-citeproc for backwards compatibility:
188191 let needsCiteproc = isJust (lookup "bibliography" (optMetadata opts)) &&
189192 optCiteMethod opts `notElem` [Natbib, Biblatex] &&
190− "pandoc-citeproc" `notElem` map takeBaseName filters
191− let filters' = if needsCiteproc then "pandoc-citeproc" : filters
193+ all (not . isPandocCiteproc) filters
194+ let filters' = if needsCiteproc then JSONFilter "pandoc-citeproc" : filters
192195 else filters
193196
194197 let sources = case optInputFiles opts of
@@ -501,10 +504,9 @@ convertWithOpts opts = do
501504 then fillMediaBag
502505 else return)
503506 >=> return . addMetadata metadata
504− >=> applyLuaFilters datadir (optLuaFilters opts) format
505− >=> maybe return extractMedia (optExtractMedia opts)
506507 >=> applyTransforms transforms
507− >=> applyFilters readerOpts datadir filters' [format]
508+ >=> applyFilters readerOpts filters' [format]
509+ >=> maybe return extractMedia (optExtractMedia opts)
508510 )
509511
510512 case writer of
@@ -583,6 +585,10 @@ externalFilter ropts f args' d = liftIO $ do
583585 where filterException :: E.SomeException -> IO a
584586 filterException e = E.throwIO $ PandocFilterError f (show e)
585587
588+data Filter = LuaFilter FilePath
589+ | JSONFilter FilePath
590+ deriving (Show)
591+
586592-- | Data structure for command line options.
587593data Opt = Opt
588594 { optTabStop :: Int -- ^ Number of spaces per tab
@@ -626,8 +632,7 @@ data Opt = Opt
626632 , optDpi :: Int -- ^ Dpi
627633 , optWrapText :: WrapOption -- ^ Options for wrapping text
628634 , optColumns :: Int -- ^ Line length in characters
629− , optFilters :: [FilePath] -- ^ Filters to apply
630− , optLuaFilters :: [FilePath] -- ^ Lua filters to apply
635+ , optFilters :: [Filter] -- ^ Filters to apply
631636 , optEmailObfuscation :: ObfuscationMethod
632637 , optIdentifierPrefix :: String
633638 , optStripEmptyParagraphs :: Bool -- ^ Strip empty paragraphs
@@ -700,7 +705,6 @@ defaultOpts = Opt
700705 , optWrapText = WrapAuto
701706 , optColumns = 72
702707 , optFilters = []
703− , optLuaFilters = []
704708 , optEmailObfuscation = NoObfuscation
705709 , optIdentifierPrefix = ""
706710 , optStripEmptyParagraphs = False
@@ -832,41 +836,46 @@ applyTransforms transforms d = return $ foldr ($) d transforms
832836 -- First we check to see if a filter is found. If not, and if it's
833837 -- not an absolute path, we check to see whether it's in `userdir/filters`.
834838 -- If not, we leave it unchanged.
835−expandFilterPath :: MonadIO m => Maybe FilePath -> FilePath -> m FilePath
836−expandFilterPath mbDatadir fp = liftIO $ do
837− fpExists <- doesFileExist fp
839+expandFilterPath :: PandocMonad m => FilePath -> m FilePath
840+expandFilterPath fp = do
841+ mbDatadir <- getUserDataDir
842+ fpExists <- fileExists fp
838843 if fpExists
839844 then return fp
840845 else case mbDatadir of
841846 Just datadir | isRelative fp -> do
842847 let filterPath = datadir </> "filters" </> fp
843− filterPathExists <- doesFileExist filterPath
848+ filterPathExists <- fileExists filterPath
844849 if filterPathExists
845850 then return filterPath
846851 else return fp
847852 _ -> return fp
848853
849−applyLuaFilters :: Maybe FilePath -> [FilePath] -> String -> Pandoc
850− -> PandocIO Pandoc
851−applyLuaFilters mbDatadir filters format d = do
852− expandedFilters <- mapM (expandFilterPath mbDatadir) filters
853− let go f d' = do
854− res <- runLuaFilter f format d'
855− case res of
856− Right x -> return x
857− Left (LuaException s) -> E.throw (PandocFilterError f s)
858− foldrM ($) d $ map go expandedFilters
859−
860−applyFilters :: MonadIO m
861− => ReaderOptions
862− -> Maybe FilePath
863− -> [FilePath]
854+applyFilters :: ReaderOptions
855+ -> [Filter]
864856 -> [String]
865857 -> Pandoc
866− -> m Pandoc
867−applyFilters ropts mbDatadir filters args d = do
868− expandedFilters <- mapM (expandFilterPath mbDatadir) filters
869− foldrM ($) d $ map (flip (externalFilter ropts) args) expandedFilters
858+ -> PandocIO Pandoc
859+applyFilters ropts filters args d = do
860+ foldrM ($) d $ map (applyFilter ropts args) filters
861+
862+applyFilter :: ReaderOptions
863+ -> [String]
864+ -> Filter
865+ -> Pandoc
866+ -> PandocIO Pandoc
867+applyFilter _ropts args (LuaFilter f) d = do
868+ f' <- expandFilterPath f
869+ let format = case args of
870+ (x:_) -> x
871+ _ -> error "Format not supplied for lua filter"
872+ res <- runLuaFilter f' format d
873+ case res of
874+ Right x -> return x
875+ Left (LuaException s) -> E.throw (PandocFilterError f s)
876+applyFilter ropts args (JSONFilter f) d = do
877+ f' <- expandFilterPath f
878+ liftIO $ externalFilter ropts f' args d
870879
871880readSource :: FilePath -> PandocIO Text
872881readSource "-" = liftIO (UTF8.toText <$> BS.getContents)
@@ -968,13 +977,15 @@ options =
968977
969978 , Option "F" ["filter"]
970979 (ReqArg
971− (\arg opt -> return opt { optFilters = arg : optFilters opt })
980+ (\arg opt -> return opt { optFilters =
981+ JSONFilter arg : optFilters opt })
972982 "PROGRAM")
973983 "" -- "External JSON filter"
974984
975985 , Option "" ["lua-filter"]
976986 (ReqArg
977− (\arg opt -> return opt { optLuaFilters = arg : optLuaFilters opt })
987+ (\arg opt -> return opt { optFilters =
988+ LuaFilter arg : optFilters opt })
978989 "SCRIPTPATH")
979990 "" -- "Lua filter"
980991
@@ -1720,4 +1731,5 @@ deprecatedOption o msg =
17201731-- see https://github.com/jgm/pandoc/pull/4083
17211732-- using generic deriving caused long compilation times
17221733$(deriveJSON defaultOptions ''LineEnding)
1734+$(deriveJSON defaultOptions ''Filter)
17231735$(deriveJSON defaultOptions ''Opt)