summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Lua
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2022-09-12 21:35:26 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2022-09-20 14:11:22 -0700
commit47a7cdecb970f7cd87019e8def8ccd2496d32677 (patch)
tree8815126c6bca30877eebac67160b58795929bbdb /src/Text/Pandoc/Lua
parent7c07e57b6037fe1d1d941df70f53d974d4726796 (diff)
Export `applyFilter` from T.P.Lua.Filter.
Motivation: now we need not have any logic referring to hslua types (LuaE) in T.P.Filter.Lua. This restricts the parts of Pandoc that deal directly with hslua to T.P.Lua (and in tests, Tests.Lua). [API changes] T.P.Lua now exports `applyFilter`, `readCustom`, and `writeCustom`. The lower-level function `runFilterFile` is no longer exported.
Diffstat (limited to 'src/Text/Pandoc/Lua')
-rw-r--r--src/Text/Pandoc/Lua/Filter.hs32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Lua/Filter.hs b/src/Text/Pandoc/Lua/Filter.hs
index da8af9a26..b920f358d 100644
--- a/src/Text/Pandoc/Lua/Filter.hs
+++ b/src/Text/Pandoc/Lua/Filter.hs
@@ -13,15 +13,22 @@ Stability : alpha
Types and functions for running Lua filters.
-}
module Text.Pandoc.Lua.Filter
- ( runFilterFile
+ ( applyFilter
) where
import Control.Monad ((>=>), (<$!>))
import HsLua as Lua
import Text.Pandoc.Definition
-import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.ErrorConversion ()
import Text.Pandoc.Lua.Marshal.AST
import Text.Pandoc.Lua.Marshal.Filter
+import Text.Pandoc.Lua.Global (Global (..), setGlobals)
+import Text.Pandoc.Lua.Init (runLua)
+import Control.Exception (throw)
+import qualified Data.Text as T
+import Text.Pandoc.Class (PandocMonad)
+import Control.Monad.Trans (MonadIO)
+import Text.Pandoc.Error (PandocError (PandocFilterError, PandocLuaError))
+
-- | Transform document using the filter defined in the given file.
@@ -44,3 +51,24 @@ runFilterFile filterPath doc = do
runAll :: [Filter] -> Pandoc -> LuaE PandocError Pandoc
runAll = foldr ((>=>) . applyFully) return
+
+-- | Run the Lua filter in @filterPath@ for a transformation to the
+-- target format (first element in args). Pandoc uses Lua init files to
+-- setup the Lua interpreter.
+applyFilter :: (PandocMonad m, MonadIO m)
+ => [Global]
+ -> FilePath
+ -> Pandoc
+ -> m Pandoc
+applyFilter globals fp doc = do
+ runLua >=> forceResult fp $ do
+ setGlobals globals
+ runFilterFile fp doc
+
+forceResult :: (PandocMonad m, MonadIO m)
+ => FilePath -> Either PandocError Pandoc -> m Pandoc
+forceResult fp eitherResult = case eitherResult of
+ Right x -> return x
+ Left err -> throw . PandocFilterError (T.pack fp) $ case err of
+ PandocLuaError msg -> msg
+ _ -> T.pack $ show err