Skip to content

Instantly share code, notes, and snippets.

@infotroph
Last active June 6, 2018 05:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infotroph/5458cb5b4370092f9c32 to your computer and use it in GitHub Desktop.
Save infotroph/5458cb5b4370092f9c32 to your computer and use it in GitHub Desktop.
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