Module: HpricotTruncator::NodeWithChildren

Defined in:
lib/hpricot-util.rb

Instance Method Summary collapse

Instance Method Details

#truncate(words, truncate_string) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hpricot-util.rb', line 47

def truncate(words, truncate_string)
  return self if inner_text.mb_chars.split.size <= words
  truncated_node = dup
  truncated_node.name = name
  truncated_node.raw_attributes = raw_attributes
  truncated_node.children = []
  each_child do |node|
    break if words <= 0
    node_length = node.inner_text.mb_chars.split.size
    truncated_node.children << node.truncate(words, truncate_string)
    words -= node_length
  end
  truncated_node
end