How to split an HTML document using Nokogiri?

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.

+3
source share
1 answer

I just needed to do something like this. I split the large HTML file into “chapters”, where the chapter starts with a tag <h1>.

I also wanted to keep the title of the chapters in a hash and ignore everything until the first tag <h1>.

Here is the code:

full_book = Nokogiri::HTML(File.read('full-book.html'))
@chapters = full_book.xpath('//body').children.inject([]) do |chapters_hash, child|
  if child.name == 'h1'
    title = child.inner_text
    chapters_hash << { :title => title, :contents => ''}
  end

  next chapters_hash if chapters_hash.empty?
  chapters_hash.last[:contents] << child.to_xhtml
  chapters_hash
end
+4

Source: https://habr.com/ru/post/1759688/


All Articles