Module: NokogiriTruncator::NodeWithChildren

Defined in:
lib/bergamasco/summarize.rb

Instance Method Summary collapse

Instance Method Details

#truncate(max_length, ellipsis) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bergamasco/summarize.rb', line 47

def truncate(max_length, ellipsis)
  return self if inner_text.length <= max_length
  truncated_node = self.dup
  truncated_node.children.remove

  self.children.each do |node|
    remaining_length = max_length - truncated_node.inner_text.length
    break if remaining_length <= 0
    truncated_node.add_child node.truncate(remaining_length, ellipsis)
  end
  truncated_node
end