aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2024-10-01 12:08:00 +0200
committerAlbert Krewinkel <[email protected]>2024-10-01 12:23:36 +0200
commit4cf6ddb027cdc262e34dea50ec77f3fcdbc517d2 (patch)
treec253d35d84b05c4eacffd4171be211e726602d1b /pandoc-lua-engine
parent752d57dac18b2381fb05972cccdc21ea16d242cd (diff)
Lua: allow returning a single filter from filter files
It is now possible to return a single filter from a filter file, e.g. ``` lua -- Switch single- and double quotes return { Quoted = function (q) elem.quotetype = elem.quotetype == 'SingleQuote' and 'DoubleQuote' or 'SingleQuote' return elem end } The filter must not contain numerical indexes, or it might be treated as a list of filters.
Diffstat (limited to 'pandoc-lua-engine')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Filter.hs5
-rw-r--r--pandoc-lua-engine/test/lua/single-to-double-quoted.lua2
2 files changed, 4 insertions, 3 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Filter.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Filter.hs
index 4728f961e..40ccb2f8f 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Filter.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Filter.hs
@@ -43,7 +43,10 @@ runFilterFile' envIdx filterPath doc = do
-- filter if nothing was returned.
luaFilters <- forcePeek $
if newtop - oldtop >= 1
- then peekList peekFilter top -- get from explicit filter table
+ then liftLua (rawlen top) >>= \case
+ -- explicitly returned filter, either a single one or a list
+ 0 -> (:[]) <$!> peekFilter top -- single filter
+ _ -> peekList peekFilter top -- list of explicit filters
else (:[]) <$!> peekFilter envIdx -- get the implicit filter in _ENV
settop oldtop
runAll luaFilters doc
diff --git a/pandoc-lua-engine/test/lua/single-to-double-quoted.lua b/pandoc-lua-engine/test/lua/single-to-double-quoted.lua
index b985b215c..29843df06 100644
--- a/pandoc-lua-engine/test/lua/single-to-double-quoted.lua
+++ b/pandoc-lua-engine/test/lua/single-to-double-quoted.lua
@@ -1,10 +1,8 @@
return {
- {
Quoted = function (elem)
if elem.quotetype == "SingleQuote" then
elem.quotetype = "DoubleQuote"
end
return elem
end,
- }
}