Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enabled word count statistics #14

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>