summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-05-07 12:16:14 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-05-07 12:16:14 +0200
commitf8e125f42d8568b9f2926c2d1a3eb37acba2b3d1 (patch)
tree8df709fbdd515f4fdaf8c6f0a5a8e724a71438d7 /src
parentaf8860d26a2e4848cd57c785753a500e3bc3d206 (diff)
fillMediaBag: don't cause fatal error if resource not found.
Report warning instead and change image to its alt text.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/App.hs29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs
index 212ae7fe2..2efa69944 100644
--- a/src/Text/Pandoc/App.hs
+++ b/src/Text/Pandoc/App.hs
@@ -39,6 +39,7 @@ module Text.Pandoc.App (
) where
import Control.Applicative ((<|>))
import qualified Control.Exception as E
+import Control.Monad.Except (catchError, throwError)
import Control.Monad
import Control.Monad.Trans
import Data.Aeson (eitherDecode', encode)
@@ -70,7 +71,7 @@ import System.IO.Error (isDoesNotExistError)
import Text.Pandoc
import Text.Pandoc.Builder (setMeta)
import Text.Pandoc.Class (PandocIO, getLog, withMediaBag, getMediaBag,
- fetchItem, insertMedia)
+ fetchItem, insertMedia, report)
import Text.Pandoc.Highlighting (highlightingStyles)
import Text.Pandoc.Lua ( runLuaFilter )
import Text.Pandoc.MediaBag (extractMediaBag, mediaDirectory)
@@ -734,15 +735,23 @@ defaultWriterName x =
fillMedia :: Maybe String -> Pandoc -> PandocIO Pandoc
fillMedia sourceURL d = walkM handleImage d
where handleImage :: Inline -> PandocIO Inline
- handleImage (Image attr lab (src, tit)) = do
- (bs, mt) <- fetchItem sourceURL src
- let ext = fromMaybe (takeExtension src)
- (mt >>= extensionFromMimeType)
- let bs' = B.fromChunks [bs]
- let basename = showDigest $ sha1 bs'
- let fname = basename <.> ext
- insertMedia fname mt bs'
- return $ Image attr lab (fname, tit)
+ handleImage (Image attr lab (src, tit)) = catchError
+ (do (bs, mt) <- fetchItem sourceURL src
+ let ext = fromMaybe (takeExtension src)
+ (mt >>= extensionFromMimeType)
+ let bs' = B.fromChunks [bs]
+ let basename = showDigest $ sha1 bs'
+ let fname = basename <.> ext
+ insertMedia fname mt bs'
+ return $ Image attr lab (fname, tit))
+ (\e -> do
+ case e of
+ PandocResourceNotFound _ -> do
+ report $ CouldNotFetchResource src
+ "replacing image with description"
+ -- emit alt text
+ return $ Span ("",["image"],[]) lab
+ _ -> throwError e)
handleImage x = return x
extractMedia :: FilePath -> Pandoc -> PandocIO Pandoc