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
52 53 54 55 |
# File 'lib/makeup/syntax_highlighter.rb', line 52 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 46 |
# File 'lib/makeup/syntax_highlighter.rb', line 37 def highlight(path, code, = {}) [:lexer] ||= lexer(path, code) lexer = Pygments::Lexer.find([:lexer]) code = lexer.nil? ? code : Pygments.highlight(code, ()) CodeBlock.new(lexer && lexer.aliases.first, code) rescue MentosError => e # "MentosError" is what Pyments.rb raises when an unknown lexer is # attempted used CodeBlock.new(nil, @entities.encode(code)) end |
#lexer(path, code = nil, mode = nil) ⇒ Object
48 49 50 |
# File 'lib/makeup/syntax_highlighter.rb', line 48 def lexer(path, code = nil, mode = nil) self.class.lexer(path, code, mode) end |