module XCPretty::Syntax

Public Class Methods

find_lexer(filename, contents) click to toggle source

@param [String] filename The filename @param [String] contents The contents of the file @return [Rouge::Lexer]

# File lib/xcpretty/syntax.rb, line 41
def self.find_lexer(filename, contents)
  case File.extname(filename)
  when '.cpp', '.cc', '.c++', '.cxx', '.hpp', '.h++', '.hxx'
    Rouge::Lexers::Cpp
  when '.m', '.h' then Rouge::Lexers::ObjectiveC
  when '.swift' then Rouge::Lexers::Swift
  when '.ruby', '.rb' then Rouge::Lexers::Ruby
  else
    options = {
      filename: File.basename(filename),
      source: contents
    }
    Rouge::Lexer.guesses(options).first
  end
end
highlight(snippet) click to toggle source
# File lib/xcpretty/syntax.rb, line 13
def self.highlight(snippet)
  return snippet.contents unless Rouge
  highlight_with_formatter(snippet, Rouge::Formatters::Terminal256.new)
end
highlight_html(snippet) click to toggle source
# File lib/xcpretty/syntax.rb, line 18
def self.highlight_html(snippet)
  return snippet.contents unless Rouge
  highlight_with_formatter(snippet, Rouge::Formatters::HTML.new)
end
highlight_with_formatter(snippet, formatter) click to toggle source
# File lib/xcpretty/syntax.rb, line 23
def self.highlight_with_formatter(snippet, formatter)
  if snippet.file_path.include?(':')
    filename = snippet.file_path.rpartition(':').first
  else
    filename = snippet.file_path
  end

  lexer = find_lexer(filename, snippet.contents)
  if lexer
    formatter.format(lexer.lex(snippet.contents))
  else
    snippet.contents
  end
end