Module: Booky::Layout

Defined in:
lib/booky/layout.rb,
lib/booky/layout/base.rb,
lib/booky/layout/element.rb,
lib/booky/layout/helpers.rb

Defined Under Namespace

Modules: Base, Helpers Classes: Element

Constant Summary collapse

LOG_ELEMENTS =
:h0, :h1, :h2
@@config =
nil

Class Method Summary collapse

Class Method Details

.compile(ast, layout = Booky::Layout::Base) ⇒ Object

Performs a double rendering and creates the final pdf. The first rendering is for the actual definition of the content and page references. The second then actually renders the pdf



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/booky/layout.rb', line 10

def self.compile(ast, layout = Booky::Layout::Base)
  
  # 1. Render: Creates TOC Contents
  config.runs = 1
  layout.document { |document| create_elements(ast, layout, document) }
  reset_config_after_first_render
  
  # 2. Render Creates Document
  config.runs = 2
  pdf = layout.document do |document|
    create_elements(ast, layout, document)
    create_page_links(document)
    create_outline(layout, document)
    create_page_numbering(layout, document)
  end
  pdf.render_file "#{Booky.name}.pdf"
  
end

.configObject

Creates a global layout config that holds the state during processing



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/booky/layout.rb', line 71

def self.config
  @@config ||= OpenStruct.new(
    :runs             => 0,
    :last_level       => 0,
    :current_chapter  => 0,
    :levels           => { 0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0 },
    :toc              => [],
    :toc_offset       => 1,
    :tof              => [],
    :no_content_pages => [],
    :chapter_info     => []
  )
end

.create_elements(ast, layout, document) ⇒ Object

Traverses the AST tree and tries to create a class of every entry type:

{:type => :h1, ... }
-> H1.new({:type => :h1, ... }, ...)

The object then executes its normal prawn methods in the to_prawn method



35
36
37
38
39
40
41
# File 'lib/booky/layout.rb', line 35

def self.create_elements(ast, layout, document)
  ast.each_with_index do |option, index|
     log_element(option)
     klass = layout.const_get to_constant(option)
     klass.new(option, document, layout, ast, index).to_prawn
   end
end

.create_outline(layout, document) ⇒ Object

Creates the PDF outline



56
57
58
59
60
61
62
63
64
# File 'lib/booky/layout.rb', line 56

def self.create_outline(layout, document)
  klass = layout.const_get "Outline"
  begin
    klass.new(nil, document, layout).to_prawn
  rescue Exception => e
    puts "Couldn't create outline due to the following error:".red
    puts e
  end
end

Traverses through every page and adds an link anchor named ‘page0’, ‘page1’, …



51
52
53
# File 'lib/booky/layout.rb', line 51

def self.create_page_links(document)
  (1..document.page_count).each { |page| document.add_dest("page#{page}", document.dest_fit(document.state.pages[page-1])) }
end

.create_page_numbering(layout, document) ⇒ Object



66
67
68
# File 'lib/booky/layout.rb', line 66

def self.create_page_numbering(layout, document)
  layout.numbering_options(document)
end

.log_element(option) ⇒ Object



44
45
46
47
48
# File 'lib/booky/layout.rb', line 44

def self.log_element(option)
  return unless config.runs == 2
  return unless LOG_ELEMENTS.include?(option[:type])
  puts "    Creating #{option[:text]}".green
end

.reset_config!Object

Resets the config



86
87
88
# File 'lib/booky/layout.rb', line 86

def self.reset_config!
  @@config = nil
end

.reset_config_after_first_renderObject

Resets the config state after the 1. Rendering



91
92
93
94
95
96
# File 'lib/booky/layout.rb', line 91

def self.reset_config_after_first_render
  config.current_chapter    = 0
  config.tof                = []
  config.no_content_pages   = []
  config.chapter_info       = []
end

.to_constant(option) ⇒ Object



98
99
100
# File 'lib/booky/layout.rb', line 98

def self.to_constant(option)
  "#{option[:type].to_s.split('_').map{|t| t.capitalize }.join}"
end