Right now, I’m splitting the HTML document to small pieces like this:
(regular expression simplified – skipping header tag content and 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 perform that splitting?
The document is very simple, just headers, paragraphs and formatted text in it.
For example:
<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 that sample, I need to get four pieces.
I just had to do something similar. I split a large HTML file in to “chapters” where a chapter is started by an
<h1>tag.I also wanted to keep the title of the chapters in the hash and ignore everything before the first
<h1>tag.Here is the code: