-
Notifications
You must be signed in to change notification settings - Fork 1
/
weightedtags.html
45 lines (41 loc) · 1.91 KB
/
weightedtags.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{{- /*
Gets an array with weighted tags.
Based on tagadelic_build_weighted_tags() in tagadelic Drupal 4.7 module:
https://www.drupal.org/project/tagadelic
https://git.drupalcode.org/project/tagadelic/-/blob/4.7.x-2.0/tagadelic.module
*/ -}}
{{- .c.Scratch.Delete "weightedTags" -}}
{{- .c.Scratch.Add "weightedTags" slice -}}
{{- .c.Scratch.Delete "logCountTags" -}}
{{- .c.Scratch.Add "logCountTags" slice -}}
{{- .c.Scratch.Delete "minLogCount" -}}
{{- .c.Scratch.Set "minLogCount" 1e9 -}}
{{- .c.Scratch.Delete "maxLogCount" -}}
{{- .c.Scratch.Set "maxLogCount" -1e9 -}}
{{- if .tags -}}
{{- /*
Find minimum and maximum log-count.
By our MatheMagician Steven Wittens aka UnConeD.
*/ -}}
{{- range .tags.Alphabetical -}}
{{- $logCount := math.Log .Count -}}
{{- $.c.Scratch.Add "logCountTags" (slice (dict "Title" .Page.Title "RelPermalink" .Page.RelPermalink "Count" .Count "LogCount" $logCount)) -}}
{{- if lt $logCount ($.c.Scratch.Get "minLogCount") -}}
{{- $.c.Scratch.Set "minLogCount" $logCount -}}
{{- end -}}
{{- if gt $logCount ($.c.Scratch.Get "maxLogCount") -}}
{{- $.c.Scratch.Set "maxLogCount" $logCount -}}
{{- end -}}
{{- end -}}
{{- /*
Note: we need to ensure the range is slightly too large to make sure even
the largest element is rounded down.
*/ -}}
{{- $maxMinusMin := sub ($.c.Scratch.Get "maxLogCount") ($.c.Scratch.Get "minLogCount") -}}
{{- .c.Scratch.Delete "weightedTagsRange" -}}
{{- .c.Scratch.Set "weightedTagsRange" (mul (cond (gt $maxMinusMin 0.1) $maxMinusMin 0.1) 1.0001) -}}
{{- range (.c.Scratch.Get "logCountTags") -}}
{{- $weight := add 1 (math.Floor (mul $.steps (div (sub .LogCount ($.c.Scratch.Get "minLogCount")) ($.c.Scratch.Get "weightedTagsRange")))) -}}
{{- $.c.Scratch.Add "weightedTags" (slice (dict "Title" .Title "RelPermalink" .RelPermalink "Count" .Count "LogCount" .LogCount "Weight" $weight)) -}}
{{- end -}}
{{- end -}}