# By Henrik Nyh 2007-03-28. # Based on http://vemod.net/code/hpricot_goodies/hpricot_text_gsub.rb. # Licensed under the same terms as Ruby. require "rubygems" require "hpricot" module HpricotTextTransform module NodeWithChildrenExtension def text_transform!(options={}, &block) return if defined?(name) and Array(options[:except]).include?(name.to_sym) children.each { |c| c.text_transform!(options, &block) } end end module TextNodeExtension def text_transform!(options={}, &block) content.replace yield(content) end end module BogusETagExtension def text_transform!(options={}, &block) end end end Hpricot::Doc.send(:include, HpricotTextTransform::NodeWithChildrenExtension) Hpricot::Elem.send(:include, HpricotTextTransform::NodeWithChildrenExtension) Hpricot::BogusETag.send(:include, HpricotTextTransform::BogusETagExtension) Hpricot::Text.send(:include, HpricotTextTransform::TextNodeExtension) if __FILE__ == $0 require "test/unit" class HpricotTextTransformTest < Test::Unit::TestCase def assert_hpricot_transform(expected, input, options={}, &block) doc = Hpricot(input) doc.text_transform!(options, &block) assert_equal(expected, doc.to_s) end def test_with_gsub input = 'xxx' expected = 'yyy' assert_hpricot_transform(expected, input, {}) { |text| text.gsub("x", "y") } end def test_with_reverse input = 'hello world from ruby' expected = 'olleh morf dlrow ybur' assert_hpricot_transform(expected, input, {}) { |text| text.reverse } end def test_with_reverse_exclude_one_tag input = 'hello world from ruby' expected = 'olleh morf dlrow ruby' assert_hpricot_transform(expected, input, {:except => :code}) { |text| text.reverse } end def test_with_reverse_exclude_multiple_tags input = 'hello world from ruby' expected = 'hello morf dlrow ruby' assert_hpricot_transform(expected, input, {:except => [:a, :code]}) { |text| text.reverse } end def test_with_reverse_exclude_nested_tag input = 'hello world from
ruby
' expected = 'olleh morf dlrow
ruby
' assert_hpricot_transform(expected, input, {:except => :code}) { |text| text.reverse } end end end