Sha256: 747f593cb7a6b613a750fa6586bf9f6987364f1fea6b42d2174183005d01790a
Contents?: true
Size: 1.27 KB
Versions: 17
Compression:
Stored size: 1.27 KB
Contents
begin require 'rouge' rescue LoadError # rubocop:disable Style/ConstantName Rouge = nil # rubocop:enable Style/ConstantName end require 'xcpretty/snippet' module XCPretty module Syntax def self.highlight(snippet) return snippet.contents unless Rouge 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 = Rouge::Formatters::Terminal256.new formatter.format(lexer.lex(snippet.contents)) else snippet.contents end end # @param [String] filename The filename # @param [String] contents The contents of the file # @return [Rouge::Lexer] 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 end end
Version data entries
17 entries across 17 versions & 2 rubygems