3
votes

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.

1
Why are you using regex when already have a XML parser? - NullUserException
NullUserException, I don't know better way to do that yet, this is why I'm asking. - taro

1 Answers

5
votes

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:

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