From f6124213073cf162bd3cfa5e6d455807e8a502a9 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Fri, 26 Oct 2018 23:45:01 +0200 Subject: T.P.Lua: expose more useful internals (API change) Newly exported from Text.Pandoc.Lua: - `runFilterFile` to run a Lua filter from file; - data type `Global` and its constructors; and - `setGlobals` to add globals to a Lua environment. This module also contains `Pushable` and `Peekable` instances required to get pandoc's data types to and from Lua. Low-level Lua operation remain hidden in Text.Pandoc.Lua. --- src/Text/Pandoc/Lua/Filter.hs | 20 ++++++++++++++++++++ src/Text/Pandoc/Lua/Init.hs | 3 +-- 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src/Text/Pandoc/Lua') diff --git a/src/Text/Pandoc/Lua/Filter.hs b/src/Text/Pandoc/Lua/Filter.hs index d17f9a969..d47d3bd9b 100644 --- a/src/Text/Pandoc/Lua/Filter.hs +++ b/src/Text/Pandoc/Lua/Filter.hs @@ -30,6 +30,7 @@ Types and functions for running Lua filters. -} module Text.Pandoc.Lua.Filter ( LuaFilterFunction , LuaFilter + , runFilterFile , tryFilter , runFilterFunction , walkMWithLuaFilter @@ -54,6 +55,25 @@ import qualified Data.Map.Strict as Map import qualified Foreign.Lua as Lua import qualified Text.Pandoc.Lua.Util as LuaUtil +-- | Transform document using the filter defined in the given file. +runFilterFile :: FilePath -> Pandoc -> Lua Pandoc +runFilterFile filterPath doc = do + top <- Lua.gettop + stat <- LuaUtil.dofileWithTraceback filterPath + if stat /= Lua.OK + then Lua.throwTopMessage + else do + newtop <- Lua.gettop + -- Use the returned filters, or the implicitly defined global + -- filter if nothing was returned. + luaFilters <- if newtop - top >= 1 + then Lua.peek Lua.stackTop + else Lua.pushglobaltable *> fmap (:[]) Lua.popValue + runAll luaFilters doc + +runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc +runAll = foldr ((>=>) . walkMWithLuaFilter) return + -- | Filter function stored in the registry newtype LuaFilterFunction = LuaFilterFunction Lua.Reference diff --git a/src/Text/Pandoc/Lua/Init.hs b/src/Text/Pandoc/Lua/Init.hs index e922e3d93..e69447ae5 100644 --- a/src/Text/Pandoc/Lua/Init.hs +++ b/src/Text/Pandoc/Lua/Init.hs @@ -30,7 +30,6 @@ module Text.Pandoc.Lua.Init ( LuaException (..) , LuaPackageParams (..) , runLua - , initLuaState , luaPackageParams ) where @@ -94,7 +93,7 @@ luaPackageParams = do , luaPkgMediaBag = mbRef } --- Initialize the lua state with all required values +-- | Initialize the lua state with all required values initLuaState :: LuaPackageParams -> Lua () initLuaState luaPkgParams = do Lua.openlibs -- cgit v1.2.3