Class: Crosstest::Psychic::CodeHelper::Highlighter

Inherits:
Object
  • Object
show all
Defined in:
lib/crosstest/psychic/code_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Highlighter

Returns a new instance of Highlighter


5
6
7
8
9
10
11
# File 'lib/crosstest/psychic/code_helper.rb', line 5

def initialize(opts)
  require 'rouge'
  @lexer = Rouge::Lexer.find(opts[:language]) || Rouge::Lexer.guess_by_filename(opts[:filename])
  @formatter = opts[:formatter]
rescue LoadError # rubocop:disable Lint/HandleExceptions
  # No highlighting support
end

Instance Method Details

#highlight(source) ⇒ Object


13
14
15
16
17
18
19
20
# File 'lib/crosstest/psychic/code_helper.rb', line 13

def highlight(source)
  if defined?(Rouge)
    Rouge.highlight(source, @lexer, @formatter)
  else
    # No highlighting support
    source
  end
end