git.iko.soy/typeable/pandoc
on master help sign in
fillMediaBag: don't cause fatal error if resource not found.

Report warning instead and change image to its alt text.

commitf8e125f42d8568b9f2926c2d1a3eb37acba2b3d1 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 7 May 2017, 12:16 (9 years ago) parentaf8860d
1 file changed+19−10
src/Text/Pandoc/App.hs+19−10
src/Text/Pandoc/App.hs+19−10
@@ -39,6 +39,7 @@ module Text.Pandoc.App (
3939 ) where
4040import Control.Applicative ((<|>))
4141import qualified Control.Exception as E
42+import Control.Monad.Except (catchError, throwError)
4243import Control.Monad
4344import Control.Monad.Trans
4445import Data.Aeson (eitherDecode', encode)
@@ -70,7 +71,7 @@ import System.IO.Error (isDoesNotExistError)
7071import Text.Pandoc
7172import Text.Pandoc.Builder (setMeta)
7273import Text.Pandoc.Class (PandocIO, getLog, withMediaBag, getMediaBag,
73− fetchItem, insertMedia)
74+ fetchItem, insertMedia, report)
7475import Text.Pandoc.Highlighting (highlightingStyles)
7576import Text.Pandoc.Lua ( runLuaFilter )
7677import Text.Pandoc.MediaBag (extractMediaBag, mediaDirectory)
@@ -734,15 +735,23 @@ defaultWriterName x =
734735fillMedia :: Maybe String -> Pandoc -> PandocIO Pandoc
735736fillMedia sourceURL d = walkM handleImage d
736737 where handleImage :: Inline -> PandocIO Inline
737− handleImage (Image attr lab (src, tit)) = do
738− (bs, mt) <- fetchItem sourceURL src
739− let ext = fromMaybe (takeExtension src)
740− (mt >>= extensionFromMimeType)
741− let bs' = B.fromChunks [bs]
742− let basename = showDigest $ sha1 bs'
743− let fname = basename <.> ext
744− insertMedia fname mt bs'
745− return $ Image attr lab (fname, tit)
738+ handleImage (Image attr lab (src, tit)) = catchError
739+ (do (bs, mt) <- fetchItem sourceURL src
740+ let ext = fromMaybe (takeExtension src)
741+ (mt >>= extensionFromMimeType)
742+ let bs' = B.fromChunks [bs]
743+ let basename = showDigest $ sha1 bs'
744+ let fname = basename <.> ext
745+ insertMedia fname mt bs'
746+ return $ Image attr lab (fname, tit))
747+ (\e -> do
748+ case e of
749+ PandocResourceNotFound _ -> do
750+ report $ CouldNotFetchResource src
751+ "replacing image with description"
752+ -- emit alt text
753+ return $ Span ("",["image"],[]) lab
754+ _ -> throwError e)
746755 handleImage x = return x
747756
748757extractMedia :: FilePath -> Pandoc -> PandocIO Pandoc