@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, CPP #-}
1+{-# LANGUAGE OverloadedStrings, CPP, ScopedTypeVariables #-}
22{-
33Copyright (C) 2012-2014 John MacFarlane <jgm@berkeley.edu>
44
@@ -40,7 +40,8 @@ import System.Directory
4040import Data.Digest.Pure.SHA (showDigest, sha1)
4141import System.Environment
4242import Control.Monad (unless, (<=<))
43−import Control.Applicative ((<$>), (<$))
43+import qualified Control.Exception as E
44+import Control.Applicative ((<$))
4445import Data.List (isInfixOf)
4546import Data.Maybe (fromMaybe)
4647import qualified Text.Pandoc.UTF8 as UTF8
@@ -105,24 +106,29 @@ convertImages tmpdir (Image ils (src, tit)) = do
105106 img <- convertImage tmpdir src
106107 newPath <-
107108 case img of
108− Left _ -> src <$ (warn $ "Unable to convert image `" ++ src ++ "'")
109− Right (fp, action) -> fp <$ action
109+ Left e -> src <$
110+ warn ("Unable to convert image `" ++ src ++ "':\n" ++ e)
111+ Right fp -> return fp
110112 return (Image ils (newPath, tit))
111113convertImages _ x = return x
112114
113115-- Convert formats which do not work well in pdf to png
114−convertImage :: FilePath -> FilePath -> IO (Either String ((FilePath, IO ())))
116+convertImage :: FilePath -> FilePath -> IO (Either String FilePath)
115117convertImage tmpdir fname =
116118 case mime of
117119 Just "image/png" -> doNothing
118120 Just "image/jpeg" -> doNothing
119121 Just "application/pdf" -> doNothing
120− _ -> savePng <$> JP.readImage fname
122+ _ -> JP.readImage fname >>= \res ->
123+ case res of
124+ Left msg -> return $ Left msg
125+ Right img ->
126+ E.catch (Right fileOut <$ JP.savePngImage fileOut img) $
127+ \(e :: E.SomeException) -> return (Left (show e))
121128 where
122129 fileOut = replaceDirectory (replaceExtension fname (".png")) tmpdir
123− savePng = fmap (\x -> (fileOut, JP.savePngImage fileOut x))
124130 mime = getMimeType fname
125− doNothing = return (Right $ (fname, return ()))
131+ doNothing = return (Right fname)
126132
127133tex2pdf' :: FilePath -- ^ temp directory for output
128134 -> String -- ^ tex program