Permalink
Browse files

escape hakyll variables

  • Loading branch information...
blaenk committed Oct 11, 2018
1 parent 4688b65 commit 3a6d21f43304afcbbc847f8b7cd12c50f43d78b0
Showing with 2 additions and 2 deletions.
  1. +2 −2 content/posts/the-switch-to-hakyll.md
@@ -79,7 +79,7 @@ This states that the compilation process for any given post is as follows:

A post is routed using the `nicePostRoute` function which is largely borrowed from [Yann Esposito](http://yannesposito.com/Scratch/en/blog/Hakyll-setup/). It simply routes a <span class="path">posts/this-post.markdown</span> to <span class="path">posts/this-post/index.html</span> so that the post can be viewed at <span class="path">posts/this-post/</span>.

An interesting thing to note is that when templates are applied, they are supplied a [`Context`](http://hackage.haskell.org/packages/archive/hakyll/latest/doc/html/Hakyll-Web-Template-Context.html). A [`Context`](http://hackage.haskell.org/packages/archive/hakyll/latest/doc/html/Hakyll-Web-Template-Context.html) is simply a [Monoid](http://en.wikipedia.org/wiki/Monoid) that encapsulates a key (i.e. `String` identifier for the field) and an [`Item`](http://hackage.haskell.org/packages/archive/hakyll/latest/doc/html/Hakyll-Core-Item.html). During application of the template, if a field of the form `$key$` is encountered, the supplied `Context` is searched for an appropriate handler (i.e. one with the same key). If one is found, the item is passed to that `Context`'s handler and the result is substituted into the template.
An interesting thing to note is that when templates are applied, they are supplied a [`Context`](http://hackage.haskell.org/packages/archive/hakyll/latest/doc/html/Hakyll-Web-Template-Context.html). A [`Context`](http://hackage.haskell.org/packages/archive/hakyll/latest/doc/html/Hakyll-Web-Template-Context.html) is simply a [Monoid](http://en.wikipedia.org/wiki/Monoid) that encapsulates a key (i.e. `String` identifier for the field) and an [`Item`](http://hackage.haskell.org/packages/archive/hakyll/latest/doc/html/Hakyll-Core-Item.html). During application of the template, if a field of the form `\$key\$` is encountered, the supplied `Context` is searched for an appropriate handler (i.e. one with the same key). If one is found, the item is passed to that `Context`'s handler and the result is substituted into the template.

In the above `Rule` for posts, I pass a pre-crafted post `Context`, `postCtx`, and [`mappend`](http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend) to it a special tags context, `tagsCtx` which encapsulates tags information for that post.

@@ -130,7 +130,7 @@ In a [previous post](/posts/commit-tag-for-jekyll/) I talked about a liquid tag

One thing I forgot to update the previous post about was that I ended up switching from using the Rugged git-bindings for Ruby to just using straight up commands and reading their output. The reason for doing this was that, while everything worked perfectly fine on Linux, Rugged had problems building on Windows. It turned out that taking this approach ended up being simpler and had the added benefit of decreasing my dependencies.

The equivalent of a liquid tag in Jekyll would be a field, expressed as a `Context`. For this reason I created the `gitTag` function that takes a desired key, such as `git`, which would be used as `$git$` in templates, and returns a `Context` which returns the `String` of formatted HTML. One problem was that to do this I had to use `IO`, so I needed some way to escape the `Compiler` Monad. It turned out that Hakyll already had a function for something like this called `unsafeCompiler`, which it uses for `UnixFilter` for example.
The equivalent of a liquid tag in Jekyll would be a field, expressed as a `Context`. For this reason I created the `gitTag` function that takes a desired key, such as `git`, which would be used as `\$git\$` in templates, and returns a `Context` which returns the `String` of formatted HTML. One problem was that to do this I had to use `IO`, so I needed some way to escape the `Compiler` Monad. It turned out that Hakyll already had a function for something like this called `unsafeCompiler`, which it uses for `UnixFilter` for example.

Here's what `gitTag` looks like:

0 comments on commit 3a6d21f

Please sign in to comment.