module SmartExcerpt
Constants
- VERSION
Public Class Methods
included(base)
click to toggle source
# File lib/smart_excerpt.rb, line 14 def self.included(base) base.send(:extend, ClassMethods) end
Public Instance Methods
smart_truncate(obj, base_field, excerpt_field, words)
click to toggle source
# File lib/smart_excerpt.rb, line 19 def smart_truncate(obj, base_field, excerpt_field, words) trust_multiplier = 1.2 if words.is_a?(Hash) && words[:trust_multiplier] trust_multiplier = words[:trust_multiplier] end if obj.send(excerpt_field).blank? tx = obj.send(base_field) else tx = obj.send(excerpt_field) if words.is_a?(Fixnum) words *= trust_multiplier elsif words.is_a?(Hash) if words[:trust_excerpts] words = {words: Float::INFINITY} else words = Hash[words.map {|k, v| [k, v.is_a?(Fixnum) ? (v * trust_multiplier).to_i : v] }] end end end if words.is_a?(Numeric) options = {words: words.to_i} elsif words.is_a?(Hash) options = words else raise 'bad parameter for get_excerpt' end if tx.blank? '' else # kill headers and newlines unless options[:keep_headers] tx = tx.gsub(/<h\d[^>]*?>(.*?)<\/h\d>/mi, '') end unless options[:keep_newlines] tx = tx.gsub("\n", ' ').gsub("\r", '').gsub("\t", '').strip end tx = @@he.decode(tx) unless options[:keep_html] tx = @@h.strip_tags(tx) end @@h.smart_truncate(tx, options) end end