diff options
-rw-r--r-- | layouts/default.html | 34 | ||||
-rw-r--r-- | lib/helpers/reading_time.rb | 34 | ||||
-rw-r--r-- | lib/helpers_.rb | 1 |
3 files changed, 54 insertions, 15 deletions
diff --git a/layouts/default.html b/layouts/default.html index 05fa7993..eebef397 100644 --- a/layouts/default.html +++ b/layouts/default.html @@ -43,29 +43,33 @@ <%= @item[:title] %><%= @item[:title_badge] %> </h1> <% end %> - <% if @item[:author] %> - <div class="article-metadata"> + <div class="article-metadata"> + <% if @item[:author] and @item[:author_gitlab] %> Article written by <a href="https://gitlab.com/<%= @item[:author_gitlab] %>" target="_blank"><%= @item[:author] %></a> - on <%= @item[:date] %> • - <% if @item[:last_updated] %> - Last updated: <%= @item[:last_updated] %> + <% end %> + <% if @item[:date] %> + on <%= @item[:date] %> • - <% end %> + <% end %> + <% if @item[:article_type] %> Type: <%= @item[:article_type] %> - • + <% end %> + <% if @item[:level] %> Level: <%= @item[:level] %> - </div> - <% end %> + • + <% end %> + <% if @item[:last_updated] %> + Last updated: <%= @item[:last_updated] %> + • + <% end %> + <% if @item[:reading_time] %> + <%= reading_time(item.raw_content.split.size) %> to read + <% end %> + </div> <div class="article-content js-article-content" role="main" itemscope itemprop="mainContentOfPage"> <%= yield %> </div> - <% if @item[:last_updated] %> - <hr> - <p class="last-updated"> - <em>Last updated <%= @item[:last_updated] %></em> - </p> - <% end %> <%= render '/feedback.*' %> </div> <div class="clear"></div> 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 diff --git a/lib/helpers_.rb b/lib/helpers_.rb index 90c184f6..a555e37d 100644 --- a/lib/helpers_.rb +++ b/lib/helpers_.rb @@ -5,3 +5,4 @@ include Nanoc::Helpers::ChildParentBetter include Nanoc::Helpers::EditOnGitLab include Nanoc::Helpers::VersionsDropdown include Nanoc::Helpers::IconsHelper +include Nanoc::Helpers::ReadingTimeHelper |