git.iko.soy/typeable/pandoc
on master help sign in
Use catchError instead of runExceptT.
d3e5725John MacFarlane
src/Text/Pandoc/Writers/ICML.hs+11−9
@@ -15,7 +15,7 @@ into InDesign with File -> Place.
15-}
15-}
16module Text.Pandoc.Writers.ICML (writeICML) where
16module Text.Pandoc.Writers.ICML (writeICML) where
17import Text.Pandoc.Definition
17import Text.Pandoc.Definition
18−import Text.Pandoc.Error (PandocError)
18+import Text.Pandoc.Error (PandocError(..))
19import Text.Pandoc.XML
19import Text.Pandoc.XML
20import Text.Pandoc.Writers.Math (texMathToInlines)
20import Text.Pandoc.Writers.Math (texMathToInlines)
21import Text.Pandoc.Writers.Shared
21import Text.Pandoc.Writers.Shared
@@ -27,7 +27,7 @@ import Text.Pandoc.ImageSize
27import Data.List (isPrefixOf, isInfixOf, stripPrefix, intersperse)
27import Data.List (isPrefixOf, isInfixOf, stripPrefix, intersperse)
28import Data.Text as Text (breakOnAll, pack)
28import Data.Text as Text (breakOnAll, pack)
29import Control.Monad.State
29import Control.Monad.State
30−import Control.Monad.Except (runExceptT)
30+import Control.Monad.Except (catchError)
31import Network.URI (isURI)
31import Network.URI (isURI)
32import qualified Data.Set as Set
32import qualified Data.Set as Set
33import Text.Pandoc.Class (PandocMonad, report)
33import Text.Pandoc.Class (PandocMonad, report)
@@ -540,17 +540,19 @@ styleToStrAttr style =
540-- | Assemble an ICML Image.
540-- | Assemble an ICML Image.
541imageICML :: PandocMonad m => WriterOptions -> Style -> Attr -> Target -> WS m Doc
541imageICML :: PandocMonad m => WriterOptions -> Style -> Attr -> Target -> WS m Doc
542imageICML opts style attr (src, _) = do
542imageICML opts style attr (src, _) = do
543+ imgS <- catchError
543− res <- runExceptT $ lift $ P.fetchItem (writerSourceURL opts) src
544+ (do (img, _) <- 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
549 case imageSize opts img of
545 case imageSize opts img of
550 Right size -> return size
546 Right size -> return size
551 Left msg -> do
547 Left msg -> do
552 report $ CouldNotDetermineImageSize src msg
548 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)
554 let (ow, oh) = sizeInPoints imgS
556 let (ow, oh) = sizeInPoints imgS
555 (imgWidth, imgHeight) = desiredSizeInPoints opts attr imgS
557 (imgWidth, imgHeight) = desiredSizeInPoints opts attr imgS
556 hw = showFl $ ow / 2
558 hw = showFl $ ow / 2
src/Text/Pandoc/Writers/ODT.hs+12−10
@@ -45,8 +45,8 @@ import Text.Pandoc.Walk
45import Text.Pandoc.Writers.Shared ( fixDisplayMath )
45import Text.Pandoc.Writers.Shared ( fixDisplayMath )
46import Text.Pandoc.Writers.OpenDocument ( writeOpenDocument )
46import Text.Pandoc.Writers.OpenDocument ( writeOpenDocument )
47import Control.Monad.State
47import Control.Monad.State
48−import Control.Monad.Except (runExceptT)
48+import Control.Monad.Except (catchError)
49−import Text.Pandoc.Error (PandocError)
49+import Text.Pandoc.Error (PandocError(..))
50import Text.Pandoc.XML
50import Text.Pandoc.XML
51import Text.Pandoc.Pretty
51import Text.Pandoc.Pretty
52import System.FilePath ( takeExtension, takeDirectory, (<.>))
52import System.FilePath ( takeExtension, takeDirectory, (<.>))
@@ -146,13 +146,8 @@ pandocToODT opts doc@(Pandoc meta _) = do
146
146
147-- | transform both Image and Math elements
147-- | transform both Image and Math elements
148transformPicMath :: PandocMonad m => WriterOptions ->Inline -> O m Inline
148transformPicMath :: PandocMonad m => WriterOptions ->Inline -> O m Inline
149−transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = do
149+transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = catchError
150− res <- runExceptT $ lift $ P.fetchItem (writerSourceURL opts) src
150+ (do (img, mbMimeType) <- 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
156 (ptX, ptY) <- case imageSize opts img of
151 (ptX, ptY) <- case imageSize opts img of
157 Right s -> return $ sizeInPoints s
152 Right s -> return $ sizeInPoints s
158 Left msg -> do
153 Left msg -> do
@@ -181,7 +176,14 @@ transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = do
181 epochtime <- floor `fmap` (lift P.getPOSIXTime)
176 epochtime <- floor `fmap` (lift P.getPOSIXTime)
182 let entry = toEntry newsrc epochtime $ toLazy img
177 let entry = toEntry newsrc epochtime $ toLazy img
183 modify $ \st -> st{ stEntries = entry : entries }
178 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+
185transformPicMath _ (Math t math) = do
187transformPicMath _ (Math t math) = do
186 entries <- gets stEntries
188 entries <- gets stEntries
187 let dt = if t == InlineMath then DisplayInline else DisplayBlock
189 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
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 <-
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"
66+ _ -> throwError $
67+ PandocShouldNeverHappenError $
65− _ -> throwError $ PandocSomeError "Unknown file type"
68+ "Unknown file type " ++ mime
69+ sizeSpec <-
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
80− then do
84+ then do
81− report $ CouldNotFetchResource src "image contained no data"
85+ report $ CouldNotFetchResource src "image contained no data"
82− return x
86+ return x
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"
86− return x
90+ return x
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
95+ case e of
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
94
101
95-- | Convert Pandoc to a string in rich text format.
102-- | Convert Pandoc to a string in rich text format.