Module: PrawnComponents::Components::TableOfContents

Defined in:
lib/prawn_components/components/table_of_contents.rb

Instance Method Summary collapse

Instance Method Details

#table_of_contents(value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/prawn_components/components/table_of_contents.rb', line 30

def table_of_contents(value)
  value.each do |(chapter_title, data)|
    table_data = []
    start_page = data[:page]
    table_data << [
      { content: chapter_title, font_style: :bold }, 
      { content: start_page.to_s, font_style: :bold, align: :right }
    ]
    data[:headings].each do |(title, page)|
      str = "\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0#{title}\xC2\xA0\xC2\xA0"
      line_size = 140
      1.upto(line_size - str.size) do |i|
        if i.odd?
          str << "."
        else
          str << "\xC2\xA0"
        end
      end
      length_indicator = data[:headings].keys.sort_by { |s| s.size }.reverse.first.size
      diff = length_indicator - title.size

      if diff > 0
        next_diff = diff
        next_diff = 2 if diff == 1
        1.upto(next_diff + 1) do |i|
          if i.odd?
            str << "\xC2\xA0"
          else
            str << "."
          end
        end
      end

      table_data << [
        { content: title }, 
        { content: page.to_s, align: :right }
      ]
    end

    table(table_data,
      cell_style: { border_width: 0, padding: [10, 10, 5, 10] },
      width: 560
    )
    move_down(30)
  end
end