@@ -34,6 +34,7 @@ import qualified Text.Pandoc.MediaBag as MB
3434pushModule :: Lua NumResults
3535pushModule = do
3636 Lua.newtable
37+ addFunction "delete" delete
3738 addFunction "empty" empty
3839 addFunction "insert" insertMediaFn
3940 addFunction "items" items
@@ -61,6 +62,11 @@ setCommonState st = do
6162modifyCommonState :: (CommonState -> CommonState) -> Lua ()
6263modifyCommonState f = getCommonState >>= setCommonState . f
6364
65+-- | Delete a single item from the media bag.
66+delete :: FilePath -> Lua NumResults
67+delete fp = 0 <$ modifyCommonState
68+ (\st -> st { stMediaBag = MB.deleteMedia fp (stMediaBag st) })
69+
6470-- | Delete all items from the media bag.
6571empty :: Lua NumResults
6672empty = 0 <$ modifyCommonState (\st -> st { stMediaBag = mempty })
@@ -86,7 +92,7 @@ lookupMediaFn :: FilePath
8692lookupMediaFn fp = do
8793 res <- MB.lookupMedia fp . stMediaBag <$> getCommonState
8894 case res of
89− Nothing -> Lua.pushnil *> return 1
95+ Nothing -> 1 <$ Lua.pushnil
9096 Just (mimeType, contents) -> do
9197 Lua.push mimeType
9298 Lua.push contents