@@ -1,4 +1,4 @@
1−{-# LANGUAGE CPP, TupleSections, ScopedTypeVariables #-}
1+{-# LANGUAGE CPP, TupleSections, ScopedTypeVariables, PatternGuards #-}
22{-
33Copyright (C) 2006-2016 John MacFarlane <jgm@berkeley.edu>
44
@@ -1042,9 +1042,24 @@ adjustMetadata metadata d = return $ M.foldWithKey setMeta d metadata
10421042applyTransforms :: [Transform] -> Pandoc -> IO Pandoc
10431043applyTransforms transforms d = return $ foldr ($) d transforms
10441044
1045−applyFilters :: [FilePath] -> [String] -> Pandoc -> IO Pandoc
1046−applyFilters filters args d =
1047− foldrM ($) d $ map (flip externalFilter args) filters
1045+ -- First we check to see if a filter is a path. If it isn't, we
1046+ -- check to see whether it's in `userdir/filters`. If not, we leave
1047+ -- it unchanged.
1048+expandFilterPath :: Maybe FilePath -> FilePath -> IO FilePath
1049+expandFilterPath mbDatadir fp
1050+ | '/' `elem` fp = return fp
1051+ | Just datadir <- mbDatadir = do
1052+ let filterPath = (datadir </> "filters" </> fp)
1053+ filterPathExists <- doesFileExist filterPath
1054+ if filterPathExists
1055+ then return filterPath
1056+ else return fp
1057+ | otherwise = return fp
1058+
1059+applyFilters :: Maybe FilePath -> [FilePath] -> [String] -> Pandoc -> IO Pandoc
1060+applyFilters mbDatadir filters args d = do
1061+ expandedFilters <- mapM (expandFilterPath mbDatadir) filters
1062+ foldrM ($) d $ map (flip externalFilter args) expandedFilters
10481063
10491064uppercaseFirstLetter :: String -> String
10501065uppercaseFirstLetter (c:cs) = toUpper c : cs
@@ -1377,7 +1392,7 @@ convertWithOpts opts args = do
13771392 doc' <- (maybe return (extractMedia media) mbExtractMedia >=>
13781393 adjustMetadata metadata >=>
13791394 applyTransforms transforms >=>
1380− applyFilters filters' [format]) doc
1395+ applyFilters datadir filters' [format]) doc
13811396
13821397 let writeBinary :: B.ByteString -> IO ()
13831398 writeBinary = B.writeFile (UTF8.encodePath outputFile)