Sha256: 3c94ad869e996232242fb9b11aeb31795b7848dd69d22e440851678fd8ffd45b

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

# -*- encoding : utf-8 -*-
class Card; module Set; class Abstract
# Set: Abstract (TableOfContents)
#
module TableOfContents;
extend Card::Set
def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-legacy/set/abstract/table_of_contents.rb"; end
module HtmlFormat; module_parent.send :register_set_format, Card::Format::HtmlFormat, self; extend Card::Set::AbstractFormat
  view :open_content do
    with_table_of_contents _render_core
  end

  view :titled_content do
    with_table_of_contents _render_core
  end

  def with_table_of_contents content
    table_of_contents(content) || content
  end

  def table_of_contents content
    return if nest_mode == :compact || !content.present? || toc_minimum&.zero?

    toc = toc_items content
    return unless toc.flatten.length >= toc_minimum

    content.replace(
      %( <div class="table-of-contents"> <h5>#{tr(:toc)}</h5> ) +
        make_table_of_contents_list(toc) + "</div>" + content
    )
  end

  def toc_minimum
    @toc_minimum ||= card.rule(:table_of_contents).to_i
  end

  def toc_items content
    toc = []
    dep = 1
    content.gsub!(/<(h\d)>(.*?)<\/h\d>/i) do |match|
      if $LAST_MATCH_INFO
        tag, value = $LAST_MATCH_INFO[1, 2]
        value = 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
    toc
  end

  def make_table_of_contents_list items
    list = items.map do |i|
      if i.is_a?(Array)
        make_table_of_contents_list(i)
      else
        %(<li><a href="##{i[:uri]}"> #{i[:value]}</a></li>)
      end
    end.join("\n")
    "<ol>" + list + "</ol>"
  end
end
end;end;end;end;
# ~~ generated from /Users/ethan/dev/decko/gem/card-mod-legacy/set/abstract/table_of_contents.rb ~~

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
card-1.101.5 tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb
card-1.101.4 tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb
card-1.101.3 tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb
card-1.101.2 tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb
card-1.101.1 tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb
card-1.101.0 tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb