Class: Nokogiri::XML::Node

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

Instance Method Summary collapse

Instance Method Details

#ellipsable?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/html_truncator.rb', line 64

def ellipsable?
  HTML_Truncator.ellipsable_tags.include? name
end

#inner_truncate(max, opts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/html_truncator.rb', line 46

def inner_truncate(max, opts)
  inner, remaining = "", max
  self.children.each do |node|
    txt, nb, opts = node.truncate(remaining, opts)
    remaining -= nb
    inner += txt
    next if remaining >= 0
    if ellipsable?
      r = %r/[\s#{HTML_Truncator.punctuation_chars.join}]+$/
      inner = inner.sub(r, '') + opts[:ellipsis]
      opts[:ellipsis] = ""
      opts[:was_truncated] = true
    end
    break
  end
  [inner, remaining, opts]
end

#self_closing?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/html_truncator.rb', line 68

def self_closing?
  HTML_Truncator.self_closing_tags.include? name
end

#truncate(max, opts) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/html_truncator.rb', line 35

def truncate(max, opts)
  return ["", 1, opts] if max == 0 && !ellipsable?
  inner, remaining, opts = inner_truncate(max, opts)
  if inner.empty?
    return [self_closing? ? to_html : "", max - remaining, opts]
  end
  children.remove
  add_child Nokogiri::HTML::DocumentFragment.parse(inner)
  [to_html(:indent => 0), max - remaining, opts]
end