Skip to content

Instantly share code, notes, and snippets.

@btbytes btbytes/

forked from adityam/
Created Jan 30, 2018
What would you like to do?
Run a preprocessor in Hakyll
Example file

This is an example file to show how preprocessing works. If you want to show source code in a document, you can directly typing it in. However, that means that we cannot compile the code to check if it is working. To test the code, we must include it in a separate file, and then include the file in the main text.

pandoc does not provide any means of including an external file, so we can use another standard tool, like gpp to include external files. To avoid clash, I use gpp with -H option. In this mode, we need to use the pre-processor macros inside <...>. So, to include an example at src/hello.c` we write

<#include "src/hello.c">
#include <stdio.h>
main() {
printf("Hello World\n");
#! /usr/bin/env runhaskell
{-# LANGUAGE OverloadedStrings #-}
import Control.Arrow ((>>>), (***), arr)
import Hakyll
import Text.Pandoc
-- Create HTML5 Document with MathJax
html5WriterOptions :: WriterOptions
html5WriterOptions = defaultHakyllWriterOptions
{ writerHtml5 = True
, writerSlideVariant = NoSlides
, writerSectionDivs = True
, writerHTMLMathMethod = MathJax ""
-- Run gpp as a preprocessor
preprocess :: Compiler Resource String
preprocess = getResourceString >>> unixFilter "gpp" ["-H"]
-- Hook gpp in pageCompiler
-- See
preprocessPageCompilerWith :: ParserState -> WriterOptions
-> Compiler Resource (Page String)
preprocessPageCompilerWith state options = preprocessPageCompilerWithPandoc state options id
preprocessPageCompilerWithPandoc :: ParserState -> WriterOptions
-> (Pandoc -> Pandoc)
-> Compiler Resource (Page String)
preprocessPageCompilerWithPandoc state options f = cached cacheName $
preprocess >>> arr readPage
>>> addDefaultFields >>> arr applySelf
>>> pageReadPandocWith state
>>> arr (fmap (writePandocWith options . f))
cacheName = "Hakyll.Web.Page.pageCompilerWithPandoc"
main :: IO ()
main = hakyll $ do
match "" $ do
route $ setExtension "html"
compile $ preprocessPageCompilerWith defaultHakyllParserState html5WriterOptions
-- vim: nospell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.