diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2018-10-26 23:21:54 +0200 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2018-10-28 12:08:52 -0700 |
commit | 7f54f76e8b5a7b45cd61a354980ef77f65baba20 (patch) | |
tree | 6c3ba071736733f2ff9628043bc04991ad82f09f /src/Text/Pandoc/Filter | |
parent | 0531a4653a79b0368d6e87d7579fe27ccf6d9623 (diff) | |
download | pandoc-7f54f76e8b5a7b45cd61a354980ef77f65baba20.tar.gz |
T.P.Lua: merge runLuaFilter into T.P.Filter.Lua (API change)
The function `runLuaFilter` was only used in Text.Pandoc.Filter.Lua, use
apply from the that module instead.
Diffstat (limited to 'src/Text/Pandoc/Filter')
-rw-r--r-- | src/Text/Pandoc/Filter/Lua.hs | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/src/Text/Pandoc/Filter/Lua.hs b/src/Text/Pandoc/Filter/Lua.hs index d559fb912..6c78bef06 100644 --- a/src/Text/Pandoc/Filter/Lua.hs +++ b/src/Text/Pandoc/Filter/Lua.hs @@ -32,24 +32,55 @@ module Text.Pandoc.Filter.Lua (apply) where import Prelude import Control.Exception (throw) +import Control.Monad ((>=>)) +import Foreign.Lua (Lua) import Text.Pandoc.Class (PandocIO) import Text.Pandoc.Definition (Pandoc) import Text.Pandoc.Error (PandocError (PandocFilterError)) import Text.Pandoc.Filter.Path (expandFilterPath) -import Text.Pandoc.Lua (LuaException (..), runLuaFilter) +import Text.Pandoc.Lua (LuaException (..), runPandocLua) +import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter) +import Text.Pandoc.Lua.Global (Global (..), setGlobals) +import Text.Pandoc.Lua.Util (dofileWithTraceback) import Text.Pandoc.Options (ReaderOptions) +import qualified Foreign.Lua as Lua + +-- | 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. apply :: ReaderOptions -> [String] -> FilePath -> Pandoc -> PandocIO Pandoc -apply ropts args f d = do - f' <- expandFilterPath f +apply ropts args f doc = do + filterPath <- expandFilterPath f let format = case args of (x:_) -> x - _ -> error "Format not supplied for lua filter" - res <- runLuaFilter ropts f' format d - case res of - Right x -> return x - Left (LuaException s) -> throw (PandocFilterError f s) + _ -> error "Format not supplied for Lua filter" + runPandocLua >=> forceResult filterPath $ do + setGlobals [ FORMAT format + , PANDOC_READER_OPTIONS ropts + , PANDOC_SCRIPT_FILE filterPath + ] + top <- Lua.gettop + stat <- 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 + +forceResult :: FilePath -> Either LuaException Pandoc -> PandocIO Pandoc +forceResult fp eitherResult = case eitherResult of + Right x -> return x + Left (LuaException s) -> throw (PandocFilterError fp s) + +runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc +runAll = foldr ((>=>) . walkMWithLuaFilter) return |