Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Last active August 29, 2015 14:08
Show Gist options
  • Save lierdakil/461cf6b0ef67f8cee47e to your computer and use it in GitHub Desktop.
Save lierdakil/461cf6b0ef67f8cee47e to your computer and use it in GitHub Desktop.
Pandoc filter to insert toc openxml in the beginning of document
import Text.Pandoc.JSON
tocXml :: String
tocXml = "<w:sdt><w:sdtPr><w:docPartObj>"++
"<w:docPartGallery w:val=\"Table of Contents\" /><w:docPartUnique />"++
"</w:docPartObj></w:sdtPr><w:sdtContent><w:p><w:pPr>"++
"<w:pStyle w:val=\"TOCHeading\" /></w:pPr><w:r>"++
"<w:t>Table of Contents</w:t></w:r></w:p><w:p><w:r>"++
"<w:fldChar w:fldCharType=\"begin\" w:dirty=\"true\" />"++
"<w:instrText xml:space=\"preserve\"> TOC \\o \"1-3\" \\h \\z \\u "++
"</w:instrText><w:fldChar w:fldCharType=\"separate\" />"++
"<w:fldChar w:fldCharType=\"end\" /></w:r></w:p></w:sdtContent></w:sdt>"
tocBlock :: Block
tocBlock = RawBlock (Format "openxml") tocXml
go :: Pandoc -> Pandoc
go (Pandoc meta blk) =Pandoc meta (tocBlock:blk)
-- One could also replace header with text "toc"
-- go :: Block -> Block
-- go (Header 1 _ [Str "toc"]) = tocBlock
main :: IO ()
main = toJSONFilter go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment