diff options
author | Jesse Rosenthal <jrosenthal@jhu.edu> | 2016-12-13 19:35:07 -0500 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-01-25 17:07:42 +0100 |
commit | 5814096d79770edabc2822ff66747e3559c61e76 (patch) | |
tree | 6a1f0c92740dccdc5516c47f3353b0d7c23f2362 /src/Text | |
parent | adcd4c5b7b116d59360f2e0e05472b3df98d1de2 (diff) | |
download | pandoc-5814096d79770edabc2822ff66747e3559c61e76.tar.gz |
Introduce DeferredMediaBag.
This is a lazy MediaBag, that will only be evaluated (downloaded/read
in) upon demand.
Note that we use fetchItem in getDefferedMedia at the moment to read
in/download. This means that we don't need to distinguish between URIs
and FilePaths. But there is an inefficiency here: `fetchItem` will pull
an item out of the mediaBag if it's already there, and then we'll
reinsert it. We could separate out `fetchItem` into the function that
checks the MediaBag and the underlying downloader/read-inner.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Class.hs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs index 9c11256c8..4412e8d76 100644 --- a/src/Text/Pandoc/Class.hs +++ b/src/Text/Pandoc/Class.hs @@ -100,6 +100,7 @@ import Data.Default import System.IO.Error import qualified Data.Map as M import Text.Pandoc.Error +import Data.Monoid class (Functor m, Applicative m, Monad m, MonadError PandocError m) => PandocMonad m where @@ -167,6 +168,29 @@ warningWithPos pos msg = lift $ warning $ msg ++ " " ++ show pos -- +newtype DeferredMediaPath = DeferredMediaPath {unDefer :: String} + deriving (Show, Eq) + +data DeferredMediaBag = DeferredMediaBag MediaBag [DeferredMediaPath] + deriving (Show) + +instance Monoid DeferredMediaBag where + mempty = DeferredMediaBag mempty mempty + mappend (DeferredMediaBag mb lst) (DeferredMediaBag mb' lst') = + DeferredMediaBag (mb <> mb') (lst <> lst') + +getDeferredMedia :: PandocMonad m => DeferredMediaBag -> m MediaBag +getDeferredMedia (DeferredMediaBag mb defMedia) = do + fetchedMedia <- mapM (\dfp -> fetchItem Nothing (unDefer dfp)) defMedia + return $ foldr + (\(dfp, (bs, mbMime)) mb' -> + MB.insertMedia (unDefer dfp) mbMime (BL.fromStrict bs) mb') + mb + (zip defMedia fetchedMedia) + +dropDeferredMedia :: DeferredMediaBag -> MediaBag +dropDeferredMedia (DeferredMediaBag mb _) = mb + data CommonState = CommonState { stWarnings :: [String] , stMediaBag :: MediaBag , stInputFiles :: Maybe [FilePath] |