@@ -44,7 +44,7 @@ import qualified Data.ByteString as B
4444import qualified Data.Map as M
4545import Text.Printf ( printf )
4646import Text.Pandoc.ImageSize
47−import Control.Monad.Except (throwError, runExceptT, lift)
47+import Control.Monad.Except (throwError, catchError)
4848import Text.Pandoc.Error
4949import Text.Pandoc.Class (PandocMonad, report)
5050import qualified Text.Pandoc.Class as P
@@ -53,43 +53,50 @@ import qualified Text.Pandoc.Class as P
5353-- or a MediaBag, or the internet.
5454-- If file not found or filetype not jpeg or png, leave the inline unchanged.
5555rtfEmbedImage :: PandocMonad m => WriterOptions -> Inline -> m Inline
56−rtfEmbedImage opts x@(Image attr _ (src,_)) = do
57− result <- runExceptT $ lift $ P.fetchItem (writerSourceURL opts) src
58− case result of
59− Right (imgdata, Just mime)
60− | mime == "image/jpeg" || mime == "image/png" -> do
61− let bytes = map (printf "%02x") $ B.unpack imgdata
62− filetype <- case mime of
63− "image/jpeg" -> return "\\jpegblip"
64− "image/png" -> return "\\pngblip"
65− _ -> throwError $ PandocSomeError "Unknown file type"
66− sizeSpec <- case imageSize opts imgdata of
67− Left msg -> do
68− report $ CouldNotDetermineImageSize src msg
69− return ""
70− Right sz -> return $ "\\picw" ++ show xpx ++
71− "\\pich" ++ show ypx ++
72− "\\picwgoal" ++ show (floor (xpt * 20) :: Integer)
73− ++ "\\pichgoal" ++ show (floor (ypt * 20) :: Integer)
74− -- twip = 1/1440in = 1/20pt
75− where (xpx, ypx) = sizeInPixels sz
76− (xpt, ypt) = desiredSizeInPoints opts attr sz
77− let raw = "{\\pict" ++ filetype ++ sizeSpec ++ "\\bin " ++
78− concat bytes ++ "}"
79− if B.null imgdata
80− then do
81− report $ CouldNotFetchResource src "image contained no data"
82− return x
83− else return $ RawInline (Format "rtf") raw
84− | otherwise -> do
85− report $ CouldNotFetchResource src "image is not a jpeg or png"
86− return x
87− Right (_, Nothing) -> do
88− report $ CouldNotDetermineMimeType src
89− return x
90− Left ( e :: PandocError ) -> do
91− report $ CouldNotFetchResource src (show e)
92− return x
56+rtfEmbedImage opts x@(Image attr _ (src,_)) = catchError
57+ (do result <- P.fetchItem (writerSourceURL opts) src
58+ case result of
59+ (imgdata, Just mime)
60+ | mime == "image/jpeg" || mime == "image/png" -> do
61+ let bytes = map (printf "%02x") $ B.unpack imgdata
62+ filetype <-
63+ case mime of
64+ "image/jpeg" -> return "\\jpegblip"
65+ "image/png" -> return "\\pngblip"
66+ _ -> throwError $
67+ PandocShouldNeverHappenError $
68+ "Unknown file type " ++ mime
69+ sizeSpec <-
70+ case imageSize opts imgdata of
71+ Left msg -> do
72+ report $ CouldNotDetermineImageSize src msg
73+ return ""
74+ Right sz -> return $ "\\picw" ++ show xpx ++
75+ "\\pich" ++ show ypx ++
76+ "\\picwgoal" ++ show (floor (xpt * 20) :: Integer)
77+ ++ "\\pichgoal" ++ show (floor (ypt * 20) :: Integer)
78+ -- twip = 1/1440in = 1/20pt
79+ where (xpx, ypx) = sizeInPixels sz
80+ (xpt, ypt) = desiredSizeInPoints opts attr sz
81+ let raw = "{\\pict" ++ filetype ++ sizeSpec ++ "\\bin " ++
82+ concat bytes ++ "}"
83+ if B.null imgdata
84+ then do
85+ report $ CouldNotFetchResource src "image contained no data"
86+ return x
87+ else return $ RawInline (Format "rtf") raw
88+ | otherwise -> do
89+ report $ CouldNotFetchResource src "image is not a jpeg or png"
90+ return x
91+ (_, Nothing) -> do
92+ report $ CouldNotDetermineMimeType src
93+ return x)
94+ (\e -> do
95+ case e of
96+ PandocIOError _ e' ->
97+ report $ CouldNotFetchResource src (show e')
98+ e' -> report $ CouldNotFetchResource src (show e')
99+ return x)
93100rtfEmbedImage _ x = return x
94101
95102-- | Convert Pandoc to a string in rich text format.