Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created March 31, 2016 11:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scturtle/9236b7208aab9985a3af0d319bdb4041 to your computer and use it in GitHub Desktop.
Save scturtle/9236b7208aab9985a3af0d319bdb4041 to your computer and use it in GitHub Desktop.
hakyll use external pygments
customPandocCompiler :: Compiler (Item String)
customPandocCompiler =
let extraExtensions =
[ Ext_east_asian_line_breaks
, Ext_tex_math_double_backslash
]
customExtensions = foldr S.insert pandocExtensions extraExtensions
writerOptions = defaultHakyllWriterOptions {
writerExtensions = customExtensions
, writerHighlight = True
, writerHTMLMathMethod = MathJax ""
}
in pandocCompilerWithTransformM defaultHakyllReaderOptions
writerOptions
pygmentize
-- modified from bitbucket.org/honk/pandoc-filters
pygmentize :: Pandoc -> Compiler Pandoc
pygmentize (Pandoc meta bs) = Pandoc meta <$> mapM highlight bs
highlight :: Block -> Compiler Block
highlight (CodeBlock (_, options, _) code) =
RawBlock "html" <$> unsafeCompiler (pygments code options)
highlight x = return x
pygments :: String -> [String] -> IO String
pygments code options =
case options of
(lang:_) ->
readProcess "pygmentize" ["-l", toLower <$> lang, "-f", "html"] code
_ -> return $ "<div class =\"highlight\"><pre>" ++ code ++ "</pre></div>"
Copy link

ghost commented Feb 8, 2017

Hey! Any insight on how to use this? I added it to my Hakyll site's Main.hs file and I'm getting the following errors:

[2 of 2] Compiling Main             ( src/Main.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/brandonstil-es-exe/brandonstil-es-exe-tmp/Main.o )

/Users/brandon/Code/brandonstil.es/src/Main.hs:165:32: error:
    Not in scope: ‘S.insert’
    No module named ‘S’ is imported.

/Users/brandon/Code/brandonstil.es/src/Main.hs:167:11: error: Not in scope: ‘writerExtensions’

/Users/brandon/Code/brandonstil.es/src/Main.hs:168:11: error: Not in scope: ‘writerHighlight’

/Users/brandon/Code/brandonstil.es/src/Main.hs:169:11: error: Not in scope: ‘writerHTMLMathMethod’

/Users/brandon/Code/brandonstil.es/src/Main.hs:176:15: error:
    Not in scope: type constructor or class ‘Pandoc’

/Users/brandon/Code/brandonstil.es/src/Main.hs:176:34: error:
    Not in scope: type constructor or class ‘Pandoc’

/Users/brandon/Code/brandonstil.es/src/Main.hs:177:13: error: Not in scope: data constructor ‘Pandoc’

/Users/brandon/Code/brandonstil.es/src/Main.hs:179:14: error:
    Not in scope: type constructor or class ‘Block’

/Users/brandon/Code/brandonstil.es/src/Main.hs:179:32: error:
    Not in scope: type constructor or class ‘Block’

/Users/brandon/Code/brandonstil.es/src/Main.hs:180:12: error:
    Not in scope: data constructor ‘CodeBlock’

If the above build failed with a linker error or .so/DLL error, addressing these may help:
Warning: module not listed in brandonstil-es.cabal for 'brandonstil-es-exe' component (add to other-modules): Lib

--  While building package brandonstil-es-0.1.0.0 using:
      /Users/brandon/.stack/setup-exe-cache/x86_64-osx/setup-Simple-Cabal-1.24.2.0-ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:brandonstil-es exe:brandonstil-es-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

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