Class: TP::Slide::Code
Constant Summary
collapse
- LANGUAGE_MAPPINGS =
{
[nil] => 'text',
%w[clj] => 'clojure',
%w[objc] => 'cpp',
%w[rb] => 'ruby'
}
Instance Attribute Summary
Attributes inherited from TP::Slide
#markdown
Instance Method Summary
collapse
Methods inherited from TP::Slide
#centered_header, #content, #frames, #hard_width?, #header, #initialize, #lines, #pdf_content_height, #pdf_content_top_left, #pdf_header_height
Constructor Details
This class inherits a constructor from TP::Slide
Instance Method Details
#character_ratio(pdf) ⇒ Object
26
27
28
|
# File 'lib/tp/slide/code.rb', line 26
def character_ratio(pdf)
pdf.font_size / pdf.width_of("#")
end
|
#code ⇒ Object
52
53
54
|
# File 'lib/tp/slide/code.rb', line 52
def code
content.lines.to_a.reject { |line| line.start_with? "```" }.join
end
|
#height ⇒ Object
44
45
46
|
# File 'lib/tp/slide/code.rb', line 44
def height
code.lines.count + 2
end
|
#highlighted_code ⇒ Object
56
57
58
|
# File 'lib/tp/slide/code.rb', line 56
def highlighted_code
CodeRay.encode(code, language, :terminal)
end
|
#language ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/tp/slide/code.rb', line 66
def language
LANGUAGE_MAPPINGS.each do |keys, value|
return value if keys.include? raw_language
end
raw_language
end
|
#maximum_line_length ⇒ Object
30
31
32
|
# File 'lib/tp/slide/code.rb', line 30
def maximum_line_length
[code.lines.to_a.map { |line| line.rstrip.length }.max, 80].min
end
|
#prawn_code ⇒ Object
34
35
36
|
# File 'lib/tp/slide/code.rb', line 34
def prawn_code
CodeRay.scan(code.gsub(' ', Prawn::Text::NBSP), language).to_prawn
end
|
#raw_language ⇒ Object
60
61
62
63
64
|
# File 'lib/tp/slide/code.rb', line 60
def raw_language
match = lines[2].match(/^\`{3}(\w+)/)
match[1] if match
end
|
#render_pdf(pdf) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/tp/slide/code.rb', line 9
def render_pdf(pdf)
pdf.text_box ,
align: :center,
overflow: :shrink_to_fit,
single_line: true,
height: ,
size:
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 ⇒ Object
38
39
40
41
42
|
# File 'lib/tp/slide/code.rb', line 38
def render_terminal
+
"\n\n" +
highlighted_code
end
|
#width ⇒ Object
48
49
50
|
# File 'lib/tp/slide/code.rb', line 48
def width
lines.collect { |line| line.rstrip.length }.max
end
|