summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2011-01-28 08:42:04 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2011-01-28 08:42:04 -0800
commit382564ed9e62a42ec03650d178f48552df0071b7 (patch)
tree7c118abf2b609dcba3e676638c6a6a2fdbb417ae /src
parent0acf774011877a8de6363a79b4bb7a5b2ed83913 (diff)
RTF writer: Embed images when possible.
* Resolves Issue #275. * PNG and JPEG supported. * Export rtfEmbedImage.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc.hs2
-rw-r--r--src/Text/Pandoc/Writers/RTF.hs25
-rw-r--r--src/pandoc.hs14
3 files changed, 34 insertions, 7 deletions
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
diff --git a/src/pandoc.hs b/src/pandoc.hs
index e2d85111e..7289eb973 100644
--- a/src/pandoc.hs
+++ b/src/pandoc.hs
@@ -823,9 +823,13 @@ main = do
doc <- fmap (reader startParserState . convertTabs . intercalate "\n") (readSources sources)
- let doc' = foldr ($) doc transforms
+ let doc0 = foldr ($) doc transforms
- doc'' <- do
+ doc1 <- if writerName' == "rtf"
+ then bottomUpM rtfEmbedImage doc0
+ else return doc0
+
+ doc2 <- do
if citeMethod == Citeproc && not (null refs)
then do
csldir <- getAppUserDataDirectory "csl"
@@ -839,10 +843,10 @@ main = do
replaceDirectory
(replaceExtension cslfile "csl")
csldir
- processBiblio cslfile' refs doc'
- else return doc'
+ processBiblio cslfile' refs doc1
+ else return doc1
- writerOutput <- writer writerOptions doc''
+ writerOutput <- writer writerOptions doc2
let writerOutput' = if standalone'
then writerOutput