Last active
June 6, 2018 05:42
-
-
Save infotroph/5458cb5b4370092f9c32 to your computer and use it in GitHub Desktop.
Pandoc filter: convert horizontal rules into section breaks for Word output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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