Module: Reacco::Filters::Literate

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

Instance Method Summary collapse

Instance Method Details

#move_pre(html) ⇒ Object

Moves pre’s after headers.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reacco/filters/literate.rb', line 5

def move_pre(html)
  anchor   = nil
  position = nil

  html.css('body>*').each_cons(2) { |(node, nxt)|
    # Once we find the <pre>, move it.
    if node.name == 'pre' && anchor && anchor != node && !(node['class'].to_s =~ /full/)
      node.after "<br class='post-pre'>"

      nxt['class']    = "#{nxt['class']} after-pre"  if nxt
      node['class']   = "#{node['class']} right"

      anchor.send position, node
      anchor = nil

    # If we find one of these, put the next <pre> after it.
    elsif %w(h1 h2 h3 h4 pre).include?(node.name)
      anchor   = node
      position = :add_next_sibling

    # If we find one of these, put the <pre> before it.
    elsif node['class'].to_s.include?('after-pre')
      anchor   = node
      position = :add_previous_sibling
    end
  }

  html
end