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.
Methods
Constants
| Heading | = | Struct.new(:depth, :anchor, :title) unless const_defined?(:Heading) |
Public Instance methods
[ show source ]
# File lib/rote/filters/redcloth.rb, line 50
50: def handler text, *args
51: # determine structure of content and insert anchors where necessary
52: headings = []
53:
54: text = text.gsub /^(\s*h(\d))(.*?)(\.(.*))$/ do
55: target = $~.dup
56:
57: if target[3] =~ /#([^#]+)\)/
58: anchor = $1
59: result = target.to_s
60: else
61: anchor = headings.length
62: result = "#{target[1]}#{target[3]}(##{anchor})#{target[4]}"
63: end
64:
65: headings << Heading.new( target[2].to_i, anchor, target[5] )
66: result
67: end
68:
69: # add table of contents at top of text
70: toc = headings.map do |h|
71: %{#{'*' * h.depth} "#{h.title}":##{h.anchor}}
72: end.join("\n")
73:
74: text.insert 0, "\n\n\n"
75: text.insert 0, toc
76:
77: super text, *args
78: end