aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert.krewinkel@tourstream.eu>2017-06-27 17:11:42 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2017-06-27 17:11:42 +0200
commit4282abbd0781cf5e6731a9b43dc8cfeb1dca58fa (patch)
tree14d6b07b861ab9610b9e0eb5114bf00f343fe624 /src/Text
parentbeb78a552cb3480d55b8eca8c0c77bccd5804506 (diff)
downloadpandoc-4282abbd0781cf5e6731a9b43dc8cfeb1dca58fa.tar.gz
Text.Pandoc.Lua: keep element unchanged if filter returns nil
This was suggested by jgm and is consistent with the behavior of other filtering libraries.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Lua.hs21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index 858212df1..3770880f3 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -189,19 +189,24 @@ instance StackValue LuaFilter where
peek lua idx = fmap (LuaFilter lua) <$> Lua.peek lua idx
-- | Push a value to the stack via a lua filter function. The filter function is
--- called with all arguments that are passed to this function and is expected to
--- return a single value.
+-- called with given element as argument and is expected to return an element.
+-- Alternatively, the function can return nothing or nil, in which case the
+-- element is left unchanged.
runFilterFunction :: StackValue a => LuaState -> LuaFilterFunction -> a -> IO a
runFilterFunction lua lf x = do
pushFilterFunction lua lf
Lua.push lua x
Lua.call lua 1 1
- mbres <- Lua.peek lua (-1)
- case mbres of
- Nothing -> throwIO $ LuaException
- ("Error while trying to get a filter's return "
- ++ "value from lua stack.")
- Just res -> res <$ Lua.pop lua 1
+ resType <- Lua.ltype lua (-1)
+ case resType of
+ Lua.TNIL -> Lua.pop lua 1 *> return x
+ _ -> do
+ mbres <- Lua.peek lua (-1)
+ case mbres of
+ Nothing -> throwIO $ LuaException
+ ("Error while trying to get a filter's return "
+ ++ "value from lua stack.")
+ Just res -> res <$ Lua.pop lua 1
-- | Push the filter function to the top of the stack.
pushFilterFunction :: Lua.LuaState -> LuaFilterFunction -> IO ()