Class: Rote::Filters::RedCloth_WithToc
- Inherits:
-
RedCloth
- Object
- TextFilter
- RedCloth
- Rote::Filters::RedCloth_WithToc
- Defined in:
- lib/rote/filters/redcloth.rb
Overview
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.
Defined Under Namespace
Classes: Heading
Instance Attribute Summary
Attributes inherited from TextFilter
Instance Method Summary collapse
Methods inherited from RedCloth
Methods inherited from TextFilter
Constructor Details
This class inherits a constructor from Rote::Filters::RedCloth
Instance Method Details
#handler(text, *args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rote/filters/redcloth.rb', line 48 def handler text, *args # determine structure of content and insert anchors where necessary headings = [] text = text.gsub(/^(\s*h(\d))(.*?)(\.(.*))$/) do target = $~.dup if target[3] =~ /#([^#]+)\)/ anchor = $1 result = target.to_s else anchor = headings.length result = "#{target[1]}#{target[3]}(##{anchor})#{target[4]}" end headings << Heading.new( target[2].to_i, anchor, target[5] ) result end # add table of contents at top of text toc = headings.map do |h| %{#{'*' * h.depth} "#{h.title}":##{h.anchor}} end.join("\n") text.insert 0, "\n\n\n" text.insert 0, toc super text, *args end |