Skip to content

Commit

Permalink
feat: Enabled word count statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
chollinger93 committed Jun 20, 2023
1 parent d26b4ac commit 55dab94
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.gitignore.io/api/hugo,linux,macos
# Edit at https://www.gitignore.io/?templates=hugo,linux,macos

Expand Down Expand Up @@ -57,4 +56,5 @@ Temporary Items

# End of https://www.gitignore.io/api/hugo,linux,macos
.hugo_build.lock
exampleSite/public
exampleSite/public
*.xlsx
7 changes: 6 additions & 1 deletion exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ copyright = "© Copyright notice"
# Github base url
github = "https://github.com/chollinger93/ink-free"

# For statistics, we can compare the total word count against a book of your choice
statisticsEnabled = true
referenceBookWords = 11500.0
referenceBook = "The Metamorphosis"
referenceAuthor = "Franz Kafka"

# Nav.
[[menu.main]]
Expand All @@ -62,7 +67,7 @@ url = "/about"
weight = 3

[[menu.main]]
name = "Tags"
name = "Tags & Stats"
url = "/tags"
weight = 4

Expand Down
19 changes: 14 additions & 5 deletions layouts/_default/terms.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<!DOCTYPE html>
<html>
{{ partial "header.html" . }}


<body>
<div class="container wrapper tags">
{{ partial "head.html" . }}

{{ partial "header.html" . }}

{{ if .Site.Params.statisticsEnabled }}
{{ partial "statistics.html" . }}
{{ end }}

<h1 class="page-title">{{ .Name }}</h1>

{{ $biggest := 1 }}
{{ $smallest := 1 }}
{{ $max := 3 }}
Expand All @@ -16,14 +22,17 @@ <h1 class="page-title">{{ .Name }}</h1>

{{ $data := .Data }}
<div class="tag-cloud">
{{ range $key, $value := .Data.Terms.ByCount }}
{{ range $key, $value := .Data.Terms.ByCount }}
{{ $size := (add (mul (div $value.Count $biggest) (sub $max $min)) $min) }}
{{ $size := (cond (eq $biggest $smallest) $min $size) }}
<a style="font-size: {{ $size }}rem;" href="{{ $.Site.LanguagePrefix | absURL }}{{ $data.Plural }}/{{ $value.Name | urlize }}/">{{ $value.Name }}<sup>{{ $value.Count }}</sup></a>
{{ end }}
<a style="font-size: {{ $size }}rem;"
href="{{ $.Site.LanguagePrefix | absURL }}{{ $data.Plural }}/{{ $value.Name | urlize }}/">{{ $value.Name
}}<sup>{{ $value.Count }}</sup></a>
{{ end }}
</div>
</div>

{{ partial "footer.html" . }}
</body>

</html>
1 change: 0 additions & 1 deletion layouts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<body>
<div class="container wrapper">
{{ partial "head.html" . }}

<div class="recent-posts section">
<div class="posts">
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
Expand Down
29 changes: 29 additions & 0 deletions layouts/partials/statistics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{ $scratch := newScratch }}
{{ range .Site.RegularPages }}
{{ $wordCountNoCode := replaceRE `(?s)<div class="highlight">.*?</div>` "" .Content |
countwords }}
{{ $scratch.Add "wordcount" $wordCountNoCode }}
{{ end }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}

<h1>Statistics</h1>
<p><b>{{ $scratch.Get "wordcount" | lang.NumFmt 0 }}</b> words published over {{len $pages }} articles since
{{ range last 1 $pages }}
{{ .Date.Month }} {{ .Date.Year }}.
{{ end }}
</p>
{{ $readingMins := div ($scratch.Get "wordcount") .Site.Params.wpm }}
<p>Reading all that should take you about {{ $readingMins }} minutes
{{ if gt $readingMins 60 }}
(or ~{{div $readingMins 60.0 | lang.NumFmt 1 }} hrs).
{{else}}
.
{{end}}
</p>
<p>
For reference, that's about {{div ($scratch.Get "wordcount") (.Site.Params.referenceBookWords | default
95356.0 ) |
lang.NumFmt 1 }}x "{{
.Site.Params.referenceBook |
default "The Hobbit"}}" by {{ .Site.Params.referenceAuthor | default "J. R. R. Tolkien"}}.
</p>

0 comments on commit 55dab94

Please sign in to comment.