Commit dbc66b8d authored by Evaryont's avatar Evaryont

An all tags cloud page

parent d135e33c
......@@ -11,6 +11,7 @@ $color_palette = ['#00c853', '#00e676', '#1b5e20', '#2e7d32', '#33691e',
require_relative 'lib/helpers/resume.rb'
require_relative 'lib/helpers/blog.rb'
require_relative 'lib/helpers/reviews.rb'
require_relative 'lib/tag_cloud.rb'
# Dumb hacks are dumb. Since the blog posts don't include '.html.' in the file
# name (nor do I want them to), Middleman defaults to showing them as plain
......
# Originally written by @timurvafin in 2013
# https://github.com/timurvafin/timurv.ru/blob/master/lib/tag_cloud.rb
class TagCloud
cattr_accessor :default_options
self.default_options = {
size_min: 70,
size_max: 170,
precision: 0,
unit: '%',
threshold: 1
}
attr_reader :options
def initialize(options = {})
@options = default_options.update(options)
end
def render(tags)
tags_weight(tags_count(tags)).each do |tag_pair|
tag, weight = tag_pair
size = sprintf("%.#{options[:precision]}f", calculate_size(weight))
yield(tag, size, options[:unit])
end
end
private
def tags_count(tags)
tags.map do |tag, articles|
[tag, articles.count] if articles.count >= options[:threshold]
end.compact
end
def tags_weight(count)
# get the minimum, and maximum tag count
min, max = count.map(&:last).minmax
# map: [[tag, tag count]] -> [[tag, tag weight]]
count.map do |tag, count|
# logarithmic distribution
weight = (Math.log(count) - Math.log(min))/(Math.log(max) - Math.log(min))
[tag, weight]
end
end
def calculate_size(weight)
options[:size_min] + ((options[:size_max] - options[:size_min]) * weight).to_f
end
end
---
layout: page
title: All tags in my blog
---
<h2>All tags</h2>
<% TagCloud.new.render(blog.tags) do |tag, size, unit| %>
<%= link_to(tag, tag_path(tag), style: "font-size: #{size}#{unit}") %>
<% end %>
......@@ -3,7 +3,7 @@ layout: page
title: "Tags"
---
<p>There are <strong><%= page_articles.length %></strong> posts tagged with <%= tagname %>.</p>
<p>There are <strong><%= page_articles.length %></strong> posts tagged with <%= tagname %>. <small>(See <a href="/blog/tags.html">all tags</a>.)</small></p>
<% page_articles.each do |article| %>
<article class='archive-article'>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment