Class: Bookify::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/bookify/renderer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout: :portrait, columns: 2, output_file:, input_file: nil, input_text: nil, column_spacer: nil, margin: 50, page_size: nil) ⇒ Renderer

Returns a new instance of Renderer.



20
21
22
23
24
25
26
27
28
29
# File 'lib/bookify/renderer.rb', line 20

def initialize(layout: :portrait, columns: 2, output_file:, input_file: nil, input_text: nil, column_spacer: nil, margin: 50, page_size: nil)
  @layout = layout
  @columns = columns
  @output_file = output_file
  @input_file = input_file
  @input_text = input_text
  @column_spacer = column_spacer
  @margin = margin
  @page_size = page_size
end

Instance Attribute Details

#column_spacerObject

Returns the value of attribute column_spacer.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def column_spacer
  @column_spacer
end

#columnsObject

Returns the value of attribute columns.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def columns
  @columns
end

#input_fileObject

Returns the value of attribute input_file.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def input_file
  @input_file
end

#input_textObject

Returns the value of attribute input_text.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def input_text
  @input_text
end

#layoutObject

Returns the value of attribute layout.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def layout
  @layout
end

#marginObject

Returns the value of attribute margin.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def margin
  @margin
end

#output_fileObject

Returns the value of attribute output_file.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def output_file
  @output_file
end

#page_sizeObject

Returns the value of attribute page_size.



2
3
4
# File 'lib/bookify/renderer.rb', line 2

def page_size
  @page_size
end

Class Method Details

.from_args(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bookify/renderer.rb', line 4

def self.from_args(args)
  if ["-l", "--landscape"].include?(args[0])
    args.shift
    layout = :landscape
    columns = 3
  else
    layout = :portrait
    columns = 2
  end

  input_file = args[0]
  output_file = args[1] || input_file.split("/").last.gsub(/\.\w+/, ".pdf")

  new(layout: layout, columns: columns, input_file: input_file, output_file: output_file)
end

Instance Method Details

#renderObject



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
# File 'lib/bookify/renderer.rb', line 31

def render
  doc_opts = {margin: margin, page_layout: layout, page_size: page_size}.compact
  Bookify::Sections.reset

  Prawn::Document.generate(output_file, doc_opts) do |pdf|
    font_path = "#{File.dirname(__FILE__)}/../../fonts"

    pdf.font_families["Book Antiqua"] = {
      normal: {file: "#{font_path}/BookAntiqua.ttf"},
      bold: {file: "#{font_path}/BookAntiqua-Bold.ttf"},
      italic: {file: "#{font_path}/BookAntiqua-Italic.ttf"},
      bold_italic: {file: "#{font_path}/BookAntiqua-BoldItalic.ttf"}
    }

    pdf.fill_color "000000"
    pdf.stroke_color "333333"
    pdf.line_width(0.5)
    pdf.default_leading 0.5

    pdf.column_box [0, pdf.cursor], columns: columns, width: pdf.bounds.width, spacer: (column_spacer || pdf.font_size) do
      doc.children.each { |c| Bookify::Node.render(c, pdf) }
    end

    Bookify::Sections.all.each do |section|
      pdf.outline.section(section[:title], destination: section[:dest]) do
        section[:subsections].each do |subsection|
          pdf.outline.page title: subsection[:title], destination: subsection[:dest]
        end
      end
    end
  end
end