Right now, I'm splitting the HTML document into small pieces like this: (simplified regular expression - skipping the contents of the title tag and the closing tag)
document.at('body').inner_html.split(/<\s*h[2-6][^>]*>/i).collect do |fragment|
Nokogiri::HTML(fragment)
end
Is there an easier way to do this splitting?
The document is very simple, only headings, paragraphs and formatted text. For instance:
<body>
<h1>Main</h1>
<h2>Sub 1</h2>
<p>Text</p>
-----
<h2>Sub 2</h2>
<p>Text</p>
-----
<h3>Sub 2.1</h3>
<p>Text</p>
-----
<h3>Sub 2.2</h3>
<p>Text</p>
</body>
For this sample, I need to get four parts.
source
share