Hakyll setup

Main image

tlpl: Comment j’utilise hakyll. Abréviations, corrections typographiques, multi-language, utilisation d’index.html, etc…

Ce site web est fait avec Hakyll.

Hakyll peut être vu comme un cms minimaliste. D’une façon plus générale, il s’agit d’une bibliothèque qui facilite la création automatique de fichiers.

D’un point de vue utilisateur voici comment j’écris mes articles :

  1. J’ouvre un éditeur de texte (vim dans mon cas). J’édite un fichier markdow qui ressemble à ça :
  1. J’ouvre mon navigateur et je rafraichis de temps en temps pour voir les changements.
  2. Une fois satisfait, je lance un script minimal qui fait grosso modo un simple git push. Mon blog est hébergé sur github.

A ne pas y regarder de trop près, on peut réduire le rôle d’Hakyll à :

Créer (resp. mettre à jour) un fichier html lorsque je crée (resp. modifie) un fichier markdown.

Bien que cela semble facile, il y a de nombreux détails cachés :

  • Ajouter des métadatas comme des mots clés
  • Créer un page archive qui contient la liste de tous les articles
  • Gérer les fichier statiques
  • Créer un flux rss
  • Filtrer le contenu
  • Gérer les dépendances

Le travail d’Hakyll est de vous aider avec tout ça. Commençons par expliquer les concepts basiques.

Les concepts et la syntaxe


Pour chaque fichier que vous créer, il faut fournir :

  • un chemin de destination
  • une liste de filtres du contenu

Commençons par le cas le plus simple ; les fichiers statiques (images, fontes, etc…) Généralement, vous avec un répertoire source (ici le répertoire courant) et une répertoire destination _site.

Le code Hakyll est :

Ce programme va copier static/foo.jpg dans _site/static/foo.jpg. C’est un peu lourd pour un simple cp. Maintenant comment faire pour transformer automatiquement un fichier markdown dans le bon html?

Si vous créez un fichier posts/toto.md, cela créera un fichier _site/posts/toto.html.

Si le fichier posts/foo.md contient

le fichier _site/posts/foo.html, contiendra

Mais horreur ! _site/posts/cthulhu.html n’est pas un html complet. Il ne possède ni header, ni footer, etc… C’est ici que nous utilisons des templates. J’ajoute une nouvelle directive dans le bloc “compile”.

Maintenant si templates/posts.html contient:

Maintenant notre ctuhlhu.html contient

C’est facile. Mais il reste un problème à résoudre. Comment pouvons-nous changer le titre ? Ou par exemple, ajouter des mots clés ?

La solution est d’utiliser les Contexts. Pour cela, nous devrons ajouter des metadonnées à notre markdown1.

Et modifier légèrement notre template :

Super facile!

La suite de l’article est en Anglais. Je la traduirai volontier si suffisamment de personnes me le demande gentillement.

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?


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:


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:


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:


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. Nous pouvons aussi ajouter ces métadonnées dans un fichier externe (toto.md.metadata).


comments powered by Disqus