Sha256: d6c96aa334f4f54875b3051ccb17227173deaaa3f76a180fa2bc7f05591fbeba
Contents?: true
Size: 1.01 KB
Versions: 1
Compression:
Stored size: 1.01 KB
Contents
require 'article_readingtime/version' require 'nokogiri' WORDS_PER_MINUTE = 275 IMAGE_MAX = 15 IMAGE_MIN = 3 IMAGE_STEP = 1 module ArticleReadingtime def self.estimate_html(html, options = {}) wpm = options[:wpm] || WORDS_PER_MINUTE images_options = default_image_options(options[:images] || {}) doc = Nokogiri::HTML(html) count_text(doc, wpm) + count_images(doc, images_options) end def self.default_image_options(options) { max: options[:max] || IMAGE_MAX, min: options[:min] || IMAGE_MIN, step: options[:step] || IMAGE_STEP } end def self.count_text(doc, wpm) words = doc.inner_text.split(' ') (words.length / wpm.to_f * 60).round end def self.count_images(doc, options) images_count = doc.css('img').length images_count.times.reduce(0) do |total, i| total + if options[:max] - i * options[:step] > options[:min] options[:max] - i * options[:step] else options[:min] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
article_readingtime-1.0.0 | lib/article_readingtime.rb |