diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2017-04-30 16:14:33 +0200 |
---|---|---|
committer | Albert Krewinkel <albert@zeitkraut.de> | 2017-04-30 17:06:54 +0200 |
commit | ae21a8bb2a9d892491424f257ed0146c1b2affa2 (patch) | |
tree | 8bb96863bb27bedf1e4891a232af2ee46b561f3a /src/Text/Pandoc/Lua.hs | |
parent | 1afdccfa8c8a228187441e83bcc8e5214be3a8c8 (diff) |
Lua filter: fall-back to global filters when none is returned
The implicitly defined global filter (i.e. all element filtering
functions defined in the global lua environment) is used if no filter is
returned from a lua script. This allows to just write top-level
functions in order to define a lua filter. E.g
function Emph(elem) return pandoc.Strong(elem.content) end
Diffstat (limited to 'src/Text/Pandoc/Lua.hs')
-rw-r--r-- | src/Text/Pandoc/Lua.hs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs index ffc57c9c2..f4a22b92a 100644 --- a/src/Text/Pandoc/Lua.hs +++ b/src/Text/Pandoc/Lua.hs @@ -54,13 +54,17 @@ runLuaFilter filterPath args pd = liftIO $ do -- store module in global "pandoc" pushPandocModule lua Lua.setglobal lua "pandoc" + top <- Lua.gettop lua status <- Lua.loadfile lua filterPath if (status /= 0) then do Just luaErrMsg <- Lua.peek lua 1 error luaErrMsg else do - Lua.call lua 0 1 + Lua.call lua 0 Lua.multret + newtop <- Lua.gettop lua + -- Use the implicitly defined global filter if nothing was returned + when (newtop - top < 1) $ pushGlobalFilter lua Just luaFilters <- Lua.peek lua (-1) Lua.push lua args Lua.setglobal lua "PandocParameters" @@ -68,6 +72,13 @@ runLuaFilter filterPath args pd = liftIO $ do Lua.close lua return doc +pushGlobalFilter :: LuaState -> IO () +pushGlobalFilter lua = + Lua.newtable lua + *> Lua.getglobal2 lua "pandoc.global_filter" + *> Lua.call lua 0 1 + *> Lua.rawseti lua (-2) 1 + runAll :: [LuaFilter] -> Pandoc -> IO Pandoc runAll [] = return runAll (x:xs) = walkMWithLuaFilter x >=> runAll xs |