Sha256: 9911e81a2a5bae2df021dd445ac8c8c749ffa119b6726d3f0e99fc711a839612

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

# -*- coding: utf-8 -*-
description  'Auto-generated table of contents'
dependencies 'utils/xml'

Page.attributes do
  boolean :toc
end

Filter.create :toc do |context, content|
  return content if !context.page.attributes['toc']

  doc = XML::Fragment(content)
  toc = ''
  level = 0
  count = [0]

  elem = doc.css('h1, h2, h3, h4, h5, h6').first
  offset = elem ? elem.name[1..1].to_i - 1 : 0

  doc.traverse do |child|
    if child.name =~ /^h(\d)$/
      nr = $1.to_i - offset
      if nr > level
        while nr > level
          toc << (level == 0 ? '<ol class="toc">' : '<ol>')
          count[level] = 0
          level += 1
          toc << '<li>' if nr > level
        end
      else
        while nr < level
          level -= 1
          toc << '</li></ol>'
        end
        toc << '</li>'
      end
      count[level-1] += 1
      headline = child.children.first ? child.children.first.inner_text : ''
      section = ['sec', count[0..level-1], headline.strip.gsub(/[^\w]/, '-')].flatten.join('-').downcase
      toc << %{<li class="toc#{level-offset+1}"><a href="##{section}">#{headline}</a>}
      child.inner_html = %{<span class="number" id="#{section}">#{count[0..level-1].join('.')}</span> #{child.inner_html}}
    end
  end

  while level > 0
    level -= 1
    toc << '</li></ol>'
  end

  toc + doc.to_xhtml
end

__END__
@@ locale.yml
cs:
  attribute_toc: 'Vytvořit obsah'
de:
  attribute_toc: 'Inhaltsverzeichnis erzeugen'
en:
  attribute_toc: 'Generate Table of Contents'
fr:
  attribute_toc: "Générer Table des Matières"

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
olelo-0.9.15 plugins/filters/toc.rb
olelo-0.9.14 plugins/filters/toc.rb
olelo-0.9.13 plugins/filters/toc.rb
olelo-0.9.12 plugins/filters/toc.rb
olelo-0.9.11 plugins/filters/toc.rb
olelo-0.9.10 plugins/filters/toc.rb