Class: Anchors::Middleware

Inherits:
Rack::Plastic
  • Object
show all
Defined in:
lib/anchors.rb

Constant Summary collapse

CSS_SELECTOR =
"body"
SEPERATOR =
"_"
true

Instance Method Summary collapse

Instance Method Details

#change_nokogiri_doc(doc) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/anchors.rb', line 10

def change_nokogiri_doc(doc)
  # Throw this CSS class into the HTML page where you
  # want to have linkable anchor headers.
  doc.css(css).each do |node|
    node.css("h1,h2,h3,h4,h5,h6").each do |h|
      id = dom_id(h)
      h["id"] = id
      if link?
        a = Nokogiri::XML::Node.new "a", doc
        a["href"] = "##{h["id"]}"
        a.add_child h.children
        h.add_child a
      end
    end
  end
  doc
end