module Bergamasco::Summarize

Public Class Methods

summary(text, options={}) click to toggle source

based on from github.com/middleman/middleman-blog/blob/master/lib/middleman-blog/blog_article.rb

# File lib/bergamasco/summarize.rb, line 4
def self.summary(text, options={})
  separator = options[:separator] || "READMORE"

  if !options[:length] && text.include?(separator)
    truncate_at_separator(text, separator)
  else
    max_length = options[:length] || 250
    ellipsis = options[:ellipsis] || "..."
    truncate_at_length(text, max_length, ellipsis)
  end
end
summary_from_html(html, options={}) click to toggle source
# File lib/bergamasco/summarize.rb, line 16
def self.summary_from_html(html, options={})
  summary = Bergamasco::Sanitize.sanitize(html, options).squish
  summary(summary, options)
end
truncate_at_length(text, max_length, ellipsis = "...") click to toggle source

from github.com/middleman/middleman-blog/blob/master/lib/middleman-blog/truncate_html.rb

# File lib/bergamasco/summarize.rb, line 29
def self.truncate_at_length(text, max_length, ellipsis = "...")
  ellipsis_length = ellipsis.length
  text = text.encode('UTF-8') if text.respond_to?(:encode)
  doc = Nokogiri::HTML::DocumentFragment.parse text
  content_length = doc.inner_text.length
  actual_length = max_length - ellipsis_length
  if content_length > actual_length
    doc.truncate(actual_length, ellipsis).inner_html
  else
    text
  end
end
truncate_at_separator(text, separator) click to toggle source

from github.com/middleman/middleman-blog/blob/master/lib/middleman-blog/truncate_html.rb

# File lib/bergamasco/summarize.rb, line 22
def self.truncate_at_separator(text, separator)
  text = text.encode('UTF-8') if text.respond_to?(:encode)
  doc = Nokogiri::HTML::DocumentFragment.parse text.split(separator).first
  doc.inner_html
end