diff options
author | gitlabhq <m@gitlabhq.com> | 2011-10-21 21:55:50 +0300 |
---|---|---|
committer | gitlabhq <m@gitlabhq.com> | 2011-10-21 21:55:50 +0300 |
commit | ce9b8e7b12c61732cc181894e9b4f7dbab1f22a5 (patch) | |
tree | b23f93535fd986c9036b07644cbdd7e41d530d5d /lib/utils.rb | |
parent | 0f0c562de09e325c5689b3db8e646b68322f5515 (diff) | |
parent | 421b11158d8420b3fe337b63d5686c24b5644c94 (diff) | |
download | gitlab-ce-ce9b8e7b12c61732cc181894e9b4f7dbab1f22a5.tar.gz |
Merge branch 'dev' into feature/team_member_role_validation
Diffstat (limited to 'lib/utils.rb')
-rw-r--r-- | lib/utils.rb | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/lib/utils.rb b/lib/utils.rb index 6e7460ed99a..e57121a3e2a 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -1,8 +1,51 @@ 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 + system_colorize(data, name) + end + + def system_colorize(data, file_name) + ft = handle_file_type(file_name) + Pygments.highlight(data, :lexer => ft, :options => { :encoding => '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 |