Skip to content

Instantly share code, notes, and snippets.

@btbytes
Forked from adityam/example.md
Created January 30, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/410cdba85f068775caf51dd8654c91f0 to your computer and use it in GitHub Desktop.
Save btbytes/410cdba85f068775caf51dd8654c91f0 to your computer and use it in GitHub Desktop.
Run a preprocessor in Hakyll
title
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
-- http://groups.google.com/group/hakyll/browse_thread/thread/5322e62429605a01
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))
where
cacheName = "Hakyll.Web.Page.pageCompilerWithPandoc"
main :: IO ()
main = hakyll $ do
match "example.md" $ 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