Given markup inside an HTML document that looks like this
<h3>test</h3>
<p>test</p>
<hr/>
<h3>test2</h3>
<p>test2</p>
<hr/>
I'd like to to produce this
<div>
<h3>test</h3>
<p>test</p>
</div>
<div>
<h3>test2</h3>
<p>test2</p>
</div>
What's the most elegant way to do with with Nokogiri?
Here's an answer that uses Ruby 1.9.2's
Enumerable#chunk
to split the children into sections and also exercises Nokogiri'sNodeSet
class:This is how I'd go about it:
Edit: Reworked answer to be a bit cleaner.
Edit2: Small rewrite to shorten by two lines