From 3097ee100ed260a5c2cea7df5bf80c989687df44 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Fri, 15 Feb 2019 17:13:04 +0100 Subject: pandoc.mediabag module: add items function iterating over mediabag A new function `pandoc.mediabag.items` was added to Lua module pandoc.mediabag. This allows users to lazily iterate over all media bag items, loading items into Lua one-by-one. Example: for filename, mime_type, content in pandoc.mediabag.items() do -- use media bag item. end This is a convenient alternative to using `mediabag.list` in combination with `mediabag.lookup`. --- doc/lua-filters.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'doc') diff --git a/doc/lua-filters.md b/doc/lua-filters.md index 45abe8ac8..667a58bcc 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -2475,6 +2475,33 @@ Usage: local contents = "Hello, World!" pandoc.mediabag(fp, mt, contents) +### iter {#mediabag-iter} + +`items ()` + +Returns an iterator triple to be used with Lua's generic `for` +statement. The iterator returns the filepath, MIME type, and +content of a media bag item on each invocation. Items are +processed one-by-one to avoid excessive memory use. + +This function should be used only when full access to all items, +including their contents, is required. For all other cases, +[`list`](#mediabag-list) should be preferred. + +Returns: + + - The iterator function; must be called with the iterator state + and the current iterator value. + - Iterator state – an opaque value to be passed to the iterator + function. + - Initial iterator value. + +Usage: + + for fp, mt, contents in pandoc.mediabag.items() do + -- print(fp, mt, contents) + end + ### list {#mediabag-list} `list ()` -- cgit v1.2.3 From 0a6a11cfabead239eab3baec065d8d6e95bb6447 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Sat, 16 Feb 2019 13:20:33 +0100 Subject: pandoc.mediabag module: add function `empty` Function `pandoc.mediabag.empty` was added. It allows to clean-out the media bag, removing all entries. --- doc/lua-filters.md | 7 ++++++- src/Text/Pandoc/Lua/Module/MediaBag.hs | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/lua-filters.md b/doc/lua-filters.md index 667a58bcc..d60957a2e 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -2448,9 +2448,14 @@ The module is loaded as part of module `pandoc` and can either be accessed via the `pandoc.mediabag` field, or explicitly required, e.g.: - local mb = require 'pandoc.mediabag' +### empty {#mediabag-empty} + +`empty ()` + +Clear-out the media bag, deleting all items. + ### insert {#mediabag-insert} `insert (filepath, mime_type, contents)` diff --git a/src/Text/Pandoc/Lua/Module/MediaBag.hs b/src/Text/Pandoc/Lua/Module/MediaBag.hs index ce6303ec6..4678d46e8 100644 --- a/src/Text/Pandoc/Lua/Module/MediaBag.hs +++ b/src/Text/Pandoc/Lua/Module/MediaBag.hs @@ -34,6 +34,7 @@ import qualified Text.Pandoc.MediaBag as MB pushModule :: Lua NumResults pushModule = do Lua.newtable + addFunction "empty" empty addFunction "insert" insertMediaFn addFunction "items" items addFunction "lookup" lookupMediaFn @@ -60,6 +61,11 @@ setCommonState st = do modifyCommonState :: (CommonState -> CommonState) -> Lua () modifyCommonState f = getCommonState >>= setCommonState . f +-- | Delete all items from the media bag. +empty :: Lua NumResults +empty = 0 <$ modifyCommonState (\st -> st { stMediaBag = mempty }) + +-- | Insert a new item into the media bag. insertMediaFn :: FilePath -> Optional MimeType -> BL.ByteString -- cgit v1.2.3 From 5a82ecaaa19176afc24576fd80b91c9a529c2dcb Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Sat, 16 Feb 2019 13:35:16 +0100 Subject: pandoc.mediabag module: add function `delete` Function `pandoc.mediabag.delete` allows to remove a single item of the given name from the media bag. --- doc/lua-filters.md | 12 ++++++++++++ src/Text/Pandoc/Lua/Module/MediaBag.hs | 8 +++++++- src/Text/Pandoc/MediaBag.hs | 9 +++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/lua-filters.md b/doc/lua-filters.md index d60957a2e..2a9646dfd 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -2450,6 +2450,18 @@ e.g.: local mb = require 'pandoc.mediabag' +### delete {#mediabag-delete} + +`delete (filepath)` + +Removes a single entry from the media bag. + +Parameters: + +`filepath`: +: filename of the item to be deleted. The media bag will be + left unchanged if no entry with the given filename exists. + ### empty {#mediabag-empty} `empty ()` diff --git a/src/Text/Pandoc/Lua/Module/MediaBag.hs b/src/Text/Pandoc/Lua/Module/MediaBag.hs index 4678d46e8..261785665 100644 --- a/src/Text/Pandoc/Lua/Module/MediaBag.hs +++ b/src/Text/Pandoc/Lua/Module/MediaBag.hs @@ -34,6 +34,7 @@ import qualified Text.Pandoc.MediaBag as MB pushModule :: Lua NumResults pushModule = do Lua.newtable + addFunction "delete" delete addFunction "empty" empty addFunction "insert" insertMediaFn addFunction "items" items @@ -61,6 +62,11 @@ setCommonState st = do modifyCommonState :: (CommonState -> CommonState) -> Lua () modifyCommonState f = getCommonState >>= setCommonState . f +-- | Delete a single item from the media bag. +delete :: FilePath -> Lua NumResults +delete fp = 0 <$ modifyCommonState + (\st -> st { stMediaBag = MB.deleteMedia fp (stMediaBag st) }) + -- | Delete all items from the media bag. empty :: Lua NumResults empty = 0 <$ modifyCommonState (\st -> st { stMediaBag = mempty }) @@ -86,7 +92,7 @@ lookupMediaFn :: FilePath lookupMediaFn fp = do res <- MB.lookupMedia fp . stMediaBag <$> getCommonState case res of - Nothing -> Lua.pushnil *> return 1 + Nothing -> 1 <$ Lua.pushnil Just (mimeType, contents) -> do Lua.push mimeType Lua.push contents diff --git a/src/Text/Pandoc/MediaBag.hs b/src/Text/Pandoc/MediaBag.hs index 94512b71d..bb6fc88ac 100644 --- a/src/Text/Pandoc/MediaBag.hs +++ b/src/Text/Pandoc/MediaBag.hs @@ -16,6 +16,7 @@ interface for interacting with it. -} module Text.Pandoc.MediaBag ( MediaBag, + deleteMedia, lookupMedia, insertMedia, mediaDirectory, @@ -41,6 +42,14 @@ newtype MediaBag = MediaBag (M.Map [String] (MimeType, BL.ByteString)) instance Show MediaBag where show bag = "MediaBag " ++ show (mediaDirectory bag) +-- | Delete a media item from a 'MediaBag', or do nothing if no item corresponds +-- to the given path. +deleteMedia :: FilePath -- ^ relative path and canonical name of resource + -> MediaBag + -> MediaBag +deleteMedia fp (MediaBag mediamap) = + MediaBag $ M.delete (splitDirectories fp) mediamap + -- | Insert a media item into a 'MediaBag', replacing any existing -- value with the same name. insertMedia :: FilePath -- ^ relative path and canonical name of resource -- cgit v1.2.3