Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pandoc filter: convert horizontal rules into section breaks for Word output
#!/usr/bin/env runhaskell
{-
Pandoc filter to replace horizontal rules with hard section breaks when output is in Word format.
Credits: This is a very lightly adapted version of a `\newpage` filter
previously described on pandoc-discuss:
https://groups.google.com/forum/#!topic/pandoc-discuss/FzLrhk0vVbU
Usage:
pandoc --filter pandoc-word-sectionbreak.hs input.md -o output.docx
Alternately, compile the filter, store it anywhere you like,
add it to your PATH, and call the compiled version:
ghc --make pandoc-word-sectionbreak.hs
rm pandoc-word-sectionbreak.hi pandoc-word-sectionbreak.o
mv pandoc-word-sectionbreak /path/to/your/binaries/pandoc-word-sectionbreak
export PATH=$PATH:/path/to/your/binaries
pandoc --filter pandoc-word-sectionbreak input.md -o output.docx
N.B.:
As far as I know, this filter ONLY works correctly when output is a Word document.
It *may* work with other xml formats, but I haven't tried.
When run with other output formats, it removes the horizonal rules entirely.
This is probably not what you want.
-}
import Text.Pandoc.JSON
secBrkXml :: String
secBrkXml = "<w:p><w:pPr><w:sectPr><w:type w:val=\"nextPage\"/></w:sectPr></w:pPr></w:p>"
secBrkBlock :: Block
secBrkBlock = RawBlock (Format "openxml") secBrkXml
insertSecBrks :: Block -> Block
insertSecBrks HorizontalRule = secBrkBlock
insertSecBrks blk = blk
main = toJSONFilter insertSecBrks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.