@@ -39,12 +39,30 @@ writeRTF :: WriterOptions -> Pandoc -> String
3939writeRTF options (Pandoc meta blocks) =
4040 let head = if writerStandalone options
4141 then rtfHeader (writerHeader options) meta
42− else ""
42+ else ""
43+ toc = if writerTableOfContents options
44+ then tableOfContents $ filter isHeaderBlock blocks
45+ else ""
4346 foot = if writerStandalone options then "\n}\n" else ""
4447 body = (writerIncludeBefore options) ++
4548 concatMap (blockToRTF 0 AlignDefault) blocks ++
4649 (writerIncludeAfter options) in
47− head ++ body ++ foot
50+ head ++ toc ++ body ++ foot
51+
52+-- | Construct table of contents from list of header blocks.
53+tableOfContents :: [Block] -> String
54+tableOfContents headers =
55+ let contentsTree = hierarchicalize headers
56+ in concatMap (blockToRTF 0 AlignDefault) $ [Header 1 [Str "Contents"],
57+ BulletList (map elementToListItem contentsTree)]
58+
59+elementToListItem :: Element -> [Block]
60+elementToListItem (Blk _) = []
61+elementToListItem (Sec sectext subsecs) =
62+ [Plain sectext] ++
63+ if null subsecs
64+ then []
65+ else [BulletList (map elementToListItem subsecs)]
4866
4967-- | Convert unicode characters (> 127) into rich text format representation.
5068handleUnicode :: String -> String