Class: Rote::Filters::TOC
- Inherits:
-
Object
- Object
- Rote::Filters::TOC
- Defined in:
- lib/rote/filters/toc.rb
Overview
Page filter that supports easy construction of a Table Of Contents from your layout. This filter searches for tags matching the specified regular expression(s) (H tags by default), and stores them to be used for TOC generation in the layout. HTML Named-anchors are created based on the headings found.
Additional attributes for the A tags can be passed via the attrs
parameter.
Defined Under Namespace
Classes: Heading
Instance Attribute Summary collapse
-
#headings ⇒ Object
(also: #links)
readonly
Array of heading links with the heading title as the link text.
Instance Method Summary collapse
- #filter(text, page) ⇒ Object
-
#initialize(tags_re = /h\d+/, attrs = {}) ⇒ TOC
constructor
A new instance of TOC.
Constructor Details
#initialize(tags_re = /h\d+/, attrs = {}) ⇒ TOC
Returns a new instance of TOC.
47 48 49 50 51 |
# File 'lib/rote/filters/toc.rb', line 47 def initialize( = /h\d+/, attrs = {}) @tags_re = @attrs = attrs @headings = [] end |
Instance Attribute Details
#headings ⇒ Object (readonly) Also known as: links
Array of heading links with the heading title as the link text. Suitable for joining and outputting:
<%= links.join(" - ") %>
Note that this isn’t populated until after the filter is run.
61 62 63 |
# File 'lib/rote/filters/toc.rb', line 61 def headings @headings end |
Instance Method Details
#filter(text, page) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/rote/filters/toc.rb', line 64 def filter(text, page) # find headings *and insert named anchors* text.gsub(%r[<(#{@tags_re})>(.*?)</\1>]) do headings << (h = Heading[$1,$2]) %Q[<a name='#{h.anchor}'></a>#{$&}] end end |