Created
September 19, 2013 08:48
-
-
Save fizruk/6620756 to your computer and use it in GitHub Desktop.
A Pandoc filter to use Pygments for Pandoc.
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
-- 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>" |
How would I use it? Can it be adapted to yield latex output.
@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
External filter for pandoc that uses Pygments to highlight code. Based on this.