Class: Makeup::SyntaxHighlighter
- Inherits:
-
Object
- Object
- Makeup::SyntaxHighlighter
- Defined in:
- lib/makeup/syntax_highlighter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #highlight(path, code, options = {}) ⇒ Object
-
#initialize ⇒ SyntaxHighlighter
constructor
A new instance of SyntaxHighlighter.
- #lexer(path, code = nil, mode = nil) ⇒ Object
Constructor Details
#initialize ⇒ SyntaxHighlighter
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, = {}) [:lexer] ||= lexer(path, code) lexer = Pygments::Lexer.find([:lexer]) return unknown_lexer(code) unless lexer code = Pygments.highlight(code, ()) 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 |