Class: Booky::Layout::Base::Toc

Inherits:
Element
  • Object
show all
Includes:
Helpers
Defined in:
lib/booky/layout/base/toc.rb

Constant Summary collapse

OPTIONS =
{
  :align => :right,
  :size => 24
}

Instance Attribute Summary

Attributes inherited from Element

#ast, #document, #index, #options

Instance Method Summary collapse

Methods included from Helpers

#add_to_toc, #is_first_rendering?, #update_levels

Methods inherited from Element

#initialize, #method_missing, #next_option, #previous_option

Constructor Details

This class inherits a constructor from Booky::Layout::Element

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Booky::Layout::Element

Instance Method Details

#calculate_toc_pagesObject

Creates an temporary empty document that contains only the toc and stores its page_count and the offset that needs to be added to all following pages right after the toc



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/booky/layout/base/toc.rb', line 92

def calculate_toc_pages
  @precompiling = true
  @current_document = @document
  toc = @layout.document do |document|
    @document = document
    make_toc
    @document = @current_document
  end
  Booky::Layout.config.toc_pages  = toc.page_count
  Booky::Layout.config.toc_offset = 0
  Booky::Layout.config.toc_offset = toc.page_count - 1 if toc.page_count > 1
  @precompiling = false
end

#create_headingObject



10
11
12
13
14
15
16
# File 'lib/booky/layout/base/toc.rb', line 10

def create_heading
  fill_color Booky::Layout::Base::COLORS[1]
  text @options[:text], OPTIONS
  fill_color Booky::Layout::Base::COLORS[0]
  
  move_down 4.cm
end

#create_tocObject



18
19
20
21
22
23
24
25
26
27
28
29
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
76
77
78
# File 'lib/booky/layout/base/toc.rb', line 18

def create_toc
  previous_level = 0
  toc_data = Booky::Layout.config.toc.map do |entry|
    
    # Styles
    max_size        = 14
    size            = max_size - entry[:level]
    padding         = entry[:level] > 1 ? (entry[:level]-1) * 10 : 0
    padding_top     = entry[:level] == 1 ? 20 : 5
    padding_bottom  = entry[:level] == 1 ? 5 : 0
    borders         = entry[:level] == 1 ? [:bottom] : []
    style           = entry[:level] == 1 ? :normal : :italic
    padding_top     = 20 if entry[:level] == 0 and previous_level > 0
    previous_level  = entry[:level]
    
    # Pages Offset
    add_offset_if = 
      entry[:page]  > Booky::Layout.config.toc_page,
      @precompiling == false
    
    entry[:page] += Booky::Layout.config.toc_offset if add_offset_if.all?
    
    # Line NUmbering
    numbering = entry[:levels].values.dup
    numbering.shift
    numbering.reject!{|e| e == 0}
    
    # Cells
    name = make_cell(
      :content        => "<link anchor='page#{entry[:page]}'>#{numbering.join(".")}    #{entry[:name]}</link>",
      :padding_left   => padding, 
      :padding_top    => padding_top, 
      :padding_bottom => padding_bottom, 
      :size           => size, 
      :font_style     => style, 
      :border_width   => 1, 
      :borders        => borders
    )
    page = make_cell(
      :content        => "<link anchor='page#{entry[:page]}'>#{entry[:page]}</link>", 
      :align          => :right,
      :font_style     => style,
      :padding_top    => padding_top,
      :padding_bottom => padding_bottom,
      :borders => borders
    )
    
    [name, page]
  end
  
  # Table
  table(toc_data, 
    :width => bounds.width, 
    :column_widths => { 1 => 50 }, 
    :cell_style => { 
      :inline_format => true, 
      :border_color => Booky::Layout::Base::COLORS[2], 
      :border_width => 0.5
    }
  )
end

#define_tocObject

Defines itself as a page in the toc contents to get the toc pages calculated through calculate_toc_pages right



82
83
84
85
86
87
# File 'lib/booky/layout/base/toc.rb', line 82

def define_toc
  start_new_page
  
  update_levels(0)
  add_to_toc(0)
end

#make_tocObject

Actually creates the toc with the heading and the table



107
108
109
110
111
112
113
114
115
# File 'lib/booky/layout/base/toc.rb', line 107

def make_toc
  Booky::Layout.config.toc_page = page_number
  
  update_levels(0)
  add_to_toc(0)

  create_heading
  create_toc
end

#to_prawnObject

The first run creates the toc contents, so toc just states that its here, and also wants to be in the toc as an entry. The second run then finds out how long the toc will be, and actually renders it to the document



120
121
122
123
124
125
126
127
128
# File 'lib/booky/layout/base/toc.rb', line 120

def to_prawn
  case Booky::Layout.config.runs
    when 1 then define_toc
    when 2
      calculate_toc_pages
      start_new_page
      make_toc
  end
end