Henshin

Advanced Hackery

Although Henshin is set up for blogging it is massively customisable. But to do so requires some knowledge of how it works.

Templates are passed the objects whole. So to add variables available to templates is as easy as adding a method to the correct class.

File

File::Abstract

These are files which don’t physically exist on the file system. You can create these for files which are generated purely in code, for instance, indexes. They have access to the @site variable which holds the Site object being used.

File::Physical

These are physical files, corresponding to a file at the given path. They have access to the @site and @path variables.

Site

The main unit is the Site. An instance of Site is created when run that then reads the files and builds them.

An Example: Recipes

An example is always useful, here I’ll be showing a simple recipe site. Recipes are stored in the recipes/ folder, similar to how posts are stored in the posts/ folder. Here is an example,

# in recipes/shortcrust-pastry.md
---
title: Shortcrust Pastry
ingredients:
- 4oz plain flour
- pinch of salt
- 2oz butter, cubed
- 2-3 tbsp cold water
---

Put flour and salt in large bowl, add butter.

Use fingertips to rub butter into flour until 
you have a mixture resembling large breadcrumbs.

Using a knife stir in just enough water to bind 
the dough.

...

And here is the code,

# in init.rb

# Recipes are files, but should always have a title and 
# list of ingredients, and should use the 'recipe' template.
module Recipe
  extend File::Attributes

  requires :title, :ingredients
  template 'recipe'
end

# We should include the Recipe module to all files in `/recipes`
File.apply %{(^|/)recipes/}, Recipe

# The recipe site needs to have a method to list all recipes
class RecipeSite < Site
  # Read all files in the `/recipes` folder
  def recipes
    read :all, 'recipes'
  end
  
  # Then add the list of recipes to the main list of files
  files :recipes
end

# Use our RecipeSite
use RecipeSite