aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Lua/Module/MediaBag.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-05-30 10:45:06 -0400
committerGitHub <noreply@github.com>2019-05-30 10:45:06 -0400
commitd68799570859e64d2fdefb6dde15abfb6553b1a5 (patch)
tree2f52001b5775326e0a39df368c475569af85b354 /src/Text/Pandoc/Lua/Module/MediaBag.hs
parent8507d98a1534ba4049e4c1ca3671ee41a1f56c70 (diff)
parentf7222370afd50f6ea65f199fd8e4f03101ec8ff4 (diff)
downloadpandoc-d68799570859e64d2fdefb6dde15abfb6553b1a5.tar.gz
Merge pull request #5312 from tarleb/pandoc-mediabag-extension
Pandoc mediabag extension
Diffstat (limited to 'src/Text/Pandoc/Lua/Module/MediaBag.hs')
-rw-r--r--src/Text/Pandoc/Lua/Module/MediaBag.hs22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Lua/Module/MediaBag.hs b/src/Text/Pandoc/Lua/Module/MediaBag.hs
index 52aeaa1af..261785665 100644
--- a/src/Text/Pandoc/Lua/Module/MediaBag.hs
+++ b/src/Text/Pandoc/Lua/Module/MediaBag.hs
@@ -20,6 +20,7 @@ import Foreign.Lua (Lua, NumResults, Optional, liftIO)
import Text.Pandoc.Class (CommonState (..), fetchItem, putCommonState,
runIOorExplode, setMediaBag)
import Text.Pandoc.Lua.Marshaling ()
+import Text.Pandoc.Lua.Marshaling.MediaBag (pushIterator)
import Text.Pandoc.Lua.Util (addFunction)
import Text.Pandoc.MIME (MimeType)
@@ -33,7 +34,10 @@ 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
addFunction "lookup" lookupMediaFn
addFunction "list" mediaDirectoryFn
addFunction "fetch" fetch
@@ -58,6 +62,16 @@ 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 })
+
+-- | Insert a new item into the media bag.
insertMediaFn :: FilePath
-> Optional MimeType
-> BL.ByteString
@@ -66,15 +80,19 @@ insertMediaFn fp optionalMime contents = do
modifyCommonState $ \st ->
let mb = MB.insertMedia fp (Lua.fromOptional optionalMime) contents
(stMediaBag st)
- in st { stMediaBag = mb}
+ in st { stMediaBag = mb }
return 0
+-- | Returns iterator values to be used with a Lua @for@ loop.
+items :: Lua NumResults
+items = stMediaBag <$> getCommonState >>= pushIterator
+
lookupMediaFn :: FilePath
-> Lua NumResults
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