@@ -56,13 +56,14 @@ import qualified Text.Pandoc.Shared as IO ( fetchItem
5656 , getDefaultReferenceODT
5757 , warn
5858 , readDataFile)
59−import Text.Pandoc.MediaBag (MediaBag, lookupMedia)
6059import Text.Pandoc.Compat.Time (UTCTime)
6160import qualified Text.Pandoc.Compat.Time as IO (getCurrentTime)
6261import Data.Time.Clock.POSIX ( utcTimeToPOSIXSeconds
6362 , posixSecondsToUTCTime
6463 , POSIXTime )
6564import Text.Pandoc.MIME (MimeType, getMimeType)
65+import Text.Pandoc.MediaBag (MediaBag)
66+import qualified Text.Pandoc.MediaBag as MB
6667import qualified Data.ByteString as B
6768import qualified Data.ByteString.Lazy as BL
6869import qualified Control.Exception as E
@@ -100,6 +101,7 @@ class (Functor m, Applicative m, Monad m, MonadError PandocExecutionError m) =>
100101 getWarnings :: m [String]
101102 fail :: String -> m b
102103 glob :: String -> m [FilePath]
104+ insertMedia :: FilePath -> Maybe MimeType -> BL.ByteString -> m ()
103105
104106--Some functions derived from Primitives:
105107
@@ -114,11 +116,14 @@ data PandocExecutionError = PandocFileReadError FilePath
114116 deriving (Show, Typeable)
115117
116118-- Nothing in this for now, but let's put it there anyway.
117−data PandocStateIO = PandocStateIO { ioStWarnings :: [String] }
118− deriving Show
119+data PandocStateIO = PandocStateIO { ioStWarnings :: [String]
120+ , ioStMediaBag :: MediaBag
121+ } deriving Show
119122
120123instance Default PandocStateIO where
121− def = PandocStateIO { ioStWarnings = [] }
124+ def = PandocStateIO { ioStWarnings = []
125+ , ioStMediaBag = mempty
126+ }
122127
123128runIO :: PandocIO a -> IO (Either PandocExecutionError a)
124129runIO ma = flip evalStateT def $ runExceptT $ unPandocIO ma
@@ -134,7 +139,7 @@ runIOorExplode ma = do
134139
135140newtype PandocIO a = PandocIO {
136141 unPandocIO :: ExceptT PandocExecutionError (StateT PandocStateIO IO) a
137− } deriving (MonadIO, Functor, Applicative, Monad, MonadError PandocExecutionError)
142+ } deriving (MonadIO, Functor, Applicative, Monad, MonadState PandocStateIO, MonadError PandocExecutionError)
138143
139144instance PandocMonad PandocIO where
140145 lookupEnv = liftIO . IO.lookupEnv
@@ -162,6 +167,8 @@ instance PandocMonad PandocIO where
162167 liftIO $ IO.warn msg
163168 getWarnings = gets ioStWarnings
164169 glob = liftIO . IO.glob
170+ insertMedia fp mime bs =
171+ modify $ \st -> st{ioStMediaBag = MB.insertMedia fp mime bs (ioStMediaBag st) }
165172
166173data TestState = TestState { stStdGen :: StdGen
167174 , stWord8Store :: [Word8] -- should be
@@ -173,6 +180,7 @@ data TestState = TestState { stStdGen :: StdGen
173180 -- contain every
174181 -- element at most
175182 -- once, e.g. [1..]
183+ , stMediaBag :: MediaBag
176184 }
177185
178186instance Default TestState where
@@ -180,6 +188,7 @@ instance Default TestState where
180188 , stWord8Store = [1..]
181189 , stWarnings = []
182190 , stUniqStore = [1..]
191+ , stMediaBag = mempty
183192 }
184193
185194data TestEnv = TestEnv { envEnv :: [(String, String)]
@@ -264,7 +273,7 @@ instance PandocMonad PandocPure where
264273 Nothing -> return (Left $ E.toException $ PandocFileReadError fp)
265274
266275 fetchItem' media sourceUrl nm = do
267− case lookupMedia nm media of
276+ case MB.lookupMedia nm media of
268277 Nothing -> fetchItem sourceUrl nm
269278 Just (mime, bs) -> return (Right (B.concat $ BL.toChunks bs, Just mime))
270279
@@ -275,3 +284,6 @@ instance PandocMonad PandocPure where
275284 glob s = do
276285 fontFiles <- asks envFontFiles
277286 return (filter (match (compile s)) fontFiles)
287+
288+ insertMedia fp mime bs =
289+ modify $ \st -> st{stMediaBag = MB.insertMedia fp mime bs (stMediaBag st) }