class Rote::Filters::RedCloth_WithToc
Redcloth filter that adds a table of contents at the top of the given text and sufficent hyperlinks to access the various headings in the text. This can be used instead of the standard TOC
filter to get TOC
capabilities during page (rather than layout) rendering.
Contributed by Suraj Kurapati.
Constants
- Heading
Public Instance Methods
handler(text, *args)
click to toggle source
Calls superclass method
Rote::Filters::RedCloth#handler
# File lib/rote/filters/redcloth.rb 48 def handler text, *args 49 # determine structure of content and insert anchors where necessary 50 headings = [] 51 52 text = text.gsub(/^(\s*h(\d))(.*?)(\.(.*))$/) do 53 target = $~.dup 54 55 if target[3] =~ /#([^#]+)\)/ 56 anchor = $1 57 result = target.to_s 58 else 59 anchor = headings.length 60 result = "#{target[1]}#{target[3]}(##{anchor})#{target[4]}" 61 end 62 63 headings << Heading.new( target[2].to_i, anchor, target[5] ) 64 result 65 end 66 67 # add table of contents at top of text 68 toc = headings.map do |h| 69 %{#{'*' * h.depth} "#{h.title}":##{h.anchor}} 70 end.join("\n") 71 72 text.insert 0, "\n\n\n" 73 text.insert 0, toc 74 75 super text, *args 76 end