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.
This commit is contained in:
parent
beb78a552c
commit
4282abbd07
1 changed files with 13 additions and 8 deletions
|
@ -189,13 +189,18 @@ instance StackValue LuaFilter where
|
||||||
peek lua idx = fmap (LuaFilter lua) <$> Lua.peek lua idx
|
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
|
-- | 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
|
-- called with given element as argument and is expected to return an element.
|
||||||
-- return a single value.
|
-- 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 :: StackValue a => LuaState -> LuaFilterFunction -> a -> IO a
|
||||||
runFilterFunction lua lf x = do
|
runFilterFunction lua lf x = do
|
||||||
pushFilterFunction lua lf
|
pushFilterFunction lua lf
|
||||||
Lua.push lua x
|
Lua.push lua x
|
||||||
Lua.call lua 1 1
|
Lua.call lua 1 1
|
||||||
|
resType <- Lua.ltype lua (-1)
|
||||||
|
case resType of
|
||||||
|
Lua.TNIL -> Lua.pop lua 1 *> return x
|
||||||
|
_ -> do
|
||||||
mbres <- Lua.peek lua (-1)
|
mbres <- Lua.peek lua (-1)
|
||||||
case mbres of
|
case mbres of
|
||||||
Nothing -> throwIO $ LuaException
|
Nothing -> throwIO $ LuaException
|
||||||
|
|
Loading…
Add table
Reference in a new issue