Skip to content

Commit

Permalink
Group tags into buckets with statically assigned sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Nov 12, 2023
1 parent e9a4596 commit e5382f7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
14 changes: 14 additions & 0 deletions app/assets/stylesheets/publify.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
overflow: hidden;
}

.tag-sidebar-tag-67 { font-size: 0.67em; }
.tag-sidebar-tag-75 { font-size: 0.75em; }
.tag-sidebar-tag-83 { font-size: 0.83em; }
.tag-sidebar-tag-91 { font-size: 0.91em; }
.tag-sidebar-tag-100 { font-size: 1em; }
.tag-sidebar-tag-112 { font-size: 1.12em; }
.tag-sidebar-tag-125 { font-size: 1.25em; }
.tag-sidebar-tag-137 { font-size: 1.37em; }
.tag-sidebar-tag-150 { font-size: 1.50em; }
.tag-sidebar-tag-162 { font-size: 1.62em; }
.tag-sidebar-tag-175 { font-size: 1.75em; }
.tag-sidebar-tag-187 { font-size: 1.87em; }
.tag-sidebar-tag-200 { font-size: 2em; }

.hidden {
display: none;
}
28 changes: 25 additions & 3 deletions app/models/tag_sidebar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@ def sizes
average = total.to_f / @tags.size
@sizes = tags.reduce({}) do |h, tag|
size = tag.content_counter.to_f / average
h.merge tag => size.clamp(2.0 / 3.0, 2) * 100
h.merge tag => bucket(size)
end
end

def font_multiplier
80
BUCKETS = [
67,
75,
83,
91,
100,
112,
125,
137,
150,
162,
175,
187,
200
].freeze

private

def bucket(size)
base_size = size.clamp(2.0 / 3.0, 2) * 100
BUCKETS.each do |sz|
return sz if sz >= base_size
end
BUCKETS.last
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/tag_sidebar/_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="sidebar-body">
<p class="tag-sidebar-tag-cloud">
<% sidebar.tags.each do |tag| %>
<span style="font-size:<%= sidebar.sizes[tag] %>%"><%= link_to tag.display_name, tag_url(tag.name) %></span>
<span class="tag-sidebar-tag-<%= sidebar.sizes[tag] %>"><%= link_to tag.display_name, tag_url(tag.name) %></span>
<% end %>
</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion spec/models/tag_sidebar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
create(:article, keywords: "foo, quuz")

result = sidebar.sizes
expect(result.values.uniq).to contain_exactly (2.0 / 3.0 * 100), 200
expect(result.values.uniq).to contain_exactly 67, 200
end
end
end

0 comments on commit e5382f7

Please sign in to comment.