Class: HTML::Pipeline::TableOfContentsFilter
- Defined in:
- lib/html/pipeline/toc_filter.rb
Overview
HTML filter that adds a ‘name’ attribute to all headers in a document, so they can be accessed from a table of contents
Context options:
:toc_minimal_length (required) - Only add the table of contents to text with this number of characters
:toc_header (required) - Introduce the table of contents with this header
Instance Attribute Summary
Attributes inherited from Filter
Instance Method Summary collapse
Methods inherited from Filter
#base_url, call, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, to_document, to_html
Constructor Details
This class inherits a constructor from HTML::Pipeline::Filter
Instance Method Details
#call ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/html/pipeline/toc_filter.rb', line 13 def call headers = Hash.new 0 was = 1 toc = "" doc.css('h2, h3, h4, h5, h6').each do |node| level = node.name.scan(/\d/).first.to_i name = node.text.downcase name.gsub!(/[^\p{Word}\- ]/u, '') # remove punctuation name.gsub!(' ', '-') # replace spaces with dash uniq = (headers[name] > 0) ? "-#{headers[name]}" : '' headers[name] += 1 node['id'] = "#{name}#{uniq}" if was < level while was < level toc << "<ul>\n<li>" was += 1 end else toc << "</li>\n" while was > level toc << "</ul></li>\n" was -= 1 end toc << "<li>" end toc << "<a href=\"##{name}#{uniq}\">#{node.inner_html}</a>" end length = 0 doc.traverse {|node| length += node.text.length if node.text? } return doc unless length >= context[:toc_minimal_length] while was > 1 toc << "</li>\n</ul>\n" was -= 1 end toc.sub!('<ul>', '<ul class="toc">') unless headers.empty? first_child = doc.child first_child.add_previous_sibling context[:toc_header] first_child.add_previous_sibling toc end doc end |
#validate ⇒ Object
61 62 63 |
# File 'lib/html/pipeline/toc_filter.rb', line 61 def validate needs :toc_minimal_length, :toc_header end |