Lego2012
7/23/2017 - 12:11 PM

Nice tag cloud with Hugo

Nice tag cloud with Hugo

<div>
  {{ if and .IsPage (and (ne .Params.toc false) .TableOfContents) }}
  <section class="panel panel-default">
    <div class="panel-heading"><b>Table of contents</b></div>
    <div style="padding: 5px 0px">
      {{ .TableOfContents }}
    </div>
  </section>
  {{ end }}

  <section class="panel panel-default">
    <div class="panel-heading">
    <b>Recent posts</b>
    <div style="float: right"><a href="{{ "/post/" | relLangURL }}">- full list -</a></div>
    </div>
    <div style="padding: 5px 0px">
    <div class="content">
      <ul>
      {{ range first 10 (where (where .Site.Pages "Type" "post") "IsPage" true) }}
        <li>
        <a href="{{ .Permalink }}">{{ .Title }}</a>
        </li>
      {{ end }}
      </ul>
    </div>
    </div>
  </section>

  <section class="panel panel-default">
    <div class="panel-heading">
    <b>Tags</b>
    <div style="float: right"><a href="{{ "/tags/" | relLangURL }}">- full list -</a></div>
    </div>
{{ if not (eq (len $.Site.Taxonomies.tags) 0) }}
    {{ $fontUnit := "rem" }}
    {{ $largestFontSize := 2.5 }}
    {{ $smallestFontSize := 1.0 }}
    {{ $max := len (index $.Site.Taxonomies.tags.ByCount 0).Pages }}
    {{ $min := len (index $.Site.Taxonomies.tags.ByCount.Reverse 0).Pages }}

    <div id="tag-cloud" style="padding: 5px 15px">
        {{ range $name, $taxonomy := $.Site.Taxonomies.tags }}
            {{ $count := len $taxonomy.Pages }}
            {{ $weigth := div (sub (math.Log $count) (math.Log $min)) (sub (math.Log $max) (math.Log $min)) }}
            {{ $currentFontSize := (add $smallestFontSize (mul (sub $largestFontSize $smallestFontSize) $weigth) ) }}
            <!--Current font size: {{$currentFontSize}}-->
            <a href="{{ "/tags/" | relLangURL }}{{ $name | urlize }}" style="font-size:{{$currentFontSize}}{{$fontUnit}}">{{ $name }}</a>
        {{ end }}
    </div>
{{ end }}
  </section>
</div>
{{ if not (eq (len $.Site.Taxonomies.tags) 0) }}
    {{ $fontUnit := "rem" }}
    {{ $largestFontSize := 2.0 }}
    {{ $largestFontSize := 2.5 }}
    {{ $smallestFontSize := 1.0 }}
    {{ $fontSpread := sub $largestFontSize $smallestFontSize }}
    {{ $max := len (index $.Site.Taxonomies.tags.ByCount 0).Pages }}
    {{ $min := len (index $.Site.Taxonomies.tags.ByCount.Reverse 0).Pages }}
    {{ $spread := sub $max $min }}
    {{ $fontStep := div $fontSpread $spread }}

    <div id="tag-cloud" style="padding: 5px 15px">
        {{ range $name, $taxonomy := $.Site.Taxonomies.tags }}
            {{ $currentTagCount := len $taxonomy.Pages }}
            {{ $currentFontSize := (add $smallestFontSize (mul (sub $currentTagCount $min) $fontStep) ) }}
            {{ $count := len $taxonomy.Pages }}
            {{ $weigth := div (sub (math.Log $count) (math.Log $min)) (sub (math.Log $max) (math.Log $min)) }}
            {{ $currentFontSize := (add $smallestFontSize (mul (sub $largestFontSize $smallestFontSize) $weigth) ) }}
            <!--Current font size: {{$currentFontSize}}-->
            <a href="{{ "/tags/" | relLangURL }}{{ $name | urlize }}" style="font-size:{{$currentFontSize}}{{$fontUnit}}">{{ $name }}</a>
        {{ end }}
    </div>
{{ end }}