Class: TTY::Markdown::Highlighter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/markdown/highlighter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for highlighting terminal code snippets

Instance Method Summary collapse

Constructor Details

#initialize(decorator, mode: 256) ⇒ Highlighter

Create a TTY::Markdown::Highlighter instance

Examples:

highlighter = TTY::Markdown::Highlighter.new(decorator)

Parameters:



24
25
26
27
# File 'lib/tty/markdown/highlighter.rb', line 24

def initialize(decorator, mode: 256)
  @decorator = decorator
  @mode = mode
end

Instance Method Details

#highlight(code, language = nil) ⇒ String

Highlight the code snippet

Examples:

highlighter.highlight("puts 'TTY Toolkit'", "ruby")

Parameters:

  • code (String)

    the code snippet

  • language (String, nil) (defaults to: nil)

    the code language

Returns:

  • (String)


42
43
44
45
46
47
# File 'lib/tty/markdown/highlighter.rb', line 42

def highlight(code, language = nil)
  return code unless @decorator.enabled?

  lexer = select_lexer(code, language)
  formatter.format(lexer.lex(code))
end