2011-10-08 23:36:38 +02:00
|
|
|
module Utils
|
2011-10-20 21:00:00 +02:00
|
|
|
module FileHelper
|
2011-10-26 15:46:25 +02:00
|
|
|
def binary?(string)
|
2011-10-20 21:00:00 +02:00
|
|
|
string.each_byte do |x|
|
2011-10-26 15:46:25 +02:00
|
|
|
x.nonzero? or return true
|
2011-10-20 21:00:00 +02:00
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def image?
|
|
|
|
mime_type =~ /image/
|
|
|
|
end
|
|
|
|
|
|
|
|
def text?
|
|
|
|
mime_type =~ /application|text/ && !binary?(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Colorize
|
|
|
|
def colorize
|
2011-10-21 14:35:42 +02:00
|
|
|
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' })
|
2011-10-20 21:00:00 +02:00
|
|
|
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
|
2011-10-08 23:36:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|