grgr.me

latest commits to https://grgr.me/
Log | Files | Refs

commit 8629e87b9be9418a8dac838ba2a7b083fd7668c5
parent ff2415f34821cba95bc7cb3bedc4b7befac2ff2f
Author: corndog <cauchyn@firemail.cc>
Date:   Tue, 23 Oct 2018 20:54:02 -0700

Show time-to-read

Notify readers of how much content to expect, as
we all become more mindful of how we spend our
precious time.

I implement a lua filter for pandoc here.
The documentation is rough and there's not a lot
of prior art to study, but I'm impressed by its
speed and how powerful pandoc's ASTs are. Notice
that I have processing happening between pandoc,
lua filters, a youtube script and golang
templates. It's probably not too hard to convert
the youtube embed script to a lua filter.

Diffstat:
config | 2+-
in/me/index.md | 2+-
site.tmpl | 3+--
wordcount.lua | 40++++++++++++++++++++++++++++++++++++++++
4 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/config b/config @@ -7,7 +7,7 @@ URL = https://grgr.me/ *.md: config inner-template - external pandoc --to html + external pandoc --to html --lua-filter wordcount.lua external python3 youtube-embed.py template page ext .html diff --git a/in/me/index.md b/in/me/index.md @@ -32,7 +32,7 @@ description: Some things I like. # Just a few of my favorite things -<small>*Last modified: {{ template "date" .ModTime }}*</small> +<small>[*Last modified: {{ template "date" .ModTime }}*] [*\$timetoread\$*]</small> Maybe it was 2010 when I read a view on identity claiming people are the collection of their answers to *I love...*[^where]. For a while I kept a short list of links to refer to diff --git a/site.tmpl b/site.tmpl @@ -28,8 +28,7 @@ {{ end }} {{ define "date" }} -<time datetime="{{ .Format "2006-01-02" }}">{{ .Format "02 Jan 2006" }}</time> -{{end}} +<time datetime="{{ .Format "2006-01-02" }}">{{ .Format "02 Jan 2006" }}</time>{{end}} {{ define "page" }} {{ template "header" . }} diff --git a/wordcount.lua b/wordcount.lua @@ -0,0 +1,40 @@ +-- adapted from https://pandoc.org/lua-filters.html#counting-words-in-a-document +words = 0 +minutes = 0 +wpm = 150 + +wordcount = { + Str = function(el) + if el.text:match("%P") then + words = words + 1 + end + end, + + Code = function(el) + _,n = el.text:gsub("%S+", "") + words = words + n + end, + + CodeBlock = function(el) + _,n = el.text:gsub("%S+", "") + words = words + n + end +} + +function count(el) + pandoc.walk_block(pandoc.Div(el.blocks), wordcount) + minutes = math.ceil(words / wpm) +end + +function replace(el) + tooltip = words .. " words at a speed of " .. wpm .. " wpm." + tooltip = pandoc.Attr("", {}, {{"title", tooltip}}) + time_to_read = "~" .. minutes .. " minutes of text.*" + if el.text == "$timetoread$" then + return pandoc.Span(time_to_read, tooltip) + else + return el + end +end + +return {{Pandoc = count}, {Str = replace}}