aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJesse Rosenthal <jrosenthal@jhu.edu>2016-12-13 20:22:50 -0500
committerJohn MacFarlane <jgm@berkeley.edu>2017-01-25 17:07:42 +0100
commit55dbc00d55a4136271cd4c4d7ff4ab73d186f4b6 (patch)
treedc3db73154ac9431c4407d20e254972731875b2a /src/Text/Pandoc
parent5814096d79770edabc2822ff66747e3559c61e76 (diff)
downloadpandoc-55dbc00d55a4136271cd4c4d7ff4ab73d186f4b6.tar.gz
Integrate DeferredMediaBag into CommonState
The DeferredMediaBag is now the object that is held in state. It should not be visible to users, who will still deal with MediaBag through exported getters and setters. We now have a function `fetchDeferredMedia` which returns () but downloads/reads in all of the deferred media. Note that getMediaBag first fetches all deferred media.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Class.hs27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs
index 4412e8d76..836c57b2e 100644
--- a/src/Text/Pandoc/Class.hs
+++ b/src/Text/Pandoc/Class.hs
@@ -135,15 +135,17 @@ getWarnings :: PandocMonad m => m [String]
getWarnings = getsCommonState stWarnings
setMediaBag :: PandocMonad m => MediaBag -> m ()
-setMediaBag mb = modifyCommonState $ \st -> st{stMediaBag = mb}
+setMediaBag mb = modifyCommonState $
+ \st -> st{stDeferredMediaBag = DeferredMediaBag mb mempty}
getMediaBag :: PandocMonad m => m MediaBag
-getMediaBag = getsCommonState stMediaBag
+getMediaBag = fetchDeferredMedia >> (dropDeferredMedia <$> getsCommonState stDeferredMediaBag)
insertMedia :: PandocMonad m => FilePath -> Maybe MimeType -> BL.ByteString -> m ()
-insertMedia fp mime bs =
- modifyCommonState $ \st ->
- st{stMediaBag = MB.insertMedia fp mime bs (stMediaBag st) }
+insertMedia fp mime bs = do
+ (DeferredMediaBag mb dm) <- getsCommonState stDeferredMediaBag
+ let mb' = MB.insertMedia fp mime bs mb
+ modifyCommonState $ \st -> st{stDeferredMediaBag =DeferredMediaBag mb' dm }
getInputFiles :: PandocMonad m => m (Maybe [FilePath])
getInputFiles = getsCommonState stInputFiles
@@ -179,8 +181,9 @@ instance Monoid DeferredMediaBag where
mappend (DeferredMediaBag mb lst) (DeferredMediaBag mb' lst') =
DeferredMediaBag (mb <> mb') (lst <> lst')
-getDeferredMedia :: PandocMonad m => DeferredMediaBag -> m MediaBag
-getDeferredMedia (DeferredMediaBag mb defMedia) = do
+fetchDeferredMedia' :: PandocMonad m => m MediaBag
+fetchDeferredMedia' = do
+ (DeferredMediaBag mb defMedia) <- getsCommonState stDeferredMediaBag
fetchedMedia <- mapM (\dfp -> fetchItem Nothing (unDefer dfp)) defMedia
return $ foldr
(\(dfp, (bs, mbMime)) mb' ->
@@ -188,18 +191,22 @@ getDeferredMedia (DeferredMediaBag mb defMedia) = do
mb
(zip defMedia fetchedMedia)
+fetchDeferredMedia :: PandocMonad m => m ()
+fetchDeferredMedia = fetchDeferredMedia' >>= setMediaBag
+
dropDeferredMedia :: DeferredMediaBag -> MediaBag
dropDeferredMedia (DeferredMediaBag mb _) = mb
+
data CommonState = CommonState { stWarnings :: [String]
- , stMediaBag :: MediaBag
+ , stDeferredMediaBag :: DeferredMediaBag
, stInputFiles :: Maybe [FilePath]
, stOutputFile :: Maybe FilePath
}
instance Default CommonState where
def = CommonState { stWarnings = []
- , stMediaBag = mempty
+ , stDeferredMediaBag = mempty
, stInputFiles = Nothing
, stOutputFile = Nothing
}
@@ -284,7 +291,7 @@ fetchItem :: PandocMonad m
-> String
-> m (B.ByteString, Maybe MimeType)
fetchItem sourceURL s = do
- mediabag <- getMediaBag
+ mediabag <- dropDeferredMedia <$> getsCommonState stDeferredMediaBag
case lookupMedia s mediabag of
Just (mime, bs) -> return $ (BL.toStrict bs, Just mime)
Nothing ->