Module: Card::Set::Type::Basic::HtmlFormat

Extended by:
AbstractFormat
Defined in:
tmpsets/set/mod021-standard/type/basic.rb

Instance Method Summary collapse

Instance Method Details

#make_table_of_contents_list(items) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'tmpsets/set/mod021-standard/type/basic.rb', line 60

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
32
33
34
# File 'tmpsets/set/mod021-standard/type/basic.rb', line 21

def table_of_contents content
  return if nest_mode == :closed || !content.present?

  min = card.rule(:table_of_contents).to_i
  return unless min && min > 0

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

#toc_items(content) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'tmpsets/set/mod021-standard/type/basic.rb', line 36

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 = 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
  toc
end

#with_table_of_contents(content) ⇒ Object



17
18
19
# File 'tmpsets/set/mod021-standard/type/basic.rb', line 17

def with_table_of_contents content
  table_of_contents(content) || content
end