class Hpricot::Elements

def wrap(str = nil, &blk)

This code wraps every link on the page inside a +div.link+ and a +div.link_inner+ nest.

wrap(%{})
doc.search("a[@href]").

deepest spot inside the first element.
If more than one element is found in the string, Hpricot locates the
Wraps each element in the list inside the element created by HTML +str+.
def wrap(str = nil, &blk)
  each do |x|
    wrap = x.make(str, &blk)
    nest = wrap.detect { |w| w.respond_to? :children }
    unless nest
      raise "No wrapping element found."
    end
    x.parent.replace_child(x, wrap)
    nest = nest.children.first until nest.empty?
    nest.html([x])
  end
end