@@ -46,12 +46,11 @@ import qualified Control.Exception as E
46import Control.Monad
46import Control.Monad
47import Control.Monad.Except (catchError, throwError)
47import Control.Monad.Except (catchError, throwError)
48import Control.Monad.Trans
48import Control.Monad.Trans
49−import Data.Aeson (defaultOptions, eitherDecode', encode)
49+import Data.Aeson (defaultOptions)
50import Data.Aeson.TH (deriveJSON)
50import Data.Aeson.TH (deriveJSON)
51import qualified Data.ByteString as BS
51import qualified Data.ByteString as BS
52import qualified Data.ByteString.Lazy as B
52import qualified Data.ByteString.Lazy as B
53import Data.Char (toLower, toUpper)
53import Data.Char (toLower, toUpper)
54−import Data.Foldable (foldrM)
55import Data.List (find, intercalate, isPrefixOf, isSuffixOf, sort)
54import Data.List (find, intercalate, isPrefixOf, isSuffixOf, sort)
56import qualified Data.Map as M
55import qualified Data.Map as M
57import Data.Maybe (fromMaybe, isJust, isNothing)
56import Data.Maybe (fromMaybe, isJust, isNothing)
@@ -73,10 +72,9 @@ import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme,
74import Skylighting.Parser (addSyntaxDefinition, parseSyntaxDefinition)
73import Skylighting.Parser (addSyntaxDefinition, parseSyntaxDefinition)
75import System.Console.GetOpt
74import System.Console.GetOpt
76−import System.Directory (Permissions (..), doesFileExist, findExecutable,
77− getAppUserDataDirectory, getPermissions)
75+import System.Directory (getAppUserDataDirectory)
78−import System.Environment (getArgs, getEnvironment, getProgName)
76+import System.Environment (getArgs, getProgName)
79−import System.Exit (ExitCode (..), exitSuccess)
77+import System.Exit (exitSuccess)
80import System.FilePath
78import System.FilePath
81import System.IO (nativeNewline, stdout)
79import System.IO (nativeNewline, stdout)
82import qualified System.IO as IO (Newline (..))
80import qualified System.IO as IO (Newline (..))
@@ -84,10 +82,9 @@ import System.IO.Error (isDoesNotExistError)
84import Text.Pandoc
82import Text.Pandoc
85import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
83import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
86import Text.Pandoc.Builder (setMeta, deleteMeta)
84import Text.Pandoc.Builder (setMeta, deleteMeta)
85+import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), applyFilters)
87import Text.Pandoc.Highlighting (highlightingStyles)
86import Text.Pandoc.Highlighting (highlightingStyles)
88−import Text.Pandoc.Lua (LuaException (..), runLuaFilter)
89import Text.Pandoc.PDF (makePDF)
87import Text.Pandoc.PDF (makePDF)
90−import Text.Pandoc.Process (pipeProcess)
91import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
88import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
92import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
89import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
93 headerShift, isURI, ordNub, safeRead, tabFilter)
90 headerShift, isURI, ordNub, safeRead, tabFilter)
@@ -538,48 +535,6 @@ type Transform = Pandoc -> Pandoc
538isTextFormat :: String -> Bool
535isTextFormat :: String -> Bool
539isTextFormat s = s `notElem` ["odt","docx","epub2","epub3","epub","pptx"]
536isTextFormat s = s `notElem` ["odt","docx","epub2","epub3","epub","pptx"]
541−externalFilter :: MonadIO m
542− => ReaderOptions -> FilePath -> [String] -> Pandoc -> m Pandoc
543−externalFilter ropts f args' d = liftIO $ do
544− exists <- doesFileExist f
545− isExecutable <- if exists
546− then executable <$> getPermissions f
548− let (f', args'') = if exists
549− then case map toLower (takeExtension f) of
550− _ | isExecutable -> ("." </> f, args')
551− ".py" -> ("python", f:args')
552− ".hs" -> ("runhaskell", f:args')
553− ".pl" -> ("perl", f:args')
554− ".rb" -> ("ruby", f:args')
555− ".php" -> ("php", f:args')
556− ".js" -> ("node", f:args')
557− ".r" -> ("Rscript", f:args')
560− unless (exists && isExecutable) $ do
561− mbExe <- findExecutable f'
562− when (isNothing mbExe) $
563− E.throwIO $ PandocFilterError f ("Could not find executable " ++ f')
564− env <- getEnvironment
566− ( ("PANDOC_VERSION", pandocVersion)
567− : ("PANDOC_READER_OPTIONS", UTF8.toStringLazy (encode ropts))
569− (exitcode, outbs) <- E.handle filterException $
570− pipeProcess env' f' args'' $ encode d
572− ExitSuccess -> either (E.throwIO . PandocFilterError f)
573− return $ eitherDecode' outbs
574− ExitFailure ec -> E.throwIO $ PandocFilterError f
575− ("Filter returned error status " ++ show ec)
576− where filterException :: E.SomeException -> IO a
577− filterException e = E.throwIO $ PandocFilterError f (show e)
579−data Filter = LuaFilter FilePath
580− | JSONFilter FilePath
583-- | Data structure for command line options.
538-- | Data structure for command line options.
584data Opt = Opt
539data Opt = Opt
585 { optTabStop :: Int -- ^ Number of spaces per tab
540 { optTabStop :: Int -- ^ Number of spaces per tab
@@ -824,50 +779,6 @@ defaultWriterName x =
824applyTransforms :: Monad m => [Transform] -> Pandoc -> m Pandoc
779applyTransforms :: Monad m => [Transform] -> Pandoc -> m Pandoc
825applyTransforms transforms d = return $ foldr ($) d transforms
780applyTransforms transforms d = return $ foldr ($) d transforms
827− -- First we check to see if a filter is found. If not, and if it's
828− -- not an absolute path, we check to see whether it's in `userdir/filters`.
829− -- If not, we leave it unchanged.
830−expandFilterPath :: PandocMonad m => FilePath -> m FilePath
831−expandFilterPath fp = do
832− mbDatadir <- getUserDataDir
833− fpExists <- fileExists fp
836− else case mbDatadir of
837− Just datadir | isRelative fp -> do
838− let filterPath = datadir </> "filters" </> fp
839− filterPathExists <- fileExists filterPath
841− then return filterPath
845−applyFilters :: ReaderOptions
850−applyFilters ropts filters args d = do
851− foldrM ($) d $ map (applyFilter ropts args) filters
853−applyFilter :: ReaderOptions
858−applyFilter ropts args (LuaFilter f) d = do
859− f' <- expandFilterPath f
860− let format = case args of
862− _ -> error "Format not supplied for lua filter"
863− res <- runLuaFilter ropts f' format d
866− Left (LuaException s) -> E.throw (PandocFilterError f s)
867−applyFilter ropts args (JSONFilter f) d = do
868− f' <- expandFilterPath f
869− liftIO $ externalFilter ropts f' args d
871readSource :: FilePath -> PandocIO Text
782readSource :: FilePath -> PandocIO Text
872readSource "-" = liftIO (UTF8.toText <$> BS.getContents)
783readSource "-" = liftIO (UTF8.toText <$> BS.getContents)
873readSource src = case parseURI src of
784readSource src = case parseURI src of
@@ -1722,5 +1633,4 @@ deprecatedOption o msg =
1722-- see https://github.com/jgm/pandoc/pull/4083
1633-- see https://github.com/jgm/pandoc/pull/4083
1723-- using generic deriving caused long compilation times
1634-- using generic deriving caused long compilation times
1724$(deriveJSON defaultOptions ''LineEnding)
1635$(deriveJSON defaultOptions ''LineEnding)
1725−$(deriveJSON defaultOptions ''Filter)
1726$(deriveJSON defaultOptions ''Opt)
1636$(deriveJSON defaultOptions ''Opt)