git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1527 from mpickering/juicypixels

Attempts to convert gif, tiff and bmp to png in pdf writer

commite883ef4eb9a56f2319fb90e405d0612249af5924 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 12 Aug 2014, 16:57 (12 years ago) parent7684b242b31df3
3 files changed+33−6
pandoc.cabal+2−1 src/Text/Pandoc/PDF.hs+29−3 src/Text/Pandoc/Writers/LaTeX.hs+2−2
pandoc.cabal+2−1
@@ -252,7 +252,8 @@ Library
252252 SHA >= 1.6 && < 1.7,
253253 haddock-library >= 1.1 && < 1.2,
254254 old-time,
255− deepseq-generics >= 0.1 && < 0.2
255+ deepseq-generics >= 0.1 && < 0.2,
256+ JuicyPixels >= 3.1.6.1 && < 3.2
256257 if flag(https)
257258 Build-Depends: http-client >= 0.3.2 && < 0.4,
258259 http-client-tls >= 0.2 && < 0.3,
src/Text/Pandoc/PDF.hs+29−3
@@ -39,7 +39,8 @@ import System.FilePath
3939import System.Directory
4040import Data.Digest.Pure.SHA (showDigest, sha1)
4141import System.Environment
42−import Control.Monad (unless)
42+import Control.Monad (unless, (<=<))
43+import Control.Applicative ((<$>), (<$))
4344import Data.List (isInfixOf)
4445import Data.Maybe (fromMaybe)
4546import qualified Text.Pandoc.UTF8 as UTF8
@@ -47,9 +48,10 @@ import Text.Pandoc.Definition
4748import Text.Pandoc.Walk (walkM)
4849import Text.Pandoc.Shared (fetchItem', warn, withTempDir)
4950import Text.Pandoc.Options (WriterOptions(..))
50−import Text.Pandoc.MIME (extensionFromMimeType)
51+import Text.Pandoc.MIME (extensionFromMimeType, getMimeType)
5152import Text.Pandoc.Process (pipeProcess)
5253import qualified Data.ByteString.Lazy as BL
54+import qualified Codec.Picture as JP
5355#ifdef _WINDOWS
5456import Data.List (intercalate)
5557#endif
@@ -73,7 +75,7 @@ handleImages :: WriterOptions
7375 -> FilePath -- ^ temp dir to store images
7476 -> Pandoc -- ^ document
7577 -> IO Pandoc
76−handleImages opts tmpdir = walkM (handleImage' opts tmpdir)
78+handleImages opts tmpdir = walkM (convertImages tmpdir) <=< walkM (handleImage' opts tmpdir)
7779
7880handleImage' :: WriterOptions
7981 -> FilePath
@@ -98,6 +100,30 @@ handleImage' opts tmpdir (Image ils (src,tit)) = do
98100 return $ Image ils (src,tit)
99101handleImage' _ _ x = return x
100102
103+convertImages :: FilePath -> Inline -> IO Inline
104+convertImages tmpdir (Image ils (src, tit)) = do
105+ img <- convertImage tmpdir src
106+ newPath <-
107+ case img of
108+ Left _ -> src <$ (warn $ "Unable to convert image `" ++ src ++ "'")
109+ Right (fp, action) -> fp <$ action
110+ return (Image ils (newPath, tit))
111+convertImages _ x = return x
112+
113+-- Convert formats which do not work well in pdf to png
114+convertImage :: FilePath -> FilePath -> IO (Either String ((FilePath, IO ())))
115+convertImage tmpdir fname =
116+ case mime of
117+ Just "image/png" -> doNothing
118+ Just "image/jpeg" -> doNothing
119+ Just "application/pdf" -> doNothing
120+ _ -> savePng <$> JP.readImage fname
121+ where
122+ fileOut = replaceDirectory (replaceExtension fname (".png")) tmpdir
123+ savePng = fmap (\x -> (fileOut, JP.savePngImage fileOut x))
124+ mime = getMimeType fname
125+ doNothing = return (Right $ (fname, return ()))
126+
101127tex2pdf' :: FilePath -- ^ temp directory for output
102128 -> String -- ^ tex program
103129 -> String -- ^ tex source
src/Text/Pandoc/Writers/LaTeX.hs+2−2
@@ -308,7 +308,7 @@ blockToLaTeX (Div (identifier,classes,_) bs) = do
308308 ref <- toLabel identifier
309309 let linkAnchor = if null identifier
310310 then empty
311− else "\\hyperdef{}" <> braces (text ref)
311+ else "\\hyperdef{}" <> braces (text ref) <> "{}"
312312 contents <- blockListToLaTeX bs
313313 if beamer && "notes" `elem` classes -- speaker notes
314314 then return $ "\\note" <> braces contents
@@ -672,7 +672,7 @@ inlineToLaTeX (Span (id',classes,_) ils) = do
672672 ref <- toLabel id'
673673 let linkAnchor = if null id'
674674 then empty
675− else "\\hyperdef{}" <> braces (text ref)
675+ else "\\hyperdef{}" <> braces (text ref) <> "{}"
676676 fmap (linkAnchor <>)
677677 ((if noEmph then inCmd "textup" else id) .
678678 (if noStrong then inCmd "textnormal" else id) .