Class: Bread::Basket::Poster::BlockCodeHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/bread/basket/poster/block_code_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf, layout, first_line, content) ⇒ BlockCodeHandler



7
8
9
10
11
12
# File 'lib/bread/basket/poster/block_code_handler.rb', line 7

def initialize(pdf, layout, first_line, content)
  @pdf = pdf
  @layout = layout
  @text_renderer = Redcarpet::Markdown.new(TextRenderer, TEXT_RENDERER_OPTS)
  sort_input(first_line, content)
end

Instance Attribute Details

#layoutObject (readonly)

Returns the value of attribute layout.



5
6
7
# File 'lib/bread/basket/poster/block_code_handler.rb', line 5

def layout
  @layout
end

#pdfObject (readonly)

Returns the value of attribute pdf.



5
6
7
# File 'lib/bread/basket/poster/block_code_handler.rb', line 5

def pdf
  @pdf
end

#text_rendererObject (readonly)

Returns the value of attribute text_renderer.



5
6
7
# File 'lib/bread/basket/poster/block_code_handler.rb', line 5

def text_renderer
  @text_renderer
end

Instance Method Details

#caption_content(caption) ⇒ Object



41
42
43
44
45
46
# File 'lib/bread/basket/poster/block_code_handler.rb', line 41

def caption_content(caption)
  pdf.stroke_bounds
  pdf.move_down 15
  text_renderer.render(caption)
  pdf.move_down 36
end

#create_image(image_src, caption) ⇒ Object

TODO: Get a lot of these values from the CSS



24
25
26
27
28
29
30
# File 'lib/bread/basket/poster/block_code_handler.rb', line 24

def create_image(image_src, caption)
  image_path = Bread::Basket::Poster.dir_path + '/' + image_src
  @columns_width = layout.columns[0].width
  pdf.image image_path, width: @columns_width, position: :center
  pdf.move_down 15
  draw_caption(caption)
end

#draw_caption(caption) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/bread/basket/poster/block_code_handler.rb', line 32

def draw_caption(caption)
  top_left = [pdf.bounds.left + 20, pdf.cursor]
  pdf.bounding_box(top_left, width: @columns_width - 40) do
    # spin up a new renderer since this is called within one and Redcarpet
    # doesn't allow this: https://github.com/vmg/redcarpet/issues/318
    caption_content caption
  end
end

#sort_input(first_line, content) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/bread/basket/poster/block_code_handler.rb', line 14

def sort_input(first_line, content)
  case first_line
  when /\A.*\.(jpg|jpeg|png)\Z/
    create_image(first_line, content)
  else
    puts "Warning: Couldn't figure out what to do with #{content} block.\n"
  end
end