aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Text/Pandoc/Writers/RTF.hs54
-rw-r--r--templates/rtf.template33
-rw-r--r--tests/writer.rtf4
3 files changed, 40 insertions, 51 deletions
diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs
index 3830be705..9f3b9de2a 100644
--- a/src/Text/Pandoc/Writers/RTF.hs
+++ b/src/Text/Pandoc/Writers/RTF.hs
@@ -31,24 +31,31 @@ module Text.Pandoc.Writers.RTF ( writeRTF ) 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 )
-- | Convert Pandoc to a string in rich text format.
writeRTF :: WriterOptions -> Pandoc -> String
-writeRTF options (Pandoc meta blocks) =
- "" -- TODO
--- let head' = if writerStandalone options
--- then rtfHeader (writerHeader options) meta
--- else ""
--- toc = if writerTableOfContents options
--- then tableOfContents $ filter isHeaderBlock blocks
--- else ""
--- foot = if writerStandalone options then "\n}\n" else ""
--- body = writerIncludeBefore options ++
--- concatMap (blockToRTF 0 AlignDefault) blocks ++
--- writerIncludeAfter options
--- in head' ++ toc ++ body ++ foot
+writeRTF options (Pandoc (Meta title authors date) blocks) =
+ let titletext = inlineListToRTF title
+ authorstext = map inlineListToRTF authors
+ datetext = inlineListToRTF date
+ spacer = not $ all null $ titletext : datetext : authorstext
+ body = writerIncludeBefore options ++
+ concatMap (blockToRTF 0 AlignDefault) blocks ++
+ writerIncludeAfter options
+ context = writerVariables options ++
+ [ ("body", body)
+ , ("title", titletext)
+ , ("date", datetext) ] ++
+ [ ("author", a) | a <- authorstext ] ++
+ [ ("spacer", "yes") | spacer ] ++
+ [ ("toc", tableOfContents $ filter isHeaderBlock blocks) |
+ writerTableOfContents options ]
+ in if writerStandalone options
+ then renderTemplate context $ writerTemplate options
+ else body
-- | Construct table of contents from list of header blocks.
tableOfContents :: [Block] -> String
@@ -140,27 +147,6 @@ orderedMarkers indent (start, style, delim) =
_ -> orderedListMarkers (start, LowerAlpha, Period)
else orderedListMarkers (start, style, delim)
--- | Returns RTF header.
-rtfHeader :: String -- ^ header text
- -> Meta -- ^ bibliographic information
- -> String
-rtfHeader headerText (Meta title authors date) =
- let titletext = if null title
- then ""
- else rtfPar 0 0 AlignCenter $
- "\\b \\fs36 " ++ inlineListToRTF title
- authorstext = if null authors
- then ""
- else rtfPar 0 0 AlignCenter (" " ++ (intercalate "\\" $
- map inlineListToRTF authors))
- datetext = if null date
- then ""
- else rtfPar 0 0 AlignCenter (" " ++ inlineListToRTF date) in
- let spacer = if null (titletext ++ authorstext ++ datetext)
- then ""
- else rtfPar 0 0 AlignDefault "" in
- headerText ++ titletext ++ authorstext ++ datetext ++ spacer
-
-- | Convert Pandoc block element to RTF.
blockToRTF :: Int -- ^ indent level
-> Alignment -- ^ alignment
diff --git a/templates/rtf.template b/templates/rtf.template
index b0d7cb1a6..40030077b 100644
--- a/templates/rtf.template
+++ b/templates/rtf.template
@@ -1,22 +1,25 @@
+$if(legacy-header)$
+$legacy-header$
+$else$
{\rtf1\ansi\deff0{\fonttbl{\f0 \fswiss Helvetica;}{\f1 Courier;}}
{\colortbl;\red255\green0\blue0;\red0\green0\blue255;}
\widowctrl\hyphauto
-
+$endif$
+$for(header-includes)$
$header-includes$
-{\pard \qc \f0 \sa180 \li0 \fi0 \b \fs36 {\i title}\par}
-{\pard \qc \f0 \sa180 \li0 \fi0 $authors$\par}
+$endfor$
+
+$if(title)$
+{\pard \qc \f0 \sa180 \li0 \fi0 \b \fs36 $title$\par}
+$endif$
+$for(author)$
+{\pard \qc \f0 \sa180 \li0 \fi0 $author$\par}
+$endfor$
+$if(date)$
{\pard \qc \f0 \sa180 \li0 \fi0 $date$\par}
+$endif$
+$if(spacer)$
{\pard \ql \f0 \sa180 \li0 \fi0 \par}
-{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs36 Contents\par}
-{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab section oen\sa180\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs36 section oen\par}
-{\pard \ql \f0 \sa0 \li360 \fi-360 1.\tx360\tab one\par}
-{\pard \ql \f0 \sa0 \li720 \fi-360 a.\tx360\tab two\par}
-{\pard \ql \f0 \sa0 \li1080 \fi-360 iii.\tx360\tab three\sa180\sa180\sa180\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 \f1 hi\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 footnote{\super\chftn}{\*\footnote\chftn\~\plain\pard {\pard \ql \f0 \sa180 \li0 \fi0 with code\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 \f1 code\par}
-}\par}
-
+$endif$
+$body$
}
-
diff --git a/tests/writer.rtf b/tests/writer.rtf
index 46ee22c28..ae3776261 100644
--- a/tests/writer.rtf
+++ b/tests/writer.rtf
@@ -3,7 +3,8 @@
\widowctrl\hyphauto
{\pard \qc \f0 \sa180 \li0 \fi0 \b \fs36 Pandoc Test Suite\par}
-{\pard \qc \f0 \sa180 \li0 \fi0 John MacFarlane\Anonymous\par}
+{\pard \qc \f0 \sa180 \li0 \fi0 John MacFarlane\par}
+{\pard \qc \f0 \sa180 \li0 \fi0 Anonymous\par}
{\pard \qc \f0 \sa180 \li0 \fi0 July 17, 2006\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \par}
{\pard \ql \f0 \sa180 \li0 \fi0 This is a set of tests for pandoc. Most of them are adapted from John Gruber\u8217's markdown test suite.\par}
@@ -448,6 +449,5 @@ links
{\pard \ql \f0 \sa0 \li360 \fi-360 1.\tx360\tab And in list items.{\super\chftn}{\*\footnote\chftn\~\plain\pard {\pard \ql \f0 \sa180 \li0 \fi0 In list.\par}
}\sa180\par}
{\pard \ql \f0 \sa180 \li0 \fi0 This paragraph should not be part of the note, as it is not indented.\par}
-
}