summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-07-30 13:47:07 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2014-07-30 13:47:07 -0700
commit28321a18bfcf87e5a874ad86c0c7ec82ed3848e4 (patch)
tree0322fa535425c4d04282fde73580a6929d00fe70
parent67c5c7a5752591b1f894636374e730904c34a1d5 (diff)
Shared: Added fetchItem', which searches a media bag too.
-rw-r--r--src/Text/Pandoc/Shared.hs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index d296888d6..53f20f232 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -79,6 +79,7 @@ module Text.Pandoc.Shared (
readDataFile,
readDataFileUTF8,
fetchItem,
+ fetchItem',
openURL,
-- * Error handling
err,
@@ -791,6 +792,19 @@ fetchItem sourceURL s
cont <- BS.readFile $ unEscapeString s
return (cont, mime)
+-- | Like 'fetchItem', but also looks for items in a 'MediaBag'.
+fetchItem' :: Maybe MediaBag -> Maybe String -> String
+ -> IO (Either E.SomeException (BS.ByteString, Maybe String))
+fetchItem' Nothing sourceURL s = fetchItem sourceURL s
+fetchItem' (Just media) sourceURL s = do
+ case M.lookup s media of
+ Nothing -> fetchItem sourceURL s
+ Just bs -> do
+ let mime = case takeExtension s of
+ ".gz" -> getMimeType $ dropExtension s
+ x -> getMimeType x
+ return $ Right (BS.concat $ toChunks bs, mime)
+
-- | Read from a URL and return raw data and maybe mime type.
openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe String))
openURL u