@@ -14,13 +14,13 @@ module Text.Pandoc.Lua.Module.MediaBag
1414 ) where
1515
1616import Control.Monad (zipWithM_)
17−import Foreign.Lua (Lua, NumResults, Optional, liftIO)
17+import Foreign.Lua (Lua, NumResults, Optional)
1818import Text.Pandoc.Class.CommonState (CommonState (..))
19−import Text.Pandoc.Class.PandocIO (runIOorExplode)
20−import Text.Pandoc.Class.PandocMonad (fetchItem, putCommonState, setMediaBag)
19+import Text.Pandoc.Class.PandocMonad (fetchItem, getMediaBag, modifyCommonState,
20+ setMediaBag)
2121import Text.Pandoc.Lua.Marshaling ()
2222import Text.Pandoc.Lua.Marshaling.MediaBag (pushIterator)
23−import Text.Pandoc.Lua.Util (addFunction)
23+import Text.Pandoc.Lua.PandocLua (PandocLua (..), liftPandocLua, addFunction)
2424import Text.Pandoc.MIME (MimeType)
2525
2626import qualified Data.ByteString.Lazy as BL
@@ -31,9 +31,9 @@ import qualified Text.Pandoc.MediaBag as MB
3131--
3232-- MediaBag submodule
3333--
34−pushModule :: Lua NumResults
34+pushModule :: PandocLua NumResults
3535pushModule = do
36− Lua.newtable
36+ liftPandocLua Lua.newtable
3737 addFunction "delete" delete
3838 addFunction "empty" empty
3939 addFunction "insert" insertMediaFn
@@ -43,66 +43,46 @@ pushModule = do
4343 addFunction "fetch" fetch
4444 return 1
4545
46−--
47−-- Port functions from Text.Pandoc.Class to the Lua monad.
48−-- TODO: reuse existing functions.
49−
50−-- Get the current CommonState.
51−getCommonState :: Lua CommonState
52−getCommonState = do
53− Lua.getglobal "PANDOC_STATE"
54− Lua.peek Lua.stackTop
55−
56−-- Replace MediaBag in CommonState.
57−setCommonState :: CommonState -> Lua ()
58−setCommonState st = do
59− Lua.push st
60− Lua.setglobal "PANDOC_STATE"
61−
62−modifyCommonState :: (CommonState -> CommonState) -> Lua ()
63−modifyCommonState f = getCommonState >>= setCommonState . f
64−
6546-- | Delete a single item from the media bag.
66−delete :: FilePath -> Lua NumResults
47+delete :: FilePath -> PandocLua NumResults
6748delete fp = 0 <$ modifyCommonState
6849 (\st -> st { stMediaBag = MB.deleteMedia fp (stMediaBag st) })
6950
7051-- | Delete all items from the media bag.
71−empty :: Lua NumResults
52+empty :: PandocLua NumResults
7253empty = 0 <$ modifyCommonState (\st -> st { stMediaBag = mempty })
7354
7455-- | Insert a new item into the media bag.
7556insertMediaFn :: FilePath
7657 -> Optional MimeType
7758 -> BL.ByteString
78− -> Lua NumResults
59+ -> PandocLua NumResults
7960insertMediaFn fp optionalMime contents = do
80− modifyCommonState $ \st ->
81− let mb = MB.insertMedia fp (Lua.fromOptional optionalMime) contents
82− (stMediaBag st)
83− in st { stMediaBag = mb }
84− return 0
61+ mb <- getMediaBag
62+ setMediaBag $ MB.insertMedia fp (Lua.fromOptional optionalMime) contents mb
63+ return (Lua.NumResults 0)
8564
8665-- | Returns iterator values to be used with a Lua @for@ loop.
87−items :: Lua NumResults
88−items = getCommonState >>= pushIterator . stMediaBag
66+items :: PandocLua NumResults
67+items = getMediaBag >>= liftPandocLua . pushIterator
8968
9069lookupMediaFn :: FilePath
91− -> Lua NumResults
70+ -> PandocLua NumResults
9271lookupMediaFn fp = do
93− res <- MB.lookupMedia fp . stMediaBag <$> getCommonState
94− case res of
72+ res <- MB.lookupMedia fp <$> getMediaBag
73+ liftPandocLua $ case res of
9574 Nothing -> 1 <$ Lua.pushnil
9675 Just (mimeType, contents) -> do
9776 Lua.push mimeType
9877 Lua.push contents
9978 return 2
10079
101−mediaDirectoryFn :: Lua NumResults
80+mediaDirectoryFn :: PandocLua NumResults
10281mediaDirectoryFn = do
103− dirContents <- MB.mediaDirectory . stMediaBag <$> getCommonState
104− Lua.newtable
105− zipWithM_ addEntry [1..] dirContents
82+ dirContents <- MB.mediaDirectory <$> getMediaBag
83+ liftPandocLua $ do
84+ Lua.newtable
85+ zipWithM_ addEntry [1..] dirContents
10686 return 1
10787 where
10888 addEntry :: Lua.Integer -> (FilePath, MimeType, Int) -> Lua ()
@@ -114,14 +94,9 @@ mediaDirectoryFn = do
11494 Lua.rawseti (-2) idx
11595
11696fetch :: T.Text
117− -> Lua NumResults
97+ -> PandocLua NumResults
11898fetch src = do
119− commonState <- getCommonState
120− let mediaBag = stMediaBag commonState
121− (bs, mimeType) <- liftIO . runIOorExplode $ do
122− putCommonState commonState
123− setMediaBag mediaBag
124− fetchItem src
125− Lua.push $ maybe "" T.unpack mimeType
126− Lua.push bs
99+ (bs, mimeType) <- fetchItem src
100+ liftPandocLua . Lua.push $ maybe "" T.unpack mimeType
101+ liftPandocLua $ Lua.push bs
127102 return 2 -- returns 2 values: contents, mimetype