git.iko.soy/typeable/pandoc
on master help sign in
Added support for --toc to RTF writer.
56efd61fiddlosopher
src/Text/Pandoc/Writers/RTF.hs+20−2
@@ -39,12 +39,30 @@ writeRTF :: WriterOptions -> Pandoc -> String
39writeRTF options (Pandoc meta blocks) =
39writeRTF options (Pandoc meta blocks) =
40 let head = if writerStandalone options
40 let head = if writerStandalone options
41 then rtfHeader (writerHeader options) meta
41 then rtfHeader (writerHeader options) meta
42− else ""
42+ else ""
43+ toc = if writerTableOfContents options
44+ then tableOfContents $ filter isHeaderBlock blocks
45+ else ""
43 foot = if writerStandalone options then "\n}\n" else ""
46 foot = if writerStandalone options then "\n}\n" else ""
44 body = (writerIncludeBefore options) ++
47 body = (writerIncludeBefore options) ++
45 concatMap (blockToRTF 0 AlignDefault) blocks ++
48 concatMap (blockToRTF 0 AlignDefault) blocks ++
46 (writerIncludeAfter options) in
49 (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)]
48
66
49-- | Convert unicode characters (> 127) into rich text format representation.
67-- | Convert unicode characters (> 127) into rich text format representation.
50handleUnicode :: String -> String
68handleUnicode :: String -> String