toc_filter.rb 1.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
module HTML
  class Pipeline
    # HTML filter that adds a 'name' attribute to all headers
    # in a document, so they can be accessed from a table of contents
    #
    # TODO: besides adding the name attribute, we should get around to
    # eventually generating the Table of Contents itself, with links
    # to each header
    class TableOfContentsFilter < Filter
      def call
        headers = Hash.new(0)
        doc.css('h1, h2, h3, h4, h5, h6').each do |node|
          name = node.text.downcase
          name.gsub!(/[^\w\- ]/, '') # remove punctuation
          name.gsub!(' ', '-') # replace spaces with dash
          name = EscapeUtils.escape_uri(name) # escape extended UTF-8 chars
Jon Rohan's avatar
Jon Rohan committed
17

18 19 20
          uniq = (headers[name] > 0) ? "-#{headers[name]}" : ''
          headers[name] += 1
          if header_content = node.children.first
Cameron McEfee's avatar
Cameron McEfee committed
21
            header_content.add_previous_sibling(%Q{<a name="#{name}#{uniq}" class="anchor" href="##{name}#{uniq}"><span class="octicon octicon-link"></span></a>})
22
          end
Jon Rohan's avatar
Jon Rohan committed
23
        end
24
        doc
25
      end
26 27
    end
  end
Jon Rohan's avatar
Jon Rohan committed
28
end