@@ -37,7 +37,6 @@ module Text.Pandoc.Class.PandocMonad
3737 , setUserDataDir
3838 , getUserDataDir
3939 , fetchItem
40− , fetchMediaResource
4140 , getInputFiles
4241 , setInputFiles
4342 , getOutputFile
@@ -57,8 +56,6 @@ module Text.Pandoc.Class.PandocMonad
5756import Codec.Archive.Zip
5857import Control.Monad.Except (MonadError (catchError, throwError),
5958 MonadTrans, lift, when)
60−import Data.Digest.Pure.SHA (sha1, showDigest)
61−import Data.Maybe (fromMaybe)
6259import Data.List (foldl')
6360import Data.Time (UTCTime)
6461import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds,
@@ -67,7 +64,7 @@ import Data.Time.LocalTime (TimeZone, ZonedTime, utcToZonedTime)
6764import Network.URI ( escapeURIString, nonStrictRelativeTo,
6865 unEscapeString, parseURIReference, isAllowedInURI,
6966 parseURI, URI(..) )
70−import System.FilePath ((</>), (<.>), takeExtension, dropExtension,
67+import System.FilePath ((</>), takeExtension, dropExtension,
7168 isRelative, splitDirectories)
7269import System.Random (StdGen)
7370import Text.Collate.Lang (Lang(..), parseLang, renderLang)
@@ -75,8 +72,8 @@ import Text.Pandoc.Class.CommonState (CommonState (..))
7572import Text.Pandoc.Definition
7673import Text.Pandoc.Error
7774import Text.Pandoc.Logging
78−import Text.Pandoc.MIME (MimeType, getMimeType, extensionFromMimeType)
79−import Text.Pandoc.MediaBag (MediaBag, lookupMedia)
75+import Text.Pandoc.MIME (MimeType, getMimeType)
76+import Text.Pandoc.MediaBag (MediaBag, lookupMedia, MediaItem(..))
8077import Text.Pandoc.Shared (uriPathToPath, safeRead)
8178import Text.Pandoc.Translations (Term(..), Translations, lookupTerm,
8279 readTranslations)
@@ -376,7 +373,8 @@ fetchItem :: PandocMonad m
376373fetchItem s = do
377374 mediabag <- getMediaBag
378375 case lookupMedia (T.unpack s) mediabag of
379− Just (mime, bs) -> return (BL.toStrict bs, Just mime)
376+ Just item -> return (BL.toStrict (mediaContents item),
377+ Just (mediaMimeType item))
380378 Nothing -> downloadOrRead s
381379
382380-- | Returns the content and, if available, the MIME type of a resource.
@@ -629,19 +627,6 @@ withPaths (p:ps) action fp =
629627 catchError (action (p </> fp))
630628 (\_ -> withPaths ps action fp)
631629
632−-- | Fetch local or remote resource (like an image) and provide data suitable
633−-- for adding it to the MediaBag.
634−fetchMediaResource :: PandocMonad m
635− => T.Text -> m (FilePath, Maybe MimeType, BL.ByteString)
636−fetchMediaResource src = do
637− (bs, mt) <- fetchItem src
638− let ext = fromMaybe (T.pack $ takeExtension $ T.unpack src)
639− (mt >>= extensionFromMimeType)
640− let bs' = BL.fromChunks [bs]
641− let basename = showDigest $ sha1 bs'
642− let fname = basename <.> T.unpack ext
643− return (fname, mt, bs')
644−
645630-- | Traverse tree, filling media bag for any images that
646631-- aren't already in the media bag.
647632fillMediaBag :: PandocMonad m => Pandoc -> m Pandoc
@@ -649,12 +634,18 @@ fillMediaBag d = walkM handleImage d
649634 where handleImage :: PandocMonad m => Inline -> m Inline
650635 handleImage (Image attr lab (src, tit)) = catchError
651636 (do mediabag <- getMediaBag
652− case lookupMedia (T.unpack src) mediabag of
653− Just (_, _) -> return $ Image attr lab (src, tit)
654− Nothing -> do
655− (fname, mt, bs) <- fetchMediaResource src
656− insertMedia fname mt bs
657− return $ Image attr lab (T.pack fname, tit))
637+ let fp = T.unpack src
638+ src' <- T.pack <$> case lookupMedia fp mediabag of
639+ Just item -> return $ mediaPath item
640+ Nothing -> do
641+ (bs, mt) <- fetchItem src
642+ insertMedia fp mt (BL.fromStrict bs)
643+ mediabag' <- getMediaBag
644+ case lookupMedia fp mediabag' of
645+ Just item -> return $ mediaPath item
646+ Nothing -> throwError $ PandocSomeError $
647+ src <> " not successfully inserted into MediaBag"
648+ return $ Image attr lab (src', tit))
658649 (\e ->
659650 case e of
660651 PandocResourceNotFound _ -> do