git.iko.soy/typeable/pandoc
on master help sign in
ImageSize: make imageSize return an Either, not a Maybe.

This will give us better error reporting options. This is part of a fix for #1834.

commit6fe243abbdd3394c8c5fa50db107e4dcc87c6a63 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 9 May 2015, 21:32 (11 years ago) parent7920a1a
4 files changed+22−16
src/Text/Pandoc/ImageSize.hs+11−9 src/Text/Pandoc/Writers/Docx.hs+4−2 src/Text/Pandoc/Writers/ODT.hs+4−2 src/Text/Pandoc/Writers/RTF.hs+3−3
src/Text/Pandoc/ImageSize.hs+11−9
@@ -70,15 +70,17 @@ imageType img = case B.take 4 img of
7070 -> return Eps
7171 _ -> (hush . Left) "Unknown image type"
7272
73−imageSize :: ByteString -> Maybe ImageSize
74−imageSize img = do
75− t <- imageType img
76− case t of
77− Png -> pngSize img
78− Gif -> gifSize img
79− Jpeg -> jpegSize img
80− Eps -> epsSize img
81− Pdf -> Nothing -- TODO
73+imageSize :: ByteString -> Either String ImageSize
74+imageSize img =
75+ case imageType img of
76+ Just Png -> mbToEither "could not determine PNG size" $ pngSize img
77+ Just Gif -> mbToEither "could not determine GIF size" $ gifSize img
78+ Just Jpeg -> mbToEither "could not determine JPEG size" $ jpegSize img
79+ Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
80+ Just Pdf -> Left "could not determine PDF size" -- TODO
81+ Nothing -> Left "could not determine image type"
82+ where mbToEither msg Nothing = Left msg
83+ mbToEither _ (Just x) = Right x
8284
8385defaultSize :: (Integer, Integer)
8486defaultSize = (72, 72)
src/Text/Pandoc/Writers/Docx.hs+4−2
@@ -1120,8 +1120,10 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
11201120 inlinesToOpenXML opts alt
11211121 Right (img, mt) -> do
11221122 ident <- ("rId"++) `fmap` getUniqueId
1123− let size = imageSize img
1124− let (xpt,ypt) = maybe (120,120) sizeInPoints size
1123+ (xpt,ypt) <- case imageSize img of
1124+ Right size -> return $ sizeInPoints size
1125+ Left msg -> do liftIO (warn msg)
1126+ return (120,120)
11251127 -- 12700 emu = 1 pt
11261128 let (xemu,yemu) = fitToPage (xpt * 12700, ypt * 12700) (pageWidth * 12700)
11271129 let cNvPicPr = mknode "pic:cNvPicPr" [] $
src/Text/Pandoc/Writers/ODT.hs+4−2
@@ -134,8 +134,10 @@ transformPicMath opts entriesRef (Image lab (src,t)) = do
134134 warn $ "Could not find image `" ++ src ++ "', skipping..."
135135 return $ Emph lab
136136 Right (img, mbMimeType) -> do
137− let size = imageSize img
138− let (w,h) = fromMaybe (0,0) $ sizeInPoints `fmap` size
137+ (w,h) <- case imageSize img of
138+ Right size -> return $ sizeInPoints size
139+ Left msg -> do warn msg
140+ return (0,0)
139141 let tit' = show w ++ "x" ++ show h
140142 entries <- readIORef entriesRef
141143 let extension = fromMaybe (takeExtension $ takeWhile (/='?') src)
src/Text/Pandoc/Writers/RTF.hs+3−3
@@ -56,9 +56,9 @@ rtfEmbedImage opts x@(Image _ (src,_)) = do
5656 "image/jpeg" -> "\\jpegblip"
5757 "image/png" -> "\\pngblip"
5858 _ -> error "Unknown file type"
59− let sizeSpec = case imageSize imgdata of
60− Nothing -> ""
61− Just sz -> "\\picw" ++ show xpx ++
59+ sizeSpec <- case imageSize imgdata of
60+ Left msg -> warn msg >> return ""
61+ Right sz -> return $ "\\picw" ++ show xpx ++
6262 "\\pich" ++ show ypx ++
6363 "\\picwgoal" ++ show (xpt * 20)
6464 ++ "\\pichgoal" ++ show (ypt * 20)