Commit 5b6019f357d01d7871068da15549d19be1a0cf4f

Authored by Arthur Schreiber
1 parent 9ee34575

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

Gemfile
... ... @@ -18,6 +18,7 @@ gem "six"
18 18 gem "therubyracer"
19 19 gem "faker"
20 20 gem "seed-fu"
  21 +gem "linguist", "~> 1.0.0", :git => "https://github.com/github/linguist.git"
21 22 gem "pygments.rb", "0.2.7"
22 23 gem "thin"
23 24 gem "unicorn"
... ...
Gemfile.lock
... ... @@ -5,6 +5,16 @@ GIT
5 5 annotate (2.4.1.beta1)
6 6  
7 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 18 remote: https://github.com/gitlabhq/gitolite-client.git
9 19 revision: 36dabd226caa40ff052677719adaacbfe667b36c
10 20 specs:
... ... @@ -104,6 +114,7 @@ GEM
104 114 diff-lcs (1.1.3)
105 115 drapper (0.8.4)
106 116 erubis (2.7.0)
  117 + escape_utils (0.2.4)
107 118 eventmachine (0.12.10)
108 119 execjs (1.3.0)
109 120 multi_json (~> 1.0)
... ... @@ -326,6 +337,7 @@ DEPENDENCIES
326 337 kaminari
327 338 launchy
328 339 letter_opener
  340 + linguist (~> 1.0.0)!
329 341 modularity
330 342 mysql2
331 343 omniauth-ldap
... ...
app/views/refs/_tree_file.html.haml
... ... @@ -11,10 +11,10 @@
11 11 - if file.text?
12 12 .view_file_content
13 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 18 %h3
19 19 %center Empty file
20 20 - elsif file.image?
... ...
app/views/snippets/show.html.haml
... ... @@ -13,8 +13,7 @@
13 13 .view_file_header
14 14 %strong= @snippet.file_name
15 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 19 = render "notes/notes", :tid => @snippet.id, :tt => "snippet"
... ...
config/initializers/gitlabhq/20_grit_ext.rb
1 1 require 'grit'
2   -require 'pygments'
3   -require "utils"
4 2  
5 3 Grit::Blob.class_eval do
6   - include Utils::FileHelper
7   - include Utils::Colorize
  4 + include Linguist::BlobHelper
8 5 end
9 6  
10 7 #monkey patch raw_object from string
... ... @@ -15,7 +12,7 @@ Grit::GitRuby::Internal::RawObject.class_eval do
15 12 end
16 13  
17 14  
18   -Grit::Diff.class_eval do
  15 +Grit::Diff.class_eval do
19 16 def old_path
20 17 Gitlabhq::Encode.utf8 a_path
21 18 end
... ...
lib/utils.rb
... ... @@ -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