Commit 5b6019f3 authored by Arthur Schreiber's avatar Arthur Schreiber

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

parent 9ee34575
...@@ -18,6 +18,7 @@ gem "six" ...@@ -18,6 +18,7 @@ gem "six"
gem "therubyracer" gem "therubyracer"
gem "faker" gem "faker"
gem "seed-fu" gem "seed-fu"
gem "linguist", "~> 1.0.0", :git => "https://github.com/github/linguist.git"
gem "pygments.rb", "0.2.7" gem "pygments.rb", "0.2.7"
gem "thin" gem "thin"
gem "unicorn" gem "unicorn"
......
...@@ -4,6 +4,16 @@ GIT ...@@ -4,6 +4,16 @@ GIT
specs: specs:
annotate (2.4.1.beta1) annotate (2.4.1.beta1)
GIT
remote: https://github.com/github/linguist.git
revision: c444c25b27131cd2aca6017f2a9ce5eae60dfcd3
specs:
linguist (1.0.0)
charlock_holmes (~> 0.6.6)
escape_utils (~> 0.2.3)
mime-types (~> 1.18)
pygments.rb (~> 0.2.11)
GIT GIT
remote: https://github.com/gitlabhq/gitolite-client.git remote: https://github.com/gitlabhq/gitolite-client.git
revision: 36dabd226caa40ff052677719adaacbfe667b36c revision: 36dabd226caa40ff052677719adaacbfe667b36c
...@@ -104,6 +114,7 @@ GEM ...@@ -104,6 +114,7 @@ GEM
diff-lcs (1.1.3) diff-lcs (1.1.3)
drapper (0.8.4) drapper (0.8.4)
erubis (2.7.0) erubis (2.7.0)
escape_utils (0.2.4)
eventmachine (0.12.10) eventmachine (0.12.10)
execjs (1.3.0) execjs (1.3.0)
multi_json (~> 1.0) multi_json (~> 1.0)
...@@ -326,6 +337,7 @@ DEPENDENCIES ...@@ -326,6 +337,7 @@ DEPENDENCIES
kaminari kaminari
launchy launchy
letter_opener letter_opener
linguist (~> 1.0.0)!
modularity modularity
mysql2 mysql2
omniauth-ldap omniauth-ldap
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
- unless file.empty? - unless file.empty?
%div{:class => current_user.dark_scheme ? "black" : "white"} %div{:class => current_user.dark_scheme ? "black" : "white"}
= preserve do = preserve do
= raw file.colorize = raw file.colorize(options: { linenos: 'True'})
- else - else
%h3 %h3
%center Empty file %center Empty file
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
%strong= @snippet.file_name %strong= @snippet.file_name
.view_file_content .view_file_content
%div{:class => current_user.dark_scheme ? "black" : ""} %div{:class => current_user.dark_scheme ? "black" : ""}
:erb = raw @snippet.colorize(options: { linenos: 'True'})
<%= raw @snippet.colorize %>
= render "notes/notes", :tid => @snippet.id, :tt => "snippet" = render "notes/notes", :tid => @snippet.id, :tt => "snippet"
require 'grit' require 'grit'
require 'pygments'
require "utils"
Grit::Blob.class_eval do Grit::Blob.class_eval do
include Utils::FileHelper include Linguist::BlobHelper
include Utils::Colorize
end end
#monkey patch raw_object from string #monkey patch raw_object from string
......
module Utils
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)
options = { :encoding => 'utf-8', :linenos => 'True' }
# Try detect language with pygments
Pygments.highlight data, :filename => file_name, :options => options
rescue
# if it fails use manual detection
ft = handle_file_type(file_name)
Pygments.highlight(data, :lexer => ft, :options => options)
end
def handle_file_type(file_name)
case file_name
when /(\.ru|Gemfile)$/
:ruby
else
:text
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment