Recently, I've migrated my blog to Nanoc. I think there's too much hacking involved if you want to extend Jekyll. Or perhaps I didn't just read enough documentation about Jekyll. But anyway, I've already made the switch. What's done is done.
The first thing I did was to convert my markdown files. This wasn't really a problem since both Jekyll and Nanoc almost have the same format for the markdown files. Only difference I think would be the kind
and created_at
attributes so to be able to use the Blogging helper for Nanoc. Since I am trying to learn Ruby, I decided to create a script to make these changes on the files. This is what the script does:
- Retrieve the files with the extension .md on a specified directory.
- Split up the YAML attributes and the actual content.
- Create new YAML attributes based on the old one but add the
created_at
,kind
, andexcerpt
(not using this one at the moment) attributes. - Replace
{% highlight language %}...{% endhighlight %}
with~~~ language...~~~
. This is so I could use fenced code blocks. - Delete the current file.
- Write the new contents to a new file deleting the date from the beginning of the file name.
I then created a new helper. The helper contains an implementation of Redcarpet's renderer. I needed the code blocks to generate the tags <pre><code class="language-[language]">...</code></pre>
in order for Nanoc's ColorizeSyntax to pass it over to pygmentize
. This is what I have in my Rules
file in order for Nanoc to use the renderer.
filter :redcarpet,
:options => { :fenced_code_blocks => true },
:renderer => MyHelper::CustomRenderer
I didn't use kramdown which is the default markdown parser for Nanoc since I don't know how to do it. In my helper, I also had the publish_date
method which displays a better format of the created_at
attribute.
As for the layouts, it seems Nanoc's layouts are almost the same as Jekyll's but instead of using Liquid, it uses ERB. I don't really know what ERB is but I think it's just like putting <?php print $foo ?>
in your template files e.g. <%= foo %>
. For the index file, I had a list of all articles with their 'excerpts'. Nanoc has the Text helper to generate excerpts. It isn't that good though. To generate an excerpt, I used this snippet:
excerptize(strip_html(item.compiled_content), { :length => 600 })
For stylesheets, I still used Compass. There is an existing documentation on the integration of Compass with Nanoc. I don't really know what are the advantages of having a :sass
filter rather than just editing the config.rb
file and running compass watch
inside your project directory.
For the Tag items, I used this and this for the tags helper (it's the same as the latter though). Here's a breakdown of how the TagItems
module works:
# Make sure that the tags collection doesn't contain duplicate tags.
require 'set'
tags = Set.new
# Add the tags for the current item to the set if the 'tags' attribute is not
# empty.
items.each do |item|
item[:tags].each { |t| tags.add(t) } unless item[:tags].nil?
end
# Append new 'tag' item on items with the attribute 'tag'.
tags.each do |tag|
@items << Nanoc::Item.new('', { :tag => tag, :title => "Articles on “#{tag}”" }, "/tags/#{tag}")
end
In my Rules
file, I then have this:
preprocess do
create_tag_items
end
This creates new items dynamically during compilation. Once the items are in place, you can use the compile
rule to select a layout for tag items.
For the Related Articles, I have a method for this at MyHelper
. This one is based on this gist. It's kinda funny though since I was planning to display random articles for each tags tagged on the current article displayed but since this is a static site, the random articles are only generated on compilation. And this generation happens every compilation. xD I might remove this next time as this will cause problems when there are too many items.
That's it for now. I'm still planning to create an image gallery, portfolio, about page, rss feed and probably a contact page. And also add disqus. And probably a way to create new blog posts other than creating the files by hand.
References: