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
This commit is contained in:
parent
1afdccfa8c
commit
ae21a8bb2a
4 changed files with 25 additions and 2 deletions
|
@ -803,7 +803,7 @@ end
|
||||||
function M.global_filter()
|
function M.global_filter()
|
||||||
local res = {}
|
local res = {}
|
||||||
for k, v in pairs(_G) do
|
for k, v in pairs(_G) do
|
||||||
if M.Inline.constructor[k] or M.Block.constructor[k] or M.Block.constructors[k] or k == "Doc" then
|
if M.Inline.constructor[k] or M.Block.constructor[k] or k == "Doc" then
|
||||||
res[k] = v
|
res[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,13 +54,17 @@ runLuaFilter filterPath args pd = liftIO $ do
|
||||||
-- store module in global "pandoc"
|
-- store module in global "pandoc"
|
||||||
pushPandocModule lua
|
pushPandocModule lua
|
||||||
Lua.setglobal lua "pandoc"
|
Lua.setglobal lua "pandoc"
|
||||||
|
top <- Lua.gettop lua
|
||||||
status <- Lua.loadfile lua filterPath
|
status <- Lua.loadfile lua filterPath
|
||||||
if (status /= 0)
|
if (status /= 0)
|
||||||
then do
|
then do
|
||||||
Just luaErrMsg <- Lua.peek lua 1
|
Just luaErrMsg <- Lua.peek lua 1
|
||||||
error luaErrMsg
|
error luaErrMsg
|
||||||
else do
|
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)
|
Just luaFilters <- Lua.peek lua (-1)
|
||||||
Lua.push lua args
|
Lua.push lua args
|
||||||
Lua.setglobal lua "PandocParameters"
|
Lua.setglobal lua "PandocParameters"
|
||||||
|
@ -68,6 +72,13 @@ runLuaFilter filterPath args pd = liftIO $ do
|
||||||
Lua.close lua
|
Lua.close lua
|
||||||
return doc
|
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 :: [LuaFilter] -> Pandoc -> IO Pandoc
|
||||||
runAll [] = return
|
runAll [] = return
|
||||||
runAll (x:xs) = walkMWithLuaFilter x >=> runAll xs
|
runAll (x:xs) = walkMWithLuaFilter x >=> runAll xs
|
||||||
|
|
|
@ -47,6 +47,12 @@ tests =
|
||||||
(doc . para $ str "Hey!" <> linebreak <> str "What's up?")
|
(doc . para $ str "Hey!" <> linebreak <> str "What's up?")
|
||||||
(doc . para $ str "Hello," <> space <> str "World!")
|
(doc . para $ str "Hello," <> space <> str "World!")
|
||||||
|
|
||||||
|
, testCase "implicit doc filter" $
|
||||||
|
assertFilterConversion "Document contains 'Hello, World!'"
|
||||||
|
"implicit-doc-filter.lua"
|
||||||
|
(doc . plain $ linebreak)
|
||||||
|
(doc . para $ str "Hello," <> space <> str "World!")
|
||||||
|
|
||||||
, testCase "parse raw markdown blocks" $
|
, testCase "parse raw markdown blocks" $
|
||||||
assertFilterConversion "raw markdown block is converted"
|
assertFilterConversion "raw markdown block is converted"
|
||||||
"markdown-reader.lua"
|
"markdown-reader.lua"
|
||||||
|
|
6
test/lua/implicit-doc-filter.lua
Normal file
6
test/lua/implicit-doc-filter.lua
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
function Doc (doc)
|
||||||
|
local meta = {}
|
||||||
|
local hello = { pandoc.Str "Hello,", pandoc.Space(), pandoc.Str "World!" }
|
||||||
|
local blocks = { pandoc.Para(hello) }
|
||||||
|
return pandoc.Doc(blocks, meta)
|
||||||
|
end
|
Loading…
Reference in a new issue