Skip to content

Instantly share code, notes, and snippets.

@fizruk
Created September 19, 2013 08:48
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fizruk/6620756 to your computer and use it in GitHub Desktop.
Save fizruk/6620756 to your computer and use it in GitHub Desktop.
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>"
@fizruk
Copy link
Author

fizruk commented Sep 19, 2013

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

@esc
Copy link

esc commented Jun 18, 2014

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

@Debilski
Copy link

@esc ghc --make pygments.hs and then pandoc -F ./pygments someFile.md -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