Class: CodeSlide::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/code_slide/snippet.rb

Constant Summary collapse

MARK_PREFIX =
%r{^\s*(#|//)\s*}
START_MARK =
/#{MARK_PREFIX}START:\s*/
END_MARK =
/#{MARK_PREFIX}END:\s*/
EXTMAP =

rubocop:disable Style/MutableConstant

{ # rubocop:disable Style/MutableConstant
  '.rb'   => :ruby,
  '.py'   => :python,
  '.c'    => :c,
  '.java' => :java
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snippet, options = {}) ⇒ Snippet

Returns a new instance of Snippet.



59
60
61
62
# File 'lib/code_slide/snippet.rb', line 59

def initialize(snippet, options = {})
  @analyzer = CodeSlide::Analyzer.new(snippet, options)
  use_font(nil)
end

Class Method Details

.detect_language_type(filename) ⇒ Object



55
56
57
# File 'lib/code_slide/snippet.rb', line 55

def self.detect_language_type(filename)
  EXTMAP[File.extname(filename).downcase]
end

.from_file(filename, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/code_slide/snippet.rb', line 11

def self.from_file(filename, options = {})
  options = options.dup

  lines = File.readlines(filename)
  mark = options.delete(:mark)

  if mark
    start = lines.index { |line| line =~ /#{START_MARK}#{mark}\s*$/i }
    finish = lines.index { |line| line =~ /#{END_MARK}#{mark}\s*$/i }

    # if start is defined, don't include the comment itself
    start += 2 if start
  else
    start = options.delete(:start)
    finish = options.delete(:finish)
  end

  start ||= 1
  finish ||= -1
  line_start = options[:line_number_start] || start

  # assume people number their file lines starting at 1
  start -= 1 if start > 0
  finish -= 1 if finish > 0

  text = lines[start..finish].join

  if options.delete(:strip_indent)
    indent = text[/^\s*/]
    text.gsub!(/^#{indent}/m, "")
  end

  new(text,
      options.merge(lang: options[:lang] || detect_language_type(filename),
                    line_number_start: line_start))
end

Instance Method Details

#make_pdf(filename, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/code_slide/snippet.rb', line 73

def make_pdf(filename, options = {})
  PDFFormatter.new(@analyzer).
    use_font(@font, bold: @bold,
                    italic: @italic,
                    bold_italic: @bold_italic).
    build_pdf(**options).
    render_file(filename)
end

#make_png(filename, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/code_slide/snippet.rb', line 82

def make_png(filename, options = {})
  options = options.dup
  dpi = options.delete(:dpi)
  keep_pdf = options.delete(:keep_pdf)

  pdf_name = File.basename(filename, File.extname(filename)) + '.pdf'

  make_pdf(pdf_name, options)
  PNGFormatter.new(pdf_name).
    generate_png(filename, dpi: dpi)
  File.delete(pdf_name) unless keep_pdf
end

#use_font(path, bold: path, italic: path, bold_italic: path) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/code_slide/snippet.rb', line 64

def use_font(path, bold: path, italic: path, bold_italic: path)
  @font = path
  @bold = bold
  @italic = italic
  @bold_italic = bold_italic

  self
end