Class: CodeSlide::Snippet
- Inherits:
-
Object
- Object
- CodeSlide::Snippet
- 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
-
#initialize(snippet, options = {}) ⇒ Snippet
constructor
A new instance of Snippet.
- #make_pdf(filename, options = {}) ⇒ Object
- #make_png(filename, options = {}) ⇒ Object
- #use_font(path, bold: path, italic: path, bold_italic: path) ⇒ Object
Constructor Details
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, = {}) = .dup lines = File.readlines(filename) mark = .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 = .delete(:start) finish = .delete(:finish) end start ||= 1 finish ||= -1 line_start = [: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 .delete(:strip_indent) indent = text[/^\s*/] text.gsub!(/^#{indent}/m, "") end new(text, .merge(lang: [: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, = {}) PDFFormatter.new(@analyzer). use_font(@font, bold: @bold, italic: @italic, bold_italic: @bold_italic). build_pdf(**). 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, = {}) = .dup dpi = .delete(:dpi) keep_pdf = .delete(:keep_pdf) pdf_name = File.basename(filename, File.extname(filename)) + '.pdf' make_pdf(pdf_name, ) 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 |