published on in HowTo Ruby
tags: blogging book plugin regex ruby

Promoted Product Box with Octopress

Sometimes I try to share a book, a bicycle or mobile app. I tired to write the code for that. I use Octopress and I love it. Octopress built over Jekyll (what I used before) with Ruby.

module Jekyll

class ProductPromoBox < Liquid::Block
  Title = /^Title: (.*)$/
  Author = /^Author: (.*)$/
  Source = /^Source: (.*)$/
  Url = /^Url: (.*)$/
  Price = /^Price: (.*)$/
  PromoPrice = /^PromoPrice: (.*)$/
  Image = /^Image: (.*)$/
  PromoCode = /^PromoCode: (.*)$/
  Description = /^Description: (.*)$/

  def initialize(tag_name, markup, tokens)
    @by = nil
    @title = nil
    @source = nil
    @url = nil
    @price = nil
    @promoPrice = nil
    @image = nil
    @promoCode = nil
    @description = nil

    tokens[0].split(/\n/).each do |line|
      line = line.strip
      next if (line.length < 1)

      if line =~ Title
        @title = $1
      elsif line =~ Author
        @by = $1
      elsif line =~ Source
        @source = $1
      elsif line =~ Url
        @url = $1
      elsif line =~ Price
        @price = $1
      elsif line =~ PromoPrice
        @promoPrice = $1
      elsif line =~ Image
        @image = $1
      elsif line =~ PromoCode
        @promoCode = $1
      elsif line =~ Description
        @description = $1
      end
    end

    super
  end

  def render(context)
    promo = "";
    promo += "<div class='product-title'>#{with_link(@title, @url)}</div>" unless @title.nil?
    promo += "<div class='product-source'>#{with_link(@source, @url)}</div>" unless @source.nil?
    promo += "<div class='product-author'>by <span>#{@by}</span></div>" unless @by.nil?
    if @promoPrice.nil?
      promo += "<div class='product-price'><span class='now'>#{@price}</span></div>" unless @price.nil?
    else
      promo += "<div class='product-price'>Old price: <span class='old'>#{@price}</span>; now: <span class='now'>#{@promoPrice}</span></div>" unless @price.nil?
    end
    promo += "<div class='product-promo-code'>Promotion code: <span>#{@promoCode}</span></div>" unless @promoCode.nil?
    promo += "<div class='product-description'>#{@description}</div>" unless @description.nil?
    promo = "<div class='product-info'>#{promo}</div>";
    promo = with_link("<img class='product-image' src='#{@image}' alt='#{@title}' title='#{@title}'>", @url, @title) + "#{promo}" unless @image.nil?

    return "<div class='product-box'>#{promo}</div>"
  end

  def with_link(content, url, title = nil)
    return content if url.nil?

    title = content if title.nil?
    title.gsub!(/'/, "&#39;")

    return "<a href='#{url}' title='#{title}'>#{content}</a>"
  end
end
end

Liquid::Template.register_tag('product_promo_box', Jekyll::ProductPromoBox)

Gist url

Just put this file into your plugins directory. So when you write a new article about recommended books for instance I put this into the post:

{% product_promo_box %}
  Title: Regular Expressions Cookbook, 2nd Edition
  Author: Jan Goyvaerts, Steven Levithan
  Image: http://akamaicovers.oreilly.com/images/0636920023630/lrg.jpg
  Price: $39.99
  PromoPrice: $19.99
  PromoCode: DEAL
  Source: O'Reilly Media
  Url: http://shop.oreilly.com/product/0636920023630.do
  Description: Detailed Solutions in Eight Programming Languages
{% endproduct_promo_box %}

How it looks like at the end?

Feel free to use or modify ;) If you got some nice idea for that please share with us.