class TP::Slide::Code

Constants

LANGUAGE_MAPPINGS

Public Instance Methods

character_ratio(pdf) click to toggle source
# File lib/tp/slide/code.rb, line 27
def character_ratio(pdf)
  pdf.font_size / pdf.width_of("#")
end
code() click to toggle source
# File lib/tp/slide/code.rb, line 53
def code
  content.lines.to_a.reject { |line| line.start_with? "```" }.join
end
height() click to toggle source
# File lib/tp/slide/code.rb, line 45
def height
  code.lines.count + 2
end
highlighted_code() click to toggle source
# File lib/tp/slide/code.rb, line 57
def highlighted_code
  CodeRay.encode(code, language, :terminal)
end
language() click to toggle source
# File lib/tp/slide/code.rb, line 67
def language
  LANGUAGE_MAPPINGS.each do |keys, value|
    return value if keys.include? raw_language
  end

  raw_language
end
maximum_line_length() click to toggle source
# File lib/tp/slide/code.rb, line 31
def maximum_line_length
  [code.lines.to_a.map { |line| line.rstrip.length }.max, 80].min
end
prawn_code() click to toggle source
# File lib/tp/slide/code.rb, line 35
def prawn_code
  CodeRay.scan(code.gsub(' ', Prawn::Text::NBSP), language).to_prawn
end
raw_language() click to toggle source
# File lib/tp/slide/code.rb, line 61
def raw_language
  match = lines[2].match(/^\`{3}(\w+)/)

  match[1] if match
end
render_pdf(pdf) click to toggle source
# File lib/tp/slide/code.rb, line 10
def render_pdf(pdf)
  pdf.text_box header,
    align: :center,
    overflow: :shrink_to_fit,
    single_line: true,
    height: pdf_header_height,
    size: pdf_header_height

  pdf.font 'Courier' do
    pdf.formatted_text_box prawn_code,
      at: pdf_content_top_left(pdf),
      height: pdf_content_height(pdf),
      size: (pdf.bounds.width / maximum_line_length) * character_ratio(pdf),
      valign: :center
  end
end
render_terminal() click to toggle source
# File lib/tp/slide/code.rb, line 39
def render_terminal
  centered_header +
    "\n\n" +
    highlighted_code
end
width() click to toggle source
# File lib/tp/slide/code.rb, line 49
def width
  lines.collect { |line| line.rstrip.length }.max
end