Module: Middleman::Syntax::Highlighter

Defined in:
lib/middleman-syntax/highlighter.rb

Class Method Summary collapse

Class Method Details

.highlight(code, language = nil, opts = {}) ⇒ Object

A helper module for highlighting code



9
10
11
12
13
14
15
16
17
18
# File 'lib/middleman-syntax/highlighter.rb', line 9

def self.highlight(code, language=nil, opts={})
  lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText

  highlighter_options = options.to_h.merge(opts)
  highlighter_options[:css_class] = [ highlighter_options[:css_class], lexer.tag ].join(' ')
  lexer_options = highlighter_options.delete(:lexer_options)

  formatter = Middleman::Syntax::Formatters::HTML.new(highlighter_options)
  formatter.format(lexer.lex(code, lexer_options))
end