git.iko.soy/typeable/pandoc
on master help sign in
Use catchError instead of runExceptT.
commitd3e5725cbc06cca01071d4c62eac654cb243ca7b authorJohn MacFarlane <jgm@berkeley.edu> dateFri 24 Feb 2017, 15:55 (10 years ago) parent0448b7d
3 files changed+68−57
src/Text/Pandoc/Writers/ICML.hs+11−9 src/Text/Pandoc/Writers/ODT.hs+12−10 src/Text/Pandoc/Writers/RTF.hs+45−38
src/Text/Pandoc/Writers/ICML.hs+11−9
@@ -15,7 +15,7 @@ into InDesign with File -> Place.
1515-}
1616module Text.Pandoc.Writers.ICML (writeICML) where
1717import Text.Pandoc.Definition
18−import Text.Pandoc.Error (PandocError)
18+import Text.Pandoc.Error (PandocError(..))
1919import Text.Pandoc.XML
2020import Text.Pandoc.Writers.Math (texMathToInlines)
2121import Text.Pandoc.Writers.Shared
@@ -27,7 +27,7 @@ import Text.Pandoc.ImageSize
2727import Data.List (isPrefixOf, isInfixOf, stripPrefix, intersperse)
2828import Data.Text as Text (breakOnAll, pack)
2929import Control.Monad.State
30−import Control.Monad.Except (runExceptT)
30+import Control.Monad.Except (catchError)
3131import Network.URI (isURI)
3232import qualified Data.Set as Set
3333import Text.Pandoc.Class (PandocMonad, report)
@@ -540,17 +540,19 @@ styleToStrAttr style =
540540-- | Assemble an ICML Image.
541541imageICML :: PandocMonad m => WriterOptions -> Style -> Attr -> Target -> WS m Doc
542542imageICML opts style attr (src, _) = do
543− res <- runExceptT $ lift $ P.fetchItem (writerSourceURL opts) src
544− imgS <- case res of
545− Left (_ :: PandocError) -> do
546− report $ CouldNotFetchResource src ""
547− return def
548− Right (img, _) -> do
543+ imgS <- catchError
544+ (do (img, _) <- P.fetchItem (writerSourceURL opts) src
549545 case imageSize opts img of
550546 Right size -> return size
551547 Left msg -> do
552548 report $ CouldNotDetermineImageSize src msg
553− return def
549+ return def)
550+ (\e -> do
551+ case e of
552+ PandocIOError _ e' ->
553+ report $ CouldNotFetchResource src (show e')
554+ e' -> report $ CouldNotFetchResource src (show e')
555+ return def)
554556 let (ow, oh) = sizeInPoints imgS
555557 (imgWidth, imgHeight) = desiredSizeInPoints opts attr imgS
556558 hw = showFl $ ow / 2
src/Text/Pandoc/Writers/ODT.hs+12−10
@@ -45,8 +45,8 @@ import Text.Pandoc.Walk
4545import Text.Pandoc.Writers.Shared ( fixDisplayMath )
4646import Text.Pandoc.Writers.OpenDocument ( writeOpenDocument )
4747import Control.Monad.State
48−import Control.Monad.Except (runExceptT)
49−import Text.Pandoc.Error (PandocError)
48+import Control.Monad.Except (catchError)
49+import Text.Pandoc.Error (PandocError(..))
5050import Text.Pandoc.XML
5151import Text.Pandoc.Pretty
5252import System.FilePath ( takeExtension, takeDirectory, (<.>))
@@ -146,13 +146,8 @@ pandocToODT opts doc@(Pandoc meta _) = do
146146
147147-- | transform both Image and Math elements
148148transformPicMath :: PandocMonad m => WriterOptions ->Inline -> O m Inline
149−transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = do
150− res <- runExceptT $ lift $ P.fetchItem (writerSourceURL opts) src
151− case res of
152− Left (_ :: PandocError) -> do
153− report $ CouldNotFetchResource src ""
154− return $ Emph lab
155− Right (img, mbMimeType) -> do
149+transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = catchError
150+ (do (img, mbMimeType) <- P.fetchItem (writerSourceURL opts) src
156151 (ptX, ptY) <- case imageSize opts img of
157152 Right s -> return $ sizeInPoints s
158153 Left msg -> do
@@ -181,7 +176,14 @@ transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = do
181176 epochtime <- floor `fmap` (lift P.getPOSIXTime)
182177 let entry = toEntry newsrc epochtime $ toLazy img
183178 modify $ \st -> st{ stEntries = entry : entries }
184− return $ Image newattr lab (newsrc, t)
179+ return $ Image newattr lab (newsrc, t))
180+ (\e -> do
181+ case e of
182+ PandocIOError _ e' ->
183+ report $ CouldNotFetchResource src (show e')
184+ e' -> report $ CouldNotFetchResource src (show e')
185+ return $ Emph lab)
186+
185187transformPicMath _ (Math t math) = do
186188 entries <- gets stEntries
187189 let dt = if t == InlineMath then DisplayInline else DisplayBlock
src/Text/Pandoc/Writers/RTF.hs+45−38
@@ -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.