website-theme/layouts/_default/stats.html

118 lines
4.2 KiB
HTML

{{ define "main" }}
<!-- Adapted from Luke Harris https://www.lkhrs.com/ -->
{{ partial "header.html" . }}
<main class="container">
<article class="row justify-content-center">
<div class="col-md-8">
{{ .Content }}
{{ $tags := .Site.Taxonomies.tags }}
{{ $posts := where site.RegularPages "Type" "blog" }}
{{ $postCount := len $posts }}
{{ $scratch := newScratch }}
{{ $scratch.Set "longCount" 0 }}
{{ $scratch.Set "shortCount" 9999999999 }}
{{ range $posts }}
<!-- Sum words for average calculation later -->
{{ $scratch.Add "wordcount" .WordCount }}
<!-- Find longest post -->
{{ if ge .WordCount ($scratch.Get "longCount") }}
{{ $scratch.Set "longestPost" . }}
{{ $scratch.Set "longCount" .WordCount }}
{{ end }}
<!-- Find shortest post -->
{{ if le .WordCount ($scratch.Get "shortCount") }}
{{ $scratch.Set "shortestPost" . }}
{{ $scratch.Set "shortCount" .WordCount }}
{{ end }}
{{ end }}
{{ $wordCount := $scratch.Get "wordcount" }}
{{ $avgPostLength := div $wordCount $postCount }}
<table class="table">
<tr>
<td width="40%">Total Posts Published</td>
<td width="60%">{{ $postCount | lang.NumFmt 0 }}</td>
</tr>
<tr>
<td>Total Words Written</td>
<td>{{ $wordCount | lang.NumFmt 0 }}</td>
</tr>
<tr>
<td>Average Words per Post</td>
<td>{{ $avgPostLength | lang.NumFmt 0 }}</td>
</tr>
<tr>
<td>Number of Topics</td>
<td>{{ len $tags | lang.NumFmt 0 }}</td>
</tr>
<tr>
<td>Longest Post Published</td>
<td>
{{ with $scratch.Get "longestPost" }}
<a href="{{ .Permalink }}">{{ .Title }}</a> with {{ .WordCount }} words
{{ end }}
</td>
</tr>
<tr>
<td>Shortest Post Published</td>
<td>
{{ with $scratch.Get "shortestPost" }}
<a href="{{ .Permalink }}">{{ .Title }}</a> with {{ .WordCount }} words
{{ end }}
</td>
</tr>
</table>
{{ $grouped := $posts.GroupByDate "2006" }}
<h2 class="post-stats-hd">Posts Per Year</h2>
<div style="width: 100%;height: 300px;margin: 30px auto">
<canvas id="yearchart"></canvas>
<noscript>
<table class="table">
<tr>
<th width="30%">Year</td>
<th width="70%">Number of Posts</td>
</tr>
{{ range $grouped.Reverse }}
{{ $yearPostCount := len (where .Pages "Type" "blog") }}
<tr>
<td>{{ .Key }}</td>
<td>{{ $yearPostCount | lang.NumFmt 0 }}</td>
</tr>
{{ $scratch.Add "yearKeys" "'" }}
{{ $scratch.Add "yearKeys" .Key }}
{{ $scratch.Add "yearKeys" "'," }}
{{ $scratch.Add "yearCount" (string $yearPostCount) }}
{{ $scratch.Add "yearCount" "," }}
{{ end }}
</table>
</noscript>
</div>
{{ $chartPre := "{type: 'line',data: {labels: [" }}
{{ $chartYearKeys := trim ($scratch.Get "yearKeys") ","}}
{{ $chartYearCount := trim ($scratch.Get "yearCount") ","}}
{{ $chartMid := "],datasets: [{label: 'Posts Published',data: [" }}
{{ $chartPost := "],backgroundColor: 'rgb(54, 128, 56)',borderColor: 'rgb(54, 128, 56)',borderWidth: 3}]},options: {maintainAspectRatio: false, animation: false, scales: {y: {ticks: {beginAtZero: true}}}}}"
}}
{{ $chartData := printf "%s" $chartPost | printf "%s%s" $chartYearCount | printf "%s%s" $chartMid | printf "%s%s" $chartYearKeys | printf "%s%s" $chartPre | printf "%s" }}
<script src="https://cdn.jsdelivr.net/npm/chart.js/dist/chart.min.js" type="text/javascript"></script>
<script type="text/javascript">
var ctx = document.getElementById('yearchart').getContext('2d');
var options = {{ $chartData | safeJS }};
new Chart(ctx, options);
</script>
</div>
</article>
</main>
{{ partial "footer.html" . }}
{{ end }}