Sha256: 0f8d51d5a7376261251cf310be9929b8bc7cb934b772a2b50c233b7d76e7f1a9
Contents?: true
Size: 1.51 KB
Versions: 9
Compression:
Stored size: 1.51 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 highlight_with_formatter(snippet, Rouge::Formatters::Terminal256.new) end def self.highlight_html(snippet) return snippet.contents unless Rouge highlight_with_formatter(snippet, Rouge::Formatters::HTML.new) end 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 # @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
9 entries across 9 versions & 2 rubygems