Module: Reacco::Filters::Hgroup

Included in:
Readme
Defined in:
lib/reacco/filters/hgroup.rb

Instance Method Summary collapse

Instance Method Details

#wrap_hgroup(html) ⇒ Object

Wraps the first headings into an <hgroup>.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/reacco/filters/hgroup.rb', line 5

def wrap_hgroup(html)
  nodes = Array.new
  html.css('body>*').each { |node|
    # Consume all headings.
    if %w(h1 h2 h3 h4 h5 h6).include?(node.name)
      nodes << node.to_s
      node.remove

    # Once the headings stop, dump them into an <hgroup>.
    # Ah, and eat an <hr> if there is one.
    else
      node.before("<hgroup class='header'>#{nodes.join('')}</hgroup>")  if nodes.any?
      node.remove  if node.name == 'hr'
      break
    end
  }

  html
end