I’m trying to build a generic weighted tag cloud as in the final example at the bottom of https://24ways.org/2006/marking-up-a-tag-cloud
So far I have this CSS:
.tag-cloud li{display:inline}
.tag-cloud li span{position: absolute; left: -9999px; width: 90px}
.tag-cloud li.rare{font-size:1.0em}
.tag-cloud li.seldom{font-size:1.2em}
.tag-cloud li.occasional{font-size:1.4em}
.tag-cloud li.often{font-size:1.6em}
.tag-cloud li.frequent{font-size:1.8em}
.tag-cloud li.common{font-size:2.0em}
and this in the template:
{{ if not (eq (len .Site.Taxonomies.tags) 0) }}
{{ $.Scratch.SetInMap "weights" "0" "rare" }}
{{ $.Scratch.SetInMap "weights" "1" "seldom" }}
{{ $.Scratch.SetInMap "weights" "2" "occasional" }}
{{ $.Scratch.SetInMap "weights" "3" "often" }}
{{ $.Scratch.SetInMap "weights" "4" "frequent" }}
{{ $.Scratch.SetInMap "weights" "5" "common" }}
{{ $.Scratch.Set "total" 0 }}
{{ range $name, $items := .Site.Taxonomies.tags }}
{{ $.Scratch.Add "total" (len $items) }}
{{ end }}
<pre>
total = {{ $.Scratch.Get "total" }}
weights = {{ $.Scratch.GetSortedMapValues "weights" }}
third = {{ index ($.Scratch.GetSortedMapValues "weights") 2 }}
</pre>
<ol class="tag-cloud">
{{ range $name, $items := .Site.Taxonomies.tags }}
{{ if gt (len $items) 1 }}
{{ $.Scratch.Set "count" (print (len $items) " " (pluralize "article")) }}
{{ else }}
{{ $.Scratch.Set "count" "1 article" }}
{{ end }}
<li class="">
<a href="{{ $.Site.BaseURL }}tags/{{ $name | urlize | lower }}" title="{{ $name }}: {{ $.Scratch.Get "count" }}">{{ $name }}<span> ({{ $.Scratch.Get "count" }})</span></a>
</li>
{{ end }}
</ol>
In the <pre>
block it correctly prints what I expect but I’m having trouble working out how to map the count to a weight. I had a look at Drupal’s implementation but it uses min(), max(), log() and floor() functions which aren’t currently available in Hugo’s template functions.
So before I go making a pull request for something specific that I need to make this work, I thought I would ask
- Is there a better way of building a tag list with weights?
- Should I implement some function to take
.Site.Taxonomies.tags
(which returns a map of name to an array of items) and a variable number ofsteps
to return mapping of name to weight or is there something more generic I can build?