Instantly share code, notes, and snippets.

What would you like to do?
A Pandoc filter to use Pygments for Pandoc.
-- A Pandoc filter to use Pygments for Pandoc
-- Code blocks in HTML output
-- Nickolay Kudasov 2013
-- Requires Pandoc 1.12
import Text.Pandoc.Definition
import Text.Pandoc.JSON (toJSONFilter)
import Text.Pandoc.Shared
import Data.Char(toLower)
import System.Process (readProcess)
import System.IO.Unsafe
main = toJSONFilter highlight
highlight :: Block -> Block
highlight (CodeBlock (_, options , _ ) code) = RawBlock (Format "html") (pygments code options)
highlight x = x
pygments:: String -> [String] -> String
pygments code options
| (length options) == 1 = unsafePerformIO $ readProcess "pygmentize" ["-l", (map toLower (head options)), "-f", "html"] code
| (length options) == 2 = unsafePerformIO $ readProcess "pygmentize" ["-l", (map toLower (head options)), "-O linenos=inline", "-f", "html"] code
| otherwise = "<div class =\"highlight\"><pre>" ++ code ++ "</pre></div>"

This comment has been minimized.


fizruk commented Sep 19, 2013

External filter for pandoc that uses Pygments to highlight code. Based on this.


This comment has been minimized.

esc commented Jun 18, 2014

How would I use it? Can it be adapted to yield latex output.


This comment has been minimized.

Debilski commented Jul 27, 2014

@esc ghc --make pygments.hs and then pandoc -F ./pygments -o someOutput.tex

By the way, the unsafePerformIO is not needed. toJSONFilter happily accepts an IO monadic action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment