git.iko.soy/typeable/pandoc
on master help sign in
Docx Writer: Initial support for --toc option (issue #458)
5ab839aNikolay Yakimov
src/Text/Pandoc/Writers/Docx.hs+29−1
@@ -581,6 +581,33 @@ mkLvl marker lvl =
581getNumId :: WS Int
581getNumId :: WS Int
582getNumId = (((baseListId - 1) +) . length) `fmap` gets stLists
582getNumId = (((baseListId - 1) +) . length) `fmap` gets stLists
583
583
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+
584-- | Convert Pandoc document to two lists of
611-- | Convert Pandoc document to two lists of
585-- OpenXML elements (the main document and footnotes).
612-- OpenXML elements (the main document and footnotes).
586writeOpenXML :: WriterOptions -> Pandoc -> WS ([Element], [Element])
613writeOpenXML :: WriterOptions -> Pandoc -> WS ([Element], [Element])
@@ -613,7 +640,8 @@ writeOpenXML opts (Pandoc meta blocks) = do
613 let blocks' = bottomUp convertSpace blocks
640 let blocks' = bottomUp convertSpace blocks
614 doc' <- (setFirstPara >> blocksToOpenXML opts blocks')
641 doc' <- (setFirstPara >> blocksToOpenXML opts blocks')
615 notes' <- reverse `fmap` gets stFootnotes
642 notes' <- reverse `fmap` gets stFootnotes
643+ toc <- makeTOC opts
616− let meta' = title ++ subtitle ++ authors ++ date ++ abstract
644+ let meta' = title ++ subtitle ++ authors ++ date ++ toc ++ abstract
617 return (meta' ++ doc', notes')
645 return (meta' ++ doc', notes')
618
646
619-- | Convert a list of Pandoc blocks to OpenXML.
647-- | Convert a list of Pandoc blocks to OpenXML.