module TTY::Markdown::SyntaxHighliter

Syntax highlighting for terminal code snippets

@api private

Public Class Methods

available_lexers() click to toggle source

Return all available language lexers

@return [Array]

@api private

# File lib/tty/markdown/syntax_highlighter.rb, line 17
def available_lexers
  Rouge::Lexer.all.sort_by(&:tag).inject([]) do |names, lexer|
    names << lexer.tag
    if lexer.aliases.any?
      lexer.aliases.each { |a| names << a }
    end
    names
  end
end
guess_lang(code) click to toggle source

Guess langauge from code snippet

@return [String, nil]

@api private

# File lib/tty/markdown/syntax_highlighter.rb, line 33
def guess_lang(code)
  start_line = code.lines[0]
  if available_lexers.include?(start_line.strip.downcase)
    start_line.strip.downcase
  end
end
highlight(code, mode: 256, lang: nil, enabled: nil, color: ->(line) { line } click to toggle source

Highlight code snippet

@param [String] code @param [Integer] mode

the color mode supported by the terminal

@param [String] lang

the code snippet language

@param [Boolean] enabled

whether or not coloring is enabled

@param [Proc] color

the fallback coloring

@api public

# File lib/tty/markdown/syntax_highlighter.rb, line 54
def highlight(code, mode: 256, lang: nil, enabled: nil,
              color: ->(line) { line })
  lang = guess_lang(code) if lang.nil?
  lexer = Rouge::Lexer.find_fancy(lang, code) || Rouge::Lexers::PlainText

  if enabled == false
    code
  elsif 256 <= mode
    formatter = Rouge::Formatters::Terminal256.new
    formatter.format(lexer.lex(code))
  else
    code.lines.map { |line| color.(line.chomp) }.join("\n")
  end
end

Private Instance Methods

available_lexers() click to toggle source

Return all available language lexers

@return [Array]

@api private

# File lib/tty/markdown/syntax_highlighter.rb, line 17
def available_lexers
  Rouge::Lexer.all.sort_by(&:tag).inject([]) do |names, lexer|
    names << lexer.tag
    if lexer.aliases.any?
      lexer.aliases.each { |a| names << a }
    end
    names
  end
end
guess_lang(code) click to toggle source

Guess langauge from code snippet

@return [String, nil]

@api private

# File lib/tty/markdown/syntax_highlighter.rb, line 33
def guess_lang(code)
  start_line = code.lines[0]
  if available_lexers.include?(start_line.strip.downcase)
    start_line.strip.downcase
  end
end
highlight(code, mode: 256, lang: nil, enabled: nil, color: ->(line) { line } click to toggle source

Highlight code snippet

@param [String] code @param [Integer] mode

the color mode supported by the terminal

@param [String] lang

the code snippet language

@param [Boolean] enabled

whether or not coloring is enabled

@param [Proc] color

the fallback coloring

@api public

# File lib/tty/markdown/syntax_highlighter.rb, line 54
def highlight(code, mode: 256, lang: nil, enabled: nil,
              color: ->(line) { line })
  lang = guess_lang(code) if lang.nil?
  lexer = Rouge::Lexer.find_fancy(lang, code) || Rouge::Lexers::PlainText

  if enabled == false
    code
  elsif 256 <= mode
    formatter = Rouge::Formatters::Terminal256.new
    formatter.format(lexer.lex(code))
  else
    code.lines.map { |line| color.(line.chomp) }.join("\n")
  end
end