class Rote::Filters::TOC

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.



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. The @links array will usually be used in layout.

Public Class Methods

new(tags_re = /h\d+/, attrs = {}) click to toggle source
# File lib/rote/filters/toc.rb, line 47
def initialize(tags_re = /h\d+/, attrs = {})
  @tags_re = tags_re
  @attrs = attrs
  @headings = []

Public Instance Methods

filter(text, page) click to toggle source
# File lib/rote/filters/toc.rb, line 65
def filter(text, page)
  # find headings *and insert named anchors*
  text.gsub(%r[<(#{@tags_re})>(.*?)</\1>]) do
    headings << (h = Heading[$1,$2])
    %Q[<a id='#{h.anchor}'></a>#{$&}]