Class: Makeup::SyntaxHighlighter

Inherits:
Object
  • Object
show all
Defined in:
lib/makeup/syntax_highlighter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyntaxHighlighter

Returns a new instance of SyntaxHighlighter.



33
34
35
# File 'lib/makeup/syntax_highlighter.rb', line 33

def initialize
  @entities = HTMLEntities.new
end

Class Method Details

.lexer(path, code = nil, mode = nil) ⇒ Object



51
52
53
54
# File 'lib/makeup/syntax_highlighter.rb', line 51

def self.lexer(path, code = nil, mode = nil)
  lexer = Linguist::Language.detect(path, code, mode)
  lexer && (lexer.aliases.first || lexer.name)
end

Instance Method Details

#highlight(path, code, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/makeup/syntax_highlighter.rb', line 37

def highlight(path, code, options = {})
  options[:lexer] ||= lexer(path, code)
  lexer = Pygments::Lexer.find(options[:lexer])
  return unknown_lexer(code) unless lexer
  code = Pygments.highlight(code, highlight_options(options))
  CodeBlock.new(lexer.aliases.first, code)
rescue MentosError
  unknown_lexer(code)
end

#lexer(path, code = nil, mode = nil) ⇒ Object



47
48
49
# File 'lib/makeup/syntax_highlighter.rb', line 47

def lexer(path, code = nil, mode = nil)
  self.class.lexer(path, code, mode)
end