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(input_file, output_file = nil) ⇒ Renderer

Returns a new instance of Renderer.



6
7
8
9
# File 'lib/bookify/renderer.rb', line 6

def initialize(input_file, output_file = nil)
  self.input_file = input_file
  self.output_file = output_file || input_file.gsub(/\.\w+/, ".pdf")
end

Instance Attribute Details

#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

#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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bookify/renderer.rb', line 11

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