Hakyll Pandoc filtering

Posted on October 2, 2012
Tags: hacksoft

Hakyll Setup Series

  1. Setup Mathjax
  2. Setup PlantUML
  3. Setup autobuild Hakyll site Git action CI
  4. Very Simple Hakyll Pandoc Filtering Example
  5. Add Railroad Syntax to Hakyll
  6. Table Of Content in Hakyll
  7. Hakyll Access on LAN server

Pandoc filtering and translation - similar to a compiler
It will translate certain markdown strings you type up into another form.

Here I will show the simplest example:

Append a text “EOF” string to all of our codeblocks automatically

import           Text.Pandoc 
import           Text.Pandoc.Walk
import           Data.Text  
addToCodeBlock :: Pandoc -> Pandoc 
addToCodeBlock  = walk ftranslate 
  where ftranslate :: Block -> Block
        ftranslate (CodeBlock attr txt ) = CodeBlock attr (txt <> "EOF")
        ftranslate x = x 
		
simpleCompiler :: Compiler (Item String)
simpleCompiler = pandocCompilerWithTransform defaultHakyllReaderOptions defaultHakyllWriterOptions addToCodeBlock
main :: IO ()
main = do
    hakyllWith config $ do
    ...
      match "posts/*" $ do
                route $ setExtension "html"
                compile $ simpleCompiler --ONLY CHANGE THIS
                    >>= loadAndApplyTemplate "templates/post.html"    (postCtxWithTags tags)
                    ...

1 Showcase

For example in my hakyll folder, I create a new file “2099-01-01-NewBlogPost.markdown” and the contents are

---
title: Hello World
tags: tech
---

This is my blog post.
'''python
print("Hello World")

'''

The codeblock will show:

print("Hello World")
EOF

2 Extra

Text refers to Data.Text.Text

CodeBlock Attr Text Attr = (Text, [Text], [(Text, Text)])

CodeBlock (Text, [Text], [(Text, Text)]) Text

We can convert CodeBlock to Dom Elements with RawBlock.

RawBlock Format Text

example: RawBlock (Format "html") Text