From 56efd6176f0b9ed4df366116e69c3712fdfa36db Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Mon, 9 Jul 2007 00:51:07 +0000 Subject: Added support for --toc to RTF writer. git-svn-id: https://pandoc.googlecode.com/svn/trunk@657 788f1e2b-df1e-0410-8736-df70ead52e1b --- src/Text/Pandoc/Writers/RTF.hs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/Text/Pandoc/Writers') diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs index 9adc2a980..229de12a3 100644 --- a/src/Text/Pandoc/Writers/RTF.hs +++ b/src/Text/Pandoc/Writers/RTF.hs @@ -39,12 +39,30 @@ writeRTF :: WriterOptions -> Pandoc -> String writeRTF options (Pandoc meta blocks) = let head = if writerStandalone options then rtfHeader (writerHeader options) meta - else "" + 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 ++ body ++ foot + head ++ toc ++ body ++ foot + +-- | Construct table of contents from list of header blocks. +tableOfContents :: [Block] -> String +tableOfContents headers = + let contentsTree = hierarchicalize headers + in concatMap (blockToRTF 0 AlignDefault) $ [Header 1 [Str "Contents"], + BulletList (map elementToListItem contentsTree)] + +elementToListItem :: Element -> [Block] +elementToListItem (Blk _) = [] +elementToListItem (Sec sectext subsecs) = + [Plain sectext] ++ + if null subsecs + then [] + else [BulletList (map elementToListItem subsecs)] -- | Convert unicode characters (> 127) into rich text format representation. handleUnicode :: String -> String -- cgit v1.2.3