From: Gregory Seidman Date: 2006-12-25T23:22:15+09:00 Subject: Re: Hpricot ri and rdoc documentation On Mon, Dec 25, 2006 at 04:24:05PM +0900, bbiker wrote: [...] } Thank you for your offer of assistance Sure. } In simple term I need to extract text from a

element ... } yes I know that I can use "traverse_text" That works fine except when } there is no text at an expected location. [...] } An alternate way would be to simply insert arbritary text ('x ") after } the img element... no need to extract the basename. .. the s h d c } sequence is constant. } } This would let traverse_text yield "x 8 7 6 2", "x ", "x K 7 6 4", "x Q } 7 6 3 2" } A single character (x) would also work. } Actually, I prefer the alternate way...Occam's Razor You got it. } I know that I can do both using regexp. but I am trying to learn and } understand hpricot. } I understand that hpricot can modify (edit), remove, and add elements. } It is definitely stronger and faster. It also offers a more generic way } of processing html files. } } So in simple terms, the problem is how to insert text after an element. Here's some code that should help you on your way. You'll notice that I'm avoiding adding methods to Hpricot classes and instead pulling out the extra methods into modules and calling extend on the appropriate objects. Assuming you've loaded a document into the variable doc, doc.extend(Hpricot::InsertTextExtension).insert_text_after('x ', 'img') will make the changes you want: require 'rubygems' require 'hpricot' module Hpricot::HandyElementTraversal def next_sibling i = index i && pchildren[i+1] end def index(elem = nil) elem ? children.index(elem) : (pchildren && pchildren.index(self)) end def pchildren parent && parent.children end end module Hpricot::InsertTextExtension def insert_text_after(text, path) (self/path).each do |elem| elem.extend(Hpricot::HandyElementTraversal) sib = elem.next_sibling if Hpricot::Text === sib sib.content.insert(0, text) else elem.pchildren.insert(elem.index + 1, Hpricot::Text.new(text)) end end self end end --Greg