aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Text/Pandoc/Lua.hs13
-rw-r--r--src/Text/Pandoc/Lua/PandocModule.hs23
2 files changed, 22 insertions, 14 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index f7e74d0a8..2e4204898 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -44,7 +44,8 @@ import Data.Map (Map)
import Data.Maybe (isJust)
import Foreign.Lua (Lua, FromLuaStack (peek), LuaException (..), StackIndex,
Status (OK), ToLuaStack (push))
-import Text.Pandoc.Class (PandocIO, getMediaBag, setMediaBag)
+import Text.Pandoc.Class (PandocIO, getMediaBag, setMediaBag,
+ getCommonState, CommonState)
import Text.Pandoc.MediaBag (MediaBag)
import Text.Pandoc.Definition
import Text.Pandoc.Lua.PandocModule (pushPandocModule, pushMediaBagModule)
@@ -56,17 +57,19 @@ import qualified Foreign.Lua as Lua
runLuaFilter :: Maybe FilePath -> FilePath -> String
-> Pandoc -> PandocIO (Either LuaException Pandoc)
runLuaFilter datadir filterPath format pd = do
+ commonState <- getCommonState
mediaBag <- getMediaBag
mediaBagRef <- liftIO (newIORef mediaBag)
res <- liftIO . Lua.runLuaEither $
- runLuaFilter' datadir filterPath format mediaBagRef pd
+ runLuaFilter' commonState datadir filterPath format mediaBagRef pd
newMediaBag <- liftIO (readIORef mediaBagRef)
setMediaBag newMediaBag
return res
-runLuaFilter' :: Maybe FilePath -> FilePath -> String -> IORef MediaBag
+runLuaFilter' :: CommonState
+ -> Maybe FilePath -> FilePath -> String -> IORef MediaBag
-> Pandoc -> Lua Pandoc
-runLuaFilter' datadir filterPath format mbRef pd = do
+runLuaFilter' commonState datadir filterPath format mbRef pd = do
Lua.openlibs
-- store module in global "pandoc"
pushPandocModule datadir
@@ -89,7 +92,7 @@ runLuaFilter' datadir filterPath format mbRef pd = do
addMediaBagModule = do
Lua.getglobal "pandoc"
push "mediabag"
- pushMediaBagModule mbRef
+ pushMediaBagModule commonState mbRef
Lua.rawset (-3)
registerFormat = do
push format
diff --git a/src/Text/Pandoc/Lua/PandocModule.hs b/src/Text/Pandoc/Lua/PandocModule.hs
index 326de1886..bf45cab17 100644
--- a/src/Text/Pandoc/Lua/PandocModule.hs
+++ b/src/Text/Pandoc/Lua/PandocModule.hs
@@ -41,7 +41,8 @@ import Data.IORef
import Data.Text (pack)
import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
import Text.Pandoc.Class (fetchMediaResource, readDataFile, runIO,
- runIOorExplode, setUserDataDir)
+ runIOorExplode, setUserDataDir, CommonState(..),
+ putCommonState)
import Text.Pandoc.Options (ReaderOptions(readerExtensions))
import Text.Pandoc.Lua.StackInstances ()
import Text.Pandoc.Readers (Reader (..), getReader)
@@ -83,13 +84,13 @@ readDoc formatSpec content = do
--
-- MediaBag submodule
--
-pushMediaBagModule :: IORef MB.MediaBag -> Lua ()
-pushMediaBagModule mediaBagRef = do
+pushMediaBagModule :: CommonState -> IORef MB.MediaBag -> Lua ()
+pushMediaBagModule commonState mediaBagRef = do
Lua.newtable
addFunction "insert" (insertMediaFn mediaBagRef)
addFunction "lookup" (lookupMediaFn mediaBagRef)
addFunction "list" (mediaDirectoryFn mediaBagRef)
- addFunction "fetch" (insertResource mediaBagRef)
+ addFunction "fetch" (insertResource commonState mediaBagRef)
return ()
where
addFunction name fn = do
@@ -103,7 +104,8 @@ insertMediaFn :: IORef MB.MediaBag
-> BL.ByteString
-> Lua NumResults
insertMediaFn mbRef fp nilOrMime contents = do
- liftIO . modifyIORef' mbRef $ MB.insertMedia fp (toMaybe nilOrMime) contents
+ liftIO . modifyIORef' mbRef $
+ MB.insertMedia fp (toMaybe nilOrMime) contents
return 0
lookupMediaFn :: IORef MB.MediaBag
@@ -134,12 +136,15 @@ mediaDirectoryFn mbRef = do
Lua.push "length" *> Lua.push contentLength *> Lua.rawset (-3)
Lua.rawseti (-2) idx
-insertResource :: IORef MB.MediaBag
+insertResource :: CommonState
+ -> IORef MB.MediaBag
-> String
-> Lua NumResults
-insertResource mbRef src = do
- (fp, mimeType, bs) <- liftIO . runIOorExplode $ fetchMediaResource src
- liftIO $ print (fp, mimeType)
+insertResource commonState mbRef src = do
+ (fp, mimeType, bs) <- liftIO . runIOorExplode $ do
+ putCommonState commonState
+ fetchMediaResource src
+ liftIO $ print (fp, mimeType) -- TODO DEBUG
insertMediaFn mbRef fp (OrNil mimeType) bs
--