Class: HTML_Truncator

Inherits:
Object
  • Object
show all
Defined in:
lib/html_truncator.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :ellipsis => "", :length_in_chars => false }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ellipsable_tagsObject

Returns the value of attribute ellipsable_tags.



20
21
22
# File 'lib/html_truncator.rb', line 20

def ellipsable_tags
  @ellipsable_tags
end

Class Method Details

.truncate(text, max, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/html_truncator.rb', line 10

def self.truncate(text, max, opts={})
  return truncate(text, max, :ellipsis => opts) if String === opts
  opts = DEFAULT_OPTIONS.merge(opts)
  doc = Nokogiri::HTML::DocumentFragment.parse(text)
  str, _, opts = doc.truncate(max, opts)
  eval "class <<str; def html_truncated?; #{opts[:was_truncated]} end end"
  str
end