Class: HTML_Truncator

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

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ellipsable_tagsObject

Returns the value of attribute ellipsable_tags.



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

def ellipsable_tags
  @ellipsable_tags
end

.punctuation_charsObject

Returns the value of attribute punctuation_chars.



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

def punctuation_chars
  @punctuation_chars
end

.self_closing_tagsObject

Returns the value of attribute self_closing_tags.



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

def self_closing_tags
  @self_closing_tags
end

Class Method Details

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



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

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