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