diff options
author | Achilleas Pipinellis <axil@gitlab.com> | 2020-02-04 12:14:01 +0300 |
---|---|---|
committer | Achilleas Pipinellis <axil@gitlab.com> | 2020-02-05 14:42:58 +0300 |
commit | 5d393a5e959d8cf64428932601bb44d71afbabd0 (patch) | |
tree | fe61a004659662c49428c88676e0b6a10313f83f /lib/helpers | |
parent | da3a140922edae93a31136e74d12ea554e30c005 (diff) |
Add estimated reading time
This adds the helper for calculating the reading time
of each document and shows it at the bottom of the page.
Diffstat (limited to 'lib/helpers')
-rw-r--r-- | lib/helpers/reading_time.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/helpers/reading_time.rb b/lib/helpers/reading_time.rb new file mode 100644 index 00000000..1f96dcf5 --- /dev/null +++ b/lib/helpers/reading_time.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +# +# https://www.perpetual-beta.org/weblog/estimated-reading-time-in-nanoc.html +# +# Count the number of words in the document and, assuming the average +# reading speed by words-per-minute, calculate the time taken in +# minutes to read the given number of words. If there are any seconds +# in the estimated reading time, then increment the minutes by one. +# +module Nanoc::Helpers + module ReadingTimeHelper + # + # Words per minute + # http://en.wikipedia.org/wiki/Words_per_minute#Reading_and_comprehension + # + # This is a rough estimate and given that the algorithm to find the words + # is very simple, it's best to overcalculate and increase the WPM. + # Otherwise, we end up with big reading time numbers. + # + WPM = 230 + + def reading_time(words) + minutes = (words / WPM).ceil + seconds = (words % WPM / (WPM / 60)).floor + + if seconds > 0 + minutes += 1 + end + + (minutes <= 1 ? 'about a minute' : '~' + minutes.to_s + ' minutes') + end + end +end |