From 099e081eb8257989d6f25ccc14323858048647d3 Mon Sep 17 00:00:00 2001 From: Lorin Werthen Date: Sun, 6 Nov 2016 17:45:30 +0100 Subject: [PATCH] Reading time \o/ --- Gemfile | 4 ++++ Gemfile.lock | 4 +++- layouts/blogpost.erb | 3 ++- lib/helpers/blog.rb | 16 ++++++++++++++++ lib/helpers_.rb | 1 + 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 lib/helpers/blog.rb diff --git a/Gemfile b/Gemfile index 68cc260..3500262 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,10 @@ gem 'therubyracer' # Autoprefixing for class gem 'autoprefixer-rails' +# Word counting gem (which takes special characters into account) +# for reading time +gem 'words_counted' + group :development do gem 'adsf' gem 'highline' diff --git a/Gemfile.lock b/Gemfile.lock index 452be76..791ddf4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,7 @@ GEM w3c_validators (1.2) json nokogiri + words_counted (1.0.2) PLATFORMS ruby @@ -97,6 +98,7 @@ DEPENDENCIES terminal-notifier-guard therubyracer w3c_validators + words_counted BUNDLED WITH - 1.13.3 + 1.13.6 diff --git a/layouts/blogpost.erb b/layouts/blogpost.erb index 9ad45b1..428f12d 100644 --- a/layouts/blogpost.erb +++ b/layouts/blogpost.erb @@ -15,7 +15,8 @@ <% end %>

- Created at <%= item[:created_at] %> + Geschreven op <%= item[:created_at] %>
+ Leestijd: <%= reading_time @item %>


diff --git a/lib/helpers/blog.rb b/lib/helpers/blog.rb new file mode 100644 index 0000000..7b7a768 --- /dev/null +++ b/lib/helpers/blog.rb @@ -0,0 +1,16 @@ +require 'words_counted' + +module BlogHelper + def reading_time(blogpost) + human_wpm = 200.0 + words = WordsCounted.count(blogpost.reps[:text].compiled_content).token_count + + minutes = (words / human_wpm).ceil + + if minutes == 1 + "#{minutes} minuut" + else + "#{minutes} minuten" + end + end +end diff --git a/lib/helpers_.rb b/lib/helpers_.rb index 90fdc28..e139277 100644 --- a/lib/helpers_.rb +++ b/lib/helpers_.rb @@ -14,3 +14,4 @@ include DataHelper include FontAwesomeHelper include ProjectsHelper include SearchHelper +include BlogHelper