diff options
| author | gitlabhq <m@gitlabhq.com> | 2011-10-20 22:00:00 +0300 |
|---|---|---|
| committer | gitlabhq <m@gitlabhq.com> | 2011-10-20 22:00:00 +0300 |
| commit | c463eeb0908e5c00008073c07e71e06434393342 (patch) | |
| tree | de9e958048e0d7e148d8e85bd5512339aca3ec1f /lib | |
| parent | 48924dfeeafbe965ec06797b10010ebd8a5dcfcd (diff) | |
| download | gitlab-ce-c463eeb0908e5c00008073c07e71e06434393342.tar.gz | |
refactoring
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/utils.rb | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/lib/utils.rb b/lib/utils.rb index 6e7460ed99a..40bf26735c2 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -1,8 +1,47 @@ module Utils - def self.binary?(string) - string.each_byte do |x| - x.nonzero? or return true + 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 + ft = handle_file_type(name, mime_type) + Albino.colorize(data, ft, :html, 'utf-8', "linenos=True") + end + + def handle_file_type(file_name, mime_type = nil) + if file_name =~ /(\.rb|\.ru|\.rake|Rakefile|\.gemspec|\.rbx|Gemfile)$/ + :ruby + elsif file_name =~ /\.py$/ + :python + elsif file_name =~ /(\.pl|\.scala|\.c|\.cpp|\.java|\.haml|\.html|\.sass|\.scss|\.xml|\.php|\.erb)$/ + $1[1..-1].to_sym + elsif file_name =~ /\.js$/ + :javascript + elsif file_name =~ /\.sh$/ + :bash + elsif file_name =~ /\.coffee$/ + :coffeescript + elsif file_name =~ /\.yml$/ + :yaml + elsif file_name =~ /\.md$/ + :minid + else + :text + end end - false end end |
