@@ -79,6 +79,7 @@ module Text.Pandoc.Shared (
7979 readDataFile,
8080 readDataFileUTF8,
8181 fetchItem,
82+ fetchItem',
8283 openURL,
8384 -- * Error handling
8485 err,
@@ -791,6 +792,19 @@ fetchItem sourceURL s
791792 cont <- BS.readFile $ unEscapeString s
792793 return (cont, mime)
793794
795+-- | Like 'fetchItem', but also looks for items in a 'MediaBag'.
796+fetchItem' :: Maybe MediaBag -> Maybe String -> String
797+ -> IO (Either E.SomeException (BS.ByteString, Maybe String))
798+fetchItem' Nothing sourceURL s = fetchItem sourceURL s
799+fetchItem' (Just media) sourceURL s = do
800+ case M.lookup s media of
801+ Nothing -> fetchItem sourceURL s
802+ Just bs -> do
803+ let mime = case takeExtension s of
804+ ".gz" -> getMimeType $ dropExtension s
805+ x -> getMimeType x
806+ return $ Right (BS.concat $ toChunks bs, mime)
807+
794808-- | Read from a URL and return raw data and maybe mime type.
795809openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe String))
796810openURL u