Class: Bookify::Renderer

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

Constant Summary collapse

MARKDOWN_CONVERTER =
Redcarpet::Markdown.new(Redcarpet::Render::HTML)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Renderer

Returns a new instance of Renderer.



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

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

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

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



4
5
6
# File 'lib/bookify/renderer.rb', line 4

def columns
  @columns
end

#input_fileObject

Returns the value of attribute input_file.



4
5
6
# File 'lib/bookify/renderer.rb', line 4

def input_file
  @input_file
end

#layoutObject

Returns the value of attribute layout.



4
5
6
# File 'lib/bookify/renderer.rb', line 4

def layout
  @layout
end

#output_fileObject

Returns the value of attribute output_file.



4
5
6
# File 'lib/bookify/renderer.rb', line 4

def output_file
  @output_file
end

Instance Method Details

#renderObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bookify/renderer.rb', line 20

def render
  Prawn::Document.generate(output_file, margin: 50, page_layout: layout) 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 do
      doc.children.each { |c| Bookify::Node.render(c, pdf) }
    end
  end
end