class Verku::TOC::HTML
Attributes
toc[R]
Return the table of contents in hash format.
Public Class Methods
generate(content)
click to toggle source
Traverse every title normalizing its content as a permalink.
# File lib/verku/toc/html.rb, line 36 def self.generate(content) content = normalize(content) listener = new listener.content = content Stream.new(content, listener).parse listener end
normalize(content)
click to toggle source
Traverse every title and add a id
attribute. Return the modified content.
# File lib/verku/toc/html.rb, line 16 def self.normalize(content) counter = {} html = Nokogiri::HTML.parse(content) html.search("h1, h2, h3, h4, h5, h6").each do |tag| title = tag.inner_text permalink = title.to_permalink counter[permalink] ||= 0 counter[permalink] += 1 permalink = "#{permalink}-#{counter[permalink]}" if counter[permalink] > 1 tag.set_attribute("id", permalink) end html.css("body").to_xhtml.gsub(/<body>(.*?)<\/body>/m, "\\1") end
Public Instance Methods
to_hash()
click to toggle source
Return a hash with all normalized attributes.
# File lib/verku/toc/html.rb, line 59 def to_hash { :content => content, :html => to_html, :toc => toc } end
to_html()
click to toggle source
Return the table of contents in HTML
format.
# File lib/verku/toc/html.rb, line 69 def to_html String.new.tap do |html| toc.each do |options| html << %[<div class="level#{options[:level]} #{options[:permalink]}"><a href="##{options[:permalink]}"><span>#{CGI.escape_html(options[:text])}</span></a></div>] end end end