Browse files

Use Linguist to detect binaries, images, mime types. Use pygments.rb …

…instead of albino.
  • Loading branch information...
1 parent 3a2b273 commit f787af3be8336526448ebb610d29a090ef4b6077 @akzhan committed Oct 20, 2011
View
6 Gemfile
@@ -1,4 +1,4 @@
-source 'http://rubygems.org'
+source :rubygems
gem 'rails', '3.1.0'
@@ -15,11 +15,11 @@ gem 'therubyracer'
gem 'faker'
gem 'seed-fu', :git => 'git://github.com/mbleigh/seed-fu.git'
gem "inifile"
-gem "albino", :git => "git://github.com/gitlabhq/albino.git"
gem "kaminari"
gem "thin"
gem "git"
gem "acts_as_list"
+gem "linguist", :git => "git://github.com/github/linguist.git"
group :assets do
gem 'sass-rails', " ~> 3.1.0"
@@ -44,8 +44,8 @@ group :development, :test do
gem 'launchy'
end
-
group :test do
gem 'turn', :require => false
gem 'simplecov', :require => false
end
+
View
21 Gemfile.lock
@@ -5,11 +5,14 @@ GIT
annotate (2.4.1.beta1)
GIT
- remote: git://github.com/gitlabhq/albino.git
- revision: 118380924969f3a856659f86ea1f40c1ba7bfcb1
+ remote: git://github.com/github/linguist.git
+ revision: d8903afc12b1f11a82d4b4bce5b677a039b60bf4
specs:
- albino (1.3.3)
- posix-spawn (>= 0.3.6)
+ linguist (1.0.0)
+ charlock_holmes (~> 0.6.6)
+ escape_utils (~> 0.2.3)
+ mime-types (~> 1.16)
+ pygments.rb (~> 0.2.3)
GIT
remote: git://github.com/gitlabhq/grit.git
@@ -72,6 +75,7 @@ GEM
ZenTest (= 4.5)
awesome_print (0.4.0)
bcrypt-ruby (3.0.1)
+ blankslate (2.1.2.4)
builder (3.0.0)
capybara (1.0.1)
mime-types (>= 1.16)
@@ -82,6 +86,7 @@ GEM
xpath (~> 0.1.4)
carrierwave (0.5.7)
activesupport (~> 3.0)
+ charlock_holmes (0.6.8)
childprocess (0.2.2)
ffi (~> 1.0.6)
coffee-rails (3.1.1)
@@ -100,6 +105,7 @@ GEM
warden (~> 1.0.3)
diff-lcs (1.1.3)
erubis (2.7.0)
+ escape_utils (0.2.4)
eventmachine (0.12.10)
execjs (1.2.6)
multi_json (~> 1.0)
@@ -138,6 +144,8 @@ GEM
orm_adapter (0.0.5)
polyglot (0.3.2)
posix-spawn (0.3.6)
+ pygments.rb (0.2.3)
+ rubypython (>= 0.5.1)
rack (1.3.2)
rack-cache (1.0.3)
rack (>= 0.4)
@@ -189,6 +197,9 @@ GEM
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
+ rubypython (0.5.1)
+ blankslate (>= 2.1.2.3)
+ ffi (~> 1.0.7)
rubyzip (0.9.4)
sass (3.1.7)
sass-rails (3.1.1)
@@ -242,7 +253,6 @@ PLATFORMS
DEPENDENCIES
acts_as_list
- albino!
annotate!
autotest
autotest-rails
@@ -260,6 +270,7 @@ DEPENDENCIES
jquery-rails
kaminari
launchy
+ linguist!
rails (= 3.1.0)
rails-footnotes (>= 3.7.5.rc4)
rspec-rails
View
21 app/helpers/application_helper.rb
@@ -53,25 +53,4 @@ def search_autocomplete_source
[projects, default_nav, project_nav].flatten.to_json
end
- def handle_file_type(file_name, mime_type)
- 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
end
View
6 app/views/commits/_diff.html.haml
@@ -35,7 +35,7 @@
%strong{:id => "#{diff.b_path}"}= diff.b_path
%br/
.diff_file_content
- - if file.mime_type =~ /application|text/ && !Utils.binary?(file.data)
+ - if ! file.binary?
- lines_arr = diff.diff.lines.to_a
- line_old = lines_arr[2].match(/-(\d)/)[0].to_i.abs rescue 0
- line_new = lines_arr[2].match(/\+(\d)/)[0].to_i.abs rescue 0
@@ -50,9 +50,9 @@
- else
- line_new += 1
- line_old += 1
- - elsif file.mime_type =~ /image/
+ - elsif file.image?
.diff_file_content_image
- %img{:src => "data:image/jpeg;base64,#{Base64.encode64(file.data)}"}
+ %img{:src => "data:#{file.mime_type};base64,#{Base64.encode64(file.data)}"}
- else
%p
%center No preview for this file type
View
9 app/views/projects/_tree_file.html.haml
@@ -6,14 +6,13 @@
= link_to "raw", blob_project_path(@project, :commit_id => @commit.id, :path => params[:path] ), :class => "right", :target => "_blank"
= link_to "history", project_commits_path(@project, :path => params[:path]), :class => "right", :style => "margin-right:10px;"
%br/
- - if file.mime_type =~ /application|text/ && !Utils.binary?(file.data)
+ - if ! file.binary?
.view_file_content
- - ft = handle_file_type(file.name, file.mime_type)
:erb
- <%= raw Albino.colorize(content, ft, :html, 'utf-8', "linenos=True") %>
- - elsif file.mime_type =~ /image/
+ <%= raw file.colorize() %>
+ - elsif file.image?
.view_file_content_image
- %img{ :src => "data:image/jpeg;base64,#{Base64.encode64(file.data)}"}
+ %img{ :src => "data:#{file.mime_type};base64,#{Base64.encode64(file.data)}"}
- else
%p
%center No preview for this file type
View
13 app/views/projects/empty.html.erb
@@ -1,12 +1,17 @@
<div class="">
<div class="git-empty">
<h2>Git global setup:</h2>
-<% setup_str = <<eos
+<%
+
+bash_lexer = Pygments::Lexer[:bash]
+
+setup_str = <<eos
git config --global user.name "#{current_user.name}"
git config --global user.email "#{current_user.email}"
eos
+
%>
- <%= raw Albino.colorize(setup_str, :bash) %>
+<%= raw bash_lexer.highlight(setup_str) %>
<br />
<br />
<h2>Next steps:</h2>
@@ -21,7 +26,7 @@ git remote add origin #{@project.url_to_repo}
git push -u origin master
eos
%>
- <%= raw Albino.colorize(repo_setup_str, :bash) %>
+ <%= raw bash_lexer.highlight(repo_setup_str) %>
<br /><br />
<h2>Existing Git Repo?</h2>
@@ -31,7 +36,7 @@ git remote add origin #{@project.url_to_repo}
git push -u origin master
eos
%>
- <%= raw Albino.colorize(exist_repo_setup_str, :bash) %>
+ <%= raw bash_lexer.highlight(exist_repo_setup_str) %>
<br /><br />
<h2>Remove this project?</h2>
View
5 config/initializers/linguist_grit.rb
@@ -0,0 +1,5 @@
+require 'grit'
+require 'pygments'
+require 'linguist'
+
+Grit::Blob.class_eval { include Linguist::BlobHelper }
View
6 lib/utils.rb
@@ -1,8 +1,2 @@
module Utils
- def self.binary?(string)
- string.each_byte do |x|
- x.nonzero? or return true
- end
- false
- end
end

0 comments on commit f787af3

Please sign in to comment.