# A simple Hpricot text setter

**URL:** <https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331 "2006-08-15T14:14:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 15, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/1 "2006-08-15T14:14:44Z")

</div>

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

## The trick is to set both @content in Hpricot::Text and @raw\_string in it’s parent.

The folly of mistaking a paradox for a discovery, a metaphor for a  
proof, a torrent of verbiage for a spring of capital truths, and  
oneself for an oracle, is inborn in us.  
-Paul Valery, poet and philosopher (1871-1945)

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/2 "2006-08-15T14:17:35Z")

</div>

On Fri, Aug 11, 2006 at 03:19:13AM +0900, Chris G. wrote:

> # 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]

Thankyou for using Hpricot, it helps the all horses’ hearts when you do.

\_why

[1] [jQuery API Documentation](http://jquery.com/docs/base/)

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/3 "2006-08-15T14:19:58Z")

</div>

On Sat, Aug 12, 2006 at 11:23:14AM +0900, Chris G. wrote:

> 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.

Okay, yeah, you’ll need the latest Hpricot (0.4.43):

gem install hpricot --source [code.whytheluckystiff.net](http://code.whytheluckystiff.net)

Also, don’t forget to remove `require_gem 'hpricot'` and use, instead,  
`require 'hpricot'`.

\_why

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/4 "2006-08-15T14:17:40Z")

</div>

On Aug 11, 2006, at 5:20 PM, why the lucky stiff wrote:

> > # body.traverse\_text do |text|
> 
> 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 #<Hpricot::Elem: 0x7546bc> (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  
#Hpricot::Elements:0x753d48 (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.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 16, 2006, 6:07am UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/5 "2006-08-16T06:07:30Z")

</div>

On Aug 14, 2006, at 9:29 AM, why the lucky stiff wrote:

> Okay, yeah, you’ll need the latest Hpricot (0.4.43):
> 
> gem install hpricot --source [code.whytheluckystiff.net](http://code.whytheluckystiff.net)
> 
> Also, don’t forget to remove `require_gem 'hpricot'` and use, instead,  
> `require 'hpricot'`.
> 
> \_why

You seem to be making great progress with Hpricot, committing changes  
every day.

Yep, ‘require\_gem’ no longer works. Just using ‘require’ seems better.

I don’t know that I communicated my idea behind adding a set method  
for Hpricot::Text. There are times when one wants to scan an  
potentially change everything that’s _not_ markup. The markup should  
be left unchanged or modified only in trivial ways such as changing  
the order of attribute declarations.

Hpricott::Traverse#traverse\_text is great for finding as the stuff  
that’s _not_ markup, the pcdata, in an HTML file. I just added a  
method to change that data.

You suggested using inner\_html= but the only way I can see that  
working is to parse the tree looking for those elements which only  
have Hpricot::Text children and then using inner\_html= on them. But  
that would involve essentially recreating  
Hpricott::Traverse#traverse\_text to find such elements although the  
common code could mostly be factored out.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 17, 2006, 12:01am UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/6 "2006-08-17T00:01:07Z")

</div>

On Aug 16, 2006, at 12:25 PM, why the lucky stiff wrote:

> Okay, I get it. I guess I need to get //div[contains(text(), ‘…’)]  
> working.

Works for me!

> Be assured, traverse\_text will stick around.

Thanks why!

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [August 16, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/a-simple-hpricot-text-setter/102331/7 "2006-08-16T21:27:49Z")

</div>

On Wed, Aug 16, 2006 at 01:04:46PM +0900, Chris G. wrote:

> Hpricott::Traverse#traverse\_text is great for finding as the stuff  
> that’s _not_ markup, the pcdata, in an HTML file. I just added a  
> method to change that data.

Okay, I get it. I guess I need to get //div[contains(text(), ‘…’)]  
working. Be assured, traverse\_text will stick around.

\_why
