Rails as a static site generator

Posted by Anton Katunin on 27 May 2016
Tags: code

Looking for static site generator? Look no more, use Rails!


There are many too many of static site generators. Previously I had this blog generated in Jekyll. I've used Octopress as an out of the box solution which was far from being ready to use. It took me many hours to learn the api, how to style, how to deploy, how to write posts, where to put them, where to put images and how to reference them.

After 6 months of not using it and coming back to blogging I've realised I don't remember how to use octopress and jekyll. So instead of learning them again, I've decided to take another approach and write my own blog engine.

New idea

The problem that most blogs in static site generators has is that post parsing logic is mixed with view and website structure. So there are few rules I had to follow with my approach:

  1. Focus on content and use Markdown

  2. Post and all images (and other assets) should be grouped together in a single place.

  3. Post should reference images locally as well, without complex paths. This means when writing post you don't need to remember how your blogging engine works.

    before /images/posts/cover.jpg
    after cover.jpg

  4. Use as much of plain markdown as possible

    before {% img center /images/posts/cover.jpg %}
    after ![](cover.jpg)

  5. Separate blogging logic from the way it will be presented (web)

  6. Keep it as a static site


The solution is to write blogging logic in ruby, add web interface through rails and convert to static website with wget command. Finally upload static website to S3 with s3_website tool.

I've tested many variations of wget commands and settled on this:

wget -r -l inf -k -nc -nH -p -E -e robots=off -P ./out http://localhost:3000

And here you go, you are reading static website generated from rails.

Read next:

Embedded widgets with Ember.js

at Melbourne ember.js meetup