From: Chris Gehlker Date: 2006-08-12T11:23:14+09:00 Subject: Re: A simple Hpricot text setter On Aug 11, 2006, at 5:20 PM, why the lucky stiff wrote: > On Fri, Aug 11, 2006 at 03:19:13AM +0900, Chris Gehlker wrote: >> If anyone is trying to use Hpricot to clean up the actual content of >> a site while leaving the markup alone, theymight find the following >> tiny method useful: >> >> class Hpricot::Text >> # Adds a simple Hpricot method to change >> # the text embedded in an HTML document >> # >> # Example of use: >> # body.traverse_text do |text| >> # text_out = text.to_s >> # manupulate text_out >> # text.set(text_out) >> # end >> def set(string) >> @content = string >> self.raw_string = string >> end >> end > > You can also use Elements#inner_html= and Element#inner_html= for > this. > > (body/:a).inner_html = "New Link Text" > > Also: set, html, remove, append, prepend, before, after, and wrap, > which all > work just like their JQuery cousins.[1] Thanks for responding, why: and thanks very much for Hpricot. I'm a long way from completely understanding Hpricot but I did try to use inner_html in what I though was the correct way. Here is a little sample program: require 'rubygems' require_gem 'hpricot' doc = Hpricot(open('TestFile.html')) body = doc.search('body') body.each {|elmnt| elmnt.inner_html} body.inner_html (body/:a).inner_html = "New Link Text" puts doc The output is: testHpricot.rb:6: undefined method `inner_html' for # (NoMethodError) from testHpricot.rb:6:in `each' from testHpricot.rb:6 If I comment out the body.each... line I get: testHpricot.rb:7: undefined method `inner_html' for # (NoMethodError) If I comment out that line, I get: testHpricot.rb:8: undefined method `inner_html=' for []:Array (NoMethodError) What may be related is that the file text.rb is at: /usr/local/lib/ruby/gems/1.8/gems/hpricot-0.3/lib/hpricot/text.rb but it is not actually being required anywhere in Hpricot. When i tried to require it manually, i found that it was requiring files that gem didn't give me. This is all in Hpricot 0.3. Thanks again for both your time and Hpricot. -- Seven Deadly Sins? I thought it was a to-do list!