Commit 5b6019f357d01d7871068da15549d19be1a0cf4f

Authored by Arthur Schreiber
1 parent 9ee34575

Using github's linguist to better detect filetypes and to do syntax highlighting.

@@ -18,6 +18,7 @@ gem "six" @@ -18,6 +18,7 @@ gem "six"
18 gem "therubyracer" 18 gem "therubyracer"
19 gem "faker" 19 gem "faker"
20 gem "seed-fu" 20 gem "seed-fu"
  21 +gem "linguist", "~> 1.0.0", :git => "https://github.com/github/linguist.git"
21 gem "pygments.rb", "0.2.7" 22 gem "pygments.rb", "0.2.7"
22 gem "thin" 23 gem "thin"
23 gem "unicorn" 24 gem "unicorn"
@@ -5,6 +5,16 @@ GIT @@ -5,6 +5,16 @@ GIT
5 annotate (2.4.1.beta1) 5 annotate (2.4.1.beta1)
6 6
7 GIT 7 GIT
  8 + remote: https://github.com/github/linguist.git
  9 + revision: c444c25b27131cd2aca6017f2a9ce5eae60dfcd3
  10 + specs:
  11 + linguist (1.0.0)
  12 + charlock_holmes (~> 0.6.6)
  13 + escape_utils (~> 0.2.3)
  14 + mime-types (~> 1.18)
  15 + pygments.rb (~> 0.2.11)
  16 +
  17 +GIT
8 remote: https://github.com/gitlabhq/gitolite-client.git 18 remote: https://github.com/gitlabhq/gitolite-client.git
9 revision: 36dabd226caa40ff052677719adaacbfe667b36c 19 revision: 36dabd226caa40ff052677719adaacbfe667b36c
10 specs: 20 specs:
@@ -104,6 +114,7 @@ GEM @@ -104,6 +114,7 @@ GEM
104 diff-lcs (1.1.3) 114 diff-lcs (1.1.3)
105 drapper (0.8.4) 115 drapper (0.8.4)
106 erubis (2.7.0) 116 erubis (2.7.0)
  117 + escape_utils (0.2.4)
107 eventmachine (0.12.10) 118 eventmachine (0.12.10)
108 execjs (1.3.0) 119 execjs (1.3.0)
109 multi_json (~> 1.0) 120 multi_json (~> 1.0)
@@ -326,6 +337,7 @@ DEPENDENCIES @@ -326,6 +337,7 @@ DEPENDENCIES
326 kaminari 337 kaminari
327 launchy 338 launchy
328 letter_opener 339 letter_opener
  340 + linguist (~> 1.0.0)!
329 modularity 341 modularity
330 mysql2 342 mysql2
331 omniauth-ldap 343 omniauth-ldap
app/views/refs/_tree_file.html.haml
@@ -11,10 +11,10 @@ @@ -11,10 +11,10 @@
11 - if file.text? 11 - if file.text?
12 .view_file_content 12 .view_file_content
13 - unless file.empty? 13 - unless file.empty?
14 - %div{:class => current_user.dark_scheme ? "black" : "white"}  
15 - = preserve do  
16 - = raw file.colorize  
17 - - else 14 + %div{:class => current_user.dark_scheme ? "black" : "white"}
  15 + = preserve do
  16 + = raw file.colorize(options: { linenos: 'True'})
  17 + - else
18 %h3 18 %h3
19 %center Empty file 19 %center Empty file
20 - elsif file.image? 20 - elsif file.image?
app/views/snippets/show.html.haml
@@ -13,8 +13,7 @@ @@ -13,8 +13,7 @@
13 .view_file_header 13 .view_file_header
14 %strong= @snippet.file_name 14 %strong= @snippet.file_name
15 .view_file_content 15 .view_file_content
16 - %div{:class => current_user.dark_scheme ? "black" : ""}  
17 - :erb  
18 - <%= raw @snippet.colorize %> 16 + %div{:class => current_user.dark_scheme ? "black" : ""}
  17 + = raw @snippet.colorize(options: { linenos: 'True'})
19 18
20 = render "notes/notes", :tid => @snippet.id, :tt => "snippet" 19 = render "notes/notes", :tid => @snippet.id, :tt => "snippet"
config/initializers/gitlabhq/20_grit_ext.rb
1 require 'grit' 1 require 'grit'
2 -require 'pygments'  
3 -require "utils"  
4 2
5 Grit::Blob.class_eval do 3 Grit::Blob.class_eval do
6 - include Utils::FileHelper  
7 - include Utils::Colorize 4 + include Linguist::BlobHelper
8 end 5 end
9 6
10 #monkey patch raw_object from string 7 #monkey patch raw_object from string
@@ -15,7 +12,7 @@ Grit::GitRuby::Internal::RawObject.class_eval do @@ -15,7 +12,7 @@ Grit::GitRuby::Internal::RawObject.class_eval do
15 end 12 end
16 13
17 14
18 -Grit::Diff.class_eval do 15 +Grit::Diff.class_eval do
19 def old_path 16 def old_path
20 Gitlabhq::Encode.utf8 a_path 17 Gitlabhq::Encode.utf8 a_path
21 end 18 end
lib/utils.rb
@@ -1,44 +0,0 @@ @@ -1,44 +0,0 @@
1 -module Utils  
2 - module FileHelper  
3 - def binary?(string)  
4 - string.each_byte do |x|  
5 - x.nonzero? or return true  
6 - end  
7 - false  
8 - end  
9 -  
10 - def image?  
11 - mime_type =~ /image/  
12 - end  
13 -  
14 - def text?  
15 - mime_type =~ /application|text/ && !binary?(data)  
16 - end  
17 - end  
18 -  
19 - module Colorize  
20 - def colorize  
21 - system_colorize(data, name)  
22 - end  
23 -  
24 - def system_colorize(data, file_name)  
25 - options = { :encoding => 'utf-8', :linenos => 'True' }  
26 -  
27 - # Try detect language with pygments  
28 - Pygments.highlight data, :filename => file_name, :options => options  
29 - rescue  
30 - # if it fails use manual detection  
31 - ft = handle_file_type(file_name)  
32 - Pygments.highlight(data, :lexer => ft, :options => options)  
33 - end  
34 -  
35 - def handle_file_type(file_name)  
36 - case file_name  
37 - when /(\.ru|Gemfile)$/  
38 - :ruby  
39 - else  
40 - :text  
41 - end  
42 - end  
43 - end  
44 -end