Sha256: 4448e6e05bb94aeb4e9898816ec5146ce4645431a7cc045baad85aaffcd80b8e

Contents?: true

Size: 1.27 KB

Versions: 15

Compression:

Stored size: 1.27 KB

Contents

format :html do
  view :open_content do |args|
    content = _render_core args
    table_of_contents(content) || content
  end

  def table_of_contents content
    return if @mode==:closed || !content.present?
    min = card.rule(:table_of_contents).to_i
    #warn "table_of #{name}, #{min}"
    return unless min and min > 0

    toc, dep = [], 1
    content.gsub!( /<(h\d)>(.*?)<\/h\d>/i ) do |match|
      if $~
        tag, value = $~[1,2]
        value = ActionView::Base.new.strip_tags(value).strip
        next if value.empty?
        item = { :value => value, :uri => URI.escape(value) }
        case tag.downcase
        when 'h1'
          item[:depth] = dep = 1; toc << item
        when 'h2'
          toc << []  if dep == 1
          item[:depth] = dep = 2; toc.last << item
        end
        %{<a name="#{item[:uri]}"></a>#{match}}
      end
    end

    if toc.flatten.length >= min
      content.replace %{ <div class="table-of-contents"> <h5>Table of Contents</h5> } +
        make_table_of_contents_list(toc) + '</div>'+ content
    end
  end


  def make_table_of_contents_list(items)
    list = items.collect do |i|
      Array === i ? make_table_of_contents_list(i) : %{<li><a href="##{i[:uri]}"> #{i[:value]}</a></li>}
    end.join("\n")
    "<ol>" + list + "</ol>"
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
wagn-1.14.7 mod/05_standard/set/type/basic.rb
wagn-1.14.6 mod/05_standard/set/type/basic.rb
wagn-1.14.5 mod/05_standard/set/type/basic.rb
wagn-1.14.4 mod/05_standard/set/type/basic.rb
wagn-1.14.3 mod/05_standard/set/type/basic.rb
wagn-1.14.2 mod/05_standard/set/type/basic.rb
wagn-1.14.1 mod/05_standard/set/type/basic.rb
wagn-1.14.0 mod/05_standard/set/type/basic.rb
wagn-1.14.0.pre3 mod/05_standard/set/type/basic.rb
wagn-1.14.0.pre2 mod/05_standard/set/type/basic.rb
wagn-1.14.0.pre1 mod/05_standard/set/type/basic.rb
wagn-1.13.0 mod/standard/set/type/basic.rb
wagn-1.13.0.pre2 mods/standard/sets/type/basic.rb
wagn-1.13.0.pre1 mods/standard/sets/type/basic.rb
wagn-1.13.0.pre mods/standard/sets/type/basic.rb