From 382564ed9e62a42ec03650d178f48552df0071b7 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 28 Jan 2011 08:42:04 -0800 Subject: RTF writer: Embed images when possible. * Resolves Issue #275. * PNG and JPEG supported. * Export rtfEmbedImage. --- src/Text/Pandoc.hs | 2 ++ src/Text/Pandoc/Writers/RTF.hs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc.hs b/src/Text/Pandoc.hs index 0a43b4f30..ef8560284 100644 --- a/src/Text/Pandoc.hs +++ b/src/Text/Pandoc.hs @@ -106,6 +106,8 @@ module Text.Pandoc , module Text.Pandoc.Templates -- * Version , pandocVersion + -- * Miscellaneous + , rtfEmbedImage , jsonFilter ) where diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs index 63954cebf..605e4162b 100644 --- a/src/Text/Pandoc/Writers/RTF.hs +++ b/src/Text/Pandoc/Writers/RTF.hs @@ -27,13 +27,34 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Conversion of 'Pandoc' documents to RTF (rich text format). -} -module Text.Pandoc.Writers.RTF ( writeRTF ) where +module Text.Pandoc.Writers.RTF ( writeRTF, rtfEmbedImage ) where import Text.Pandoc.Definition import Text.Pandoc.Shared import Text.Pandoc.Readers.TeXMath import Text.Pandoc.Templates (renderTemplate) import Data.List ( isSuffixOf, intercalate ) -import Data.Char ( ord, isDigit ) +import Data.Char ( ord, isDigit, toLower ) +import System.FilePath ( takeExtension ) +import qualified Data.ByteString as B +import Text.Printf ( printf ) + +-- | Convert Image inlines into a raw RTF embedded image, read from a file. +-- If file not found or filetype not jpeg or png, leave the inline unchanged. +rtfEmbedImage :: Inline -> IO Inline +rtfEmbedImage x@(Image _ (src,_)) + | map toLower (takeExtension src) `elem` [".jpg",".jpeg",".png"] = do + imgdata <- catch (B.readFile src) (\_ -> return B.empty) + let bytes = map (printf "%02x") $ B.unpack imgdata + let filetype = case map toLower (takeExtension src) of + ".jpg" -> "\\jpegblip" + ".jpeg" -> "\\jpegblip" + ".png" -> "\\pngblip" + _ -> error "Unknown file type" + let raw = "{\\pict" ++ filetype ++ " " ++ concat bytes ++ "}" + return $ if B.null imgdata + then x + else RawInline "rtf" raw +rtfEmbedImage x = return x -- | Convert Pandoc to a string in rich text format. writeRTF :: WriterOptions -> Pandoc -> String -- cgit v1.2.3