git.iko.soy/typeable/pandoc
on master help sign in
pandoc.mediabag module: add function `delete`

Function `pandoc.mediabag.delete` allows to remove a single item of the given name from the media bag.

commit5a82ecaaa19176afc24576fd80b91c9a529c2dcb authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 16 Feb 2019, 13:35 (8 years ago) parent0a6a11c
3 files changed+28−1
doc/lua-filters.md+12 src/Text/Pandoc/Lua/Module/MediaBag.hs+7−1 src/Text/Pandoc/MediaBag.hs+9
doc/lua-filters.md+12
@@ -2450,6 +2450,18 @@ e.g.:
24502450
24512451 local mb = require 'pandoc.mediabag'
24522452
2453+### delete {#mediabag-delete}
2454+
2455+`delete (filepath)`
2456+
2457+Removes a single entry from the media bag.
2458+
2459+Parameters:
2460+
2461+`filepath`:
2462+: filename of the item to be deleted. The media bag will be
2463+ left unchanged if no entry with the given filename exists.
2464+
24532465### empty {#mediabag-empty}
24542466
24552467`empty ()`
src/Text/Pandoc/Lua/Module/MediaBag.hs+7−1
@@ -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
src/Text/Pandoc/MediaBag.hs+9
@@ -16,6 +16,7 @@ interface for interacting with it.
1616-}
1717module Text.Pandoc.MediaBag (
1818 MediaBag,
19+ deleteMedia,
1920 lookupMedia,
2021 insertMedia,
2122 mediaDirectory,
@@ -41,6 +42,14 @@ newtype MediaBag = MediaBag (M.Map [String] (MimeType, BL.ByteString))
4142instance Show MediaBag where
4243 show bag = "MediaBag " ++ show (mediaDirectory bag)
4344
45+-- | Delete a media item from a 'MediaBag', or do nothing if no item corresponds
46+-- to the given path.
47+deleteMedia :: FilePath -- ^ relative path and canonical name of resource
48+ -> MediaBag
49+ -> MediaBag
50+deleteMedia fp (MediaBag mediamap) =
51+ MediaBag $ M.delete (splitDirectories fp) mediamap
52+
4453-- | Insert a media item into a 'MediaBag', replacing any existing
4554-- value with the same name.
4655insertMedia :: FilePath -- ^ relative path and canonical name of resource