git.iko.soy/typeable/pandoc
on master help sign in
Docx Writer: Initial support for --toc option (issue #458)

Creates a "dirty" TOC at the beginning of document, before abstract

commit5ab839a187860632c5337b72c807d043107dd88b authorNikolay Yakimov <root@livid.pp.ru> dateSat 6 Dec 2014, 18:33 (12 years ago) parent2707898
1 file changed+29−1
src/Text/Pandoc/Writers/Docx.hs+29−1
src/Text/Pandoc/Writers/Docx.hs+29−1
@@ -581,6 +581,33 @@ mkLvl marker lvl =
581581getNumId :: WS Int
582582getNumId = (((baseListId - 1) +) . length) `fmap` gets stLists
583583
584+makeTOC :: WriterOptions -> WS [Element]
585+makeTOC opts | writerTableOfContents opts = do
586+ let depth = "1-"++(show (writerTOCDepth opts))
587+ let tocCmd = "TOC \\o \""++depth++"\" \\h \\z \\u"
588+ title <- withParaPropM (pStyleM "TOC Heading") (blocksToOpenXML opts [Para [Str "Table of Contents"]])
589+ return $
590+ [mknode "w:sdt" [] ([
591+ mknode "w:sdtPr" [] (
592+ mknode "w:docPartObj" [] (
593+ [mknode "w:docPartGallery" [("w:val","Table of Contents")] (),
594+ mknode "w:docPartUnique" [] ()]
595+ ) -- w:docPartObj
596+ ), -- w:sdtPr
597+ mknode "w:sdtContent" [] (title++[
598+ mknode "w:p" [] (
599+ mknode "w:r" [] ([
600+ mknode "w:fldChar" [("w:fldCharType","begin"),("w:dirty","true")] (),
601+ mknode "w:instrText" [("xml:space","preserve")] tocCmd,
602+ mknode "w:fldChar" [("w:fldCharType","separate")] (),
603+ mknode "w:fldChar" [("w:fldCharType","end")] ()
604+ ]) -- w:r
605+ ) -- w:p
606+ ])
607+ ])] -- w:sdt
608+makeTOC _ = return []
609+
610+
584611-- | Convert Pandoc document to two lists of
585612-- OpenXML elements (the main document and footnotes).
586613writeOpenXML :: WriterOptions -> Pandoc -> WS ([Element], [Element])
@@ -613,7 +640,8 @@ writeOpenXML opts (Pandoc meta blocks) = do
613640 let blocks' = bottomUp convertSpace blocks
614641 doc' <- (setFirstPara >> blocksToOpenXML opts blocks')
615642 notes' <- reverse `fmap` gets stFootnotes
616− let meta' = title ++ subtitle ++ authors ++ date ++ abstract
643+ toc <- makeTOC opts
644+ let meta' = title ++ subtitle ++ authors ++ date ++ toc ++ abstract
617645 return (meta' ++ doc', notes')
618646
619647-- | Convert a list of Pandoc blocks to OpenXML.