Hakyll setup

Main image

tl;dr: How I use hakyll. Abbreviations, typography corrections, multi-language, use index.html, etc…

This website is done with Hakyll.

Hakyll can be considered as a minimal cms. But more generally it is a library helping file generation. We can view it as an advanced build system (like make).

From the user perspective I blog this way:

  1. I open an editor (vim in my case) and edit a markdown file. It looks like this
  1. I open a browser and reload time to time to see the change.
  2. Once I finished I’ve written a very minimal script which mainly do a git push. My blog is hosted on github.

Being short sighted one could reduce the role of Hakyll to:

create (resp. update) html file when I create (resp. change) a markdown file.

While it sounds easy, there are a lot of hidden details:

  • Add metadatas like keywords.
  • Create an archive page containing a list of all the posts.
  • Deal with static files.
  • Creating an rss feed.
  • Filter the content with some function.
  • Dealing with dependencies.

The work of Hakyll is to help you with these. But let’s start with the basic concepts.

The concepts and syntax

Overview

For each file you create, you have to provide:

  • a destination path
  • a list of content filters.

First, let’s start with the simplest case: static files (images, fonts, etc…). Generally, you have a source directory (here is the current directory) and a destination directory _site.

The Hakyll code is:

This program will copy static/foo.jpg to _site/static/foo.jpg. I concede this is a bit overkill for a simple cp. Now how to write a markdown file and generate an html one?

If you create a file posts/foo.md, it will create a file _site/posts/foo.html.

If the file posts/foo.md contains

the file _site/posts/foo.html, will contain

But horror! _site/posts/cthulhu.html is not a complete html file. It doesn’t have any header nor footer, etc… This is where you use templates. I simply add a new directive in the compile block.

Now if templates/posts.html contains:

our cthulhu.html contains (indentation added for readability):

See, it’s easy But we have a problem. How could we change the title or add keywords?

The solution is to use Contexts. For this, we first need to add some metadatas to our markdown1.

And modify slightly our template:

As Sir Robin said just before dying before the Bridge of Death:

“That’s EASY!”

Sir Robin, the Not-Quite-So-Brave-As-Sir-Lancelot

Real customization

Now that we understand the basic functionality. How to:

  • use SASS?
  • add keywords?
  • simplify url?
  • create an archive page?
  • create an rss feed?
  • filter the content?
  • add abbreviations support?
  • manage two languages?

Use SASS

That’s easy. Simply call the executable using unixFilter. Of course you’ll have to install SASS (gem install sass). And we also use compressCss to gain some space.

Add keywords

In order to help to reference your website on the web, it is nice to add some keywords as meta datas to your html page.

In order to add keywords, we could not directly use the markdown metadatas. Because, without any, there should be any meta tag in the html.

An easy answer is to create a Context that will contains the meta tag.

Then we pass this Context to the loadAndApplyTemplate function:

☞ Here are the imports I use for this tutorial.

Simplify url

What I mean is to use url of the form:

http://domain.name/post/title-of-the-post/

I prefer this than having to add file with .html extension. We have to change the default Hakyll route behavior. We create another function niceRoute.

Not too difficult. But! There might be a problem. What if there is a foo/index.html link instead of a clean foo/ in some content?

Very simple, we simply remove all /index.html to all our links.

And we apply this filter at the end of our compilation

Create an archive page

Creating an archive start to be difficult. There is an example in the default Hakyll example. Unfortunately, it assumes all posts prefix their name with a date like in 2013-03-20-My-New-Post.md.

I migrated from an older blog and didn’t want to change my url. Also I prefer not to use any filename convention. Therefore, I add the date information in the metadata published. And the solution is here:

Where templates/archive.html contains

And base.html is a standard template (simpler than post.html).

archiveCtx provide a context containing an html representation of a list of posts in the metadata named posts. It will be used in the templates/archive.html file with $posts$.

postList returns an html representation of a list of posts given an Item sort function. The representation will apply a minimal template on all posts. Then it concatenate all the results. The template is post-item.html:

Here is how it is done:

createdFirst sort a list of item and put it inside Compiler context. We need to be in the Compiler context to access metadatas.

It wasn’t so easy. But it works pretty well.

Create an rss feed

To create an rss feed, we have to:

  • select only the lasts posts.
  • generate partially rendered posts (no css, js, etc…)

We could then render the posts twice. One for html rendering and another time for rss. Remark we need to generate the rss version to create the html one.

One of the great feature of Hakyll is to be able to save snapshots. Here is how:

Now for each post there is a snapshot named “content” associated. The snapshots are created before applying a template and after applying pandoc. Furthermore feed don’t need a source markdown file. Then we create a new file from no one. Instead of using match, we use create:

The feedConfiguration contains some general informations about the feed.

Great idea certainly steal from nanoc (my previous blog engine)!

Filter the content

As I just said, nanoc was my preceding blog engine. It is written in Ruby and as Hakyll, it is quite awesome. And one thing Ruby does more naturally than Haskell is regular expressions. I had a lot of filters in nanoc. I lost some because I don’t use them much. But I wanted to keep some. Generally, filtering the content is just a way to apply to the body a function of type String -> String.

Also we generally want prefilters (to filter the markdown) and postfilters (to filter the html after the pandoc compilation).

Here is how I do it:

Where

Now we have a simple way to filter the content. Let’s augment the markdown ability.

Add abbreviations support

Comparing to LaTeX, a very annoying markdown limitation is the lack of abbreviations.

Fortunately we can filter our content. And here is the filter I use:

It will search for all string starting by ‘%’ and it will search in the Map if there is a corresponding abbreviation. If there is one, we replace the content. Otherwise we do nothing.

Do you really believe I type

each time I write LaTeX?

Manage two languages

Generally I write my post in English and French. And this is more difficult than it appears. For example, I need to filter the language in order to get the right list of posts. I also use some words in the templates and I want them to be translated.

First I create a Map containing all translations.

Then I create a context for all key:

Conclusion

The full code is here. And except from the main file, I use literate Haskell. This way the code should be easier to understand.

If you want to know why I switched from nanoc:

My preceding nanoc website was a bit too messy. So much in fact, that the dependency system recompiled the entire website for any change.

So I had to do something about it. I had two choices:

  1. Correct my old code (in Ruby)
  2. Duplicate the core functionalities with Hakyll (in Haskell)

I added too much functionalities in my nanoc system. Starting from scratch (almost) remove efficiently a lot of unused crap.

So far I am very happy with the switch. A complete build is about 4x faster. I didn’t broke the dependency system this time. As soon as I modify and save the markdown source, I can reload the page in the browser.

I removed a lot of feature thought. Some of them will be difficult to achieve with Hakyll. A typical example:

In nanoc I could take a file like this as source:

And it will create a file foo.hs which could then be downloaded.


  1. We could also add the metadatas in an external file (foo.md.metadata).