Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utils.rb « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 52f3ee2e195aeaa4a95fceda99029b83f0f22d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Utils
  module FileHelper
    def binary?(string)
      string.each_byte do |x|
        x.nonzero? or return true
      end
      false
    end

    def image?
      mime_type =~ /image/
    end

    def text?
      mime_type =~ /application|text/ && !binary?(data)
    end
  end

  module Colorize
    def colorize
      system_colorize(data, name)
    end

    def system_colorize(data, file_name)
      options = { :encoding => 'utf-8', :linenos => 'True' }

      # Try detect language with pygments
      Pygments.highlight data, :filename => file_name, :options => options
    rescue 
      # if it fails use manual detection
      ft = handle_file_type(file_name)
      Pygments.highlight(data, :lexer => ft, :options => options)
    end

    def handle_file_type(file_name)
      case file_name
      when /(\.ru|Gemfile)$/
        :ruby
      else
        :text
      end
    end
  end
end