module ReadingTime

Public Instance Methods

count_words(html) click to toggle source
# File lib/liquid_reading_time.rb, line 14
def count_words(html)
        words(html).length
end
reading_time(html) click to toggle source
# File lib/liquid_reading_time.rb, line 18
def reading_time(html)
        (count_words(html) / 270.0).ceil
end

Private Instance Methods

text_nodes(root) click to toggle source
# File lib/liquid_reading_time.rb, line 24
def text_nodes(root)
        ignored_tags = %w[ area audio canvas code embed footer form img
                map math nav object pre script svg table track video ]

        texts = []
        root.children.each { |node|
                if node.text?
                        texts << node.text
                elsif not ignored_tags.include? node.name
                        texts.concat text_nodes node
                end
        }
        texts
end
words(html) click to toggle source
# File lib/liquid_reading_time.rb, line 39
def words(html)
        fragment = Nokogiri::HTML.fragment html
        text_nodes(fragment).map { |text| text.scan(/[\p{L}\p{M}'‘’]+/) }.flatten
end