Skip to content

Instantly share code, notes, and snippets.

@TheAshwanik
Last active December 11, 2015 09:18
Show Gist options
  • Save TheAshwanik/4578668 to your computer and use it in GitHub Desktop.
Save TheAshwanik/4578668 to your computer and use it in GitHub Desktop.
category_cloud.rb updated for tag_cloud also.
module Jekyll
class CategoryCumulusCloud < Liquid::Tag
def initialize(tag_name, markup, tokens)
.
.
.
super
end
def render(context)
lists = {}
max, min = 1, 1
config = context.registers[:site].config
if @tag_name == 'tag_cloud'
cloud_dir = config['tag_dir']
cloud = context.registers[:site].tags
else
cloud_dir = config['category_dir']
cloud = context.registers[:site].categories
end
cloud_dir = config['url'] + config['root'] + cloud_dir + '/'
#categories = context.registers[:site].categories
cloud.keys.sort_by{ |str| str.downcase }.each do |item|
count = cloud[item].count
lists[item] = count
max = count if count > max
end
.
.
.
.
lists.each do | item, counter |
url = cloud_dir + item.gsub(/_|\P{Word}/u, '-').gsub(/-{2,}/u, '-').downcase
style = "font-size: #{10 + (40 * Float(counter)/max)}%"
tagcloud << "<a href='#{url}' style='#{style}'>#{item}"
tagcloud << "</a> "
end
.
.
.
.
end
end
end
Liquid::Template.register_tag('category_cloud', Jekyll::CategoryCumulusCloud)
Liquid::Template.register_tag('tag_cloud', Jekyll::CategoryCumulusCloud)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment