Module: Card::Set::Abstract::TableOfContents::HtmlFormat

Extended by:
Card::Set::AbstractFormat
Defined in:
card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb,
card/tmpsets/set/mod032-card-mod-legacy/abstract/table_of_contents.rb

Instance Method Summary collapse

Instance Method Details

#make_table_of_contents_list(items) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 61

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

#table_of_contents(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 21

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

#toc_items(content) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 37

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

#toc_minimumObject



33
34
35
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 33

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

#view: open_contentObject



9
10
11
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 9

view :open_content do
  with_table_of_contents _render_core
end

#view: titled_contentObject



13
14
15
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 13

view :titled_content do
  with_table_of_contents _render_core
end

#with_table_of_contents(content) ⇒ Object



17
18
19
# File 'card/tmpsets/set/mod031-card-mod-legacy/abstract/table_of_contents.rb', line 17

def with_table_of_contents content
  table_of_contents(content) || content
end