Books and Jekyll rating stars

When a friend found out that I am “blogging”, she assumed I would be posting personal details and share my views about this and that. Aside from my very personal configurations however, that is obviously not the case. I’d like to change that a tiny little bit by linking to a page with a list of books – see also the header bar – that I read in the past. This will not only give you a limited idea about myself but also helps me keeping track of what I read.

To keep this post a bit technical, here is the bit of code that uses Font Awesome to display the Jekyll star ratings next to some of the books (props to Lawrence Woodman about the initial idea):

module Jekyll
  class RatingStars < Liquid::Tag

    def initialize(tag_name, text, tokens)
      super
      @stars = text
    end

    def render(context)
      rating = Float(@stars)
      num_full_stars = rating.floor()
      num_half_stars = rating - num_full_stars >= 0.5 ? 1 : 0
      num_empty_stars = 5 - num_full_stars - num_half_stars

      html = String.new
      html += '<span class="rating">'

      num_full_stars.times do
        html += '<i class="fa fa-star"></i>'
      end

      num_half_stars.times do
        html += '<i class="fa fa-star-half-o"></i>'
      end

      num_empty_stars.times do
        html += '<i class="fa fa-star-o"></i>'
      end

      html += '</span>'
      html
    end
  end
end

Liquid::Template.register_tag('rating', Jekyll::RatingStars)

Discussion