Sha256: f80e1ab1f8dc78dd599ad6bf01d08a40b36d715a8485e97fb4dc097a4e855d80

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

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

          uniq = (headers[name] > 0) ? "-#{headers[name]}" : ''
          headers[name] += 1
          if header_content = node.children.first
            header_content.add_previous_sibling(%Q{<a name="#{name}#{uniq}" class="anchor" href="##{name}#{uniq}"><span class="mini-icon mini-icon-link"></span></a>})
          end
        end
        doc
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
geothird-html-pipeline-0.0.12 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.12 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.11 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.10 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.8 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.7 lib/html/pipeline/toc_filter.rb
html-pipeline-no-charlock-0.0.6 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.6 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.5 lib/html/pipeline/toc_filter.rb
html-pipeline-0.0.4 lib/html/pipeline/toc_filter.rb