Commit c463eeb0908e5c00008073c07e71e06434393342

Authored by gitlabhq
1 parent 48924dfe

refactoring

app/helpers/application_helper.rb
... ... @@ -53,25 +53,4 @@ module ApplicationHelper
53 53 [projects, default_nav, project_nav].flatten.to_json
54 54 end
55 55  
56   - def handle_file_type(file_name, mime_type = nil)
57   - if file_name =~ /(\.rb|\.ru|\.rake|Rakefile|\.gemspec|\.rbx|Gemfile)$/
58   - :ruby
59   - elsif file_name =~ /\.py$/
60   - :python
61   - elsif file_name =~ /(\.pl|\.scala|\.c|\.cpp|\.java|\.haml|\.html|\.sass|\.scss|\.xml|\.php|\.erb)$/
62   - $1[1..-1].to_sym
63   - elsif file_name =~ /\.js$/
64   - :javascript
65   - elsif file_name =~ /\.sh$/
66   - :bash
67   - elsif file_name =~ /\.coffee$/
68   - :coffeescript
69   - elsif file_name =~ /\.yml$/
70   - :yaml
71   - elsif file_name =~ /\.md$/
72   - :minid
73   - else
74   - :text
75   - end
76   - end
77 56 end
... ...
app/models/snippet.rb
1 1 class Snippet < ActiveRecord::Base
  2 + include Utils::Colorize
  3 +
2 4 belongs_to :project
3 5 belongs_to :author, :class_name => "User"
4 6 has_many :notes, :as => :noteable
... ... @@ -28,6 +30,11 @@ class Snippet &lt; ActiveRecord::Base
28 30 ".js", ".sh", ".coffee", ".yml", ".md"
29 31 ]
30 32 end
  33 +
  34 + def colorize
  35 + ft = handle_file_type(file_name)
  36 + Albino.colorize(content, ft, :html, 'utf-8', "linenos=True")
  37 + end
31 38 end
32 39 # == Schema Information
33 40 #
... ...
app/views/commits/_diff.html.haml
1   -- require "utils"
2 1 .file_stats
3 2 - @commit.diffs.each do |diff|
4 3 - if diff.deleted_file
... ... @@ -35,7 +34,7 @@
35 34 %strong{:id => "#{diff.b_path}"}= diff.b_path
36 35 %br/
37 36 .diff_file_content
38   - - if file.mime_type =~ /application|text/ && !Utils.binary?(file.data)
  37 + - if file.text?
39 38 - lines_arr = diff.diff.lines.to_a
40 39 - line_old = lines_arr[2].match(/-(\d)/)[0].to_i.abs rescue 0
41 40 - line_new = lines_arr[2].match(/\+(\d)/)[0].to_i.abs rescue 0
... ... @@ -50,9 +49,9 @@
50 49 - else
51 50 - line_new += 1
52 51 - line_old += 1
53   - - elsif file.mime_type =~ /image/
  52 + - elsif file.image?
54 53 .diff_file_content_image
55   - %img{:src => "data:image/jpeg;base64,#{Base64.encode64(file.data)}"}
  54 + %img{:src => "data:#{file.mime_type};base64,#{Base64.encode64(file.data)}"}
56 55 - else
57 56 %p
58 57 %center No preview for this file type
... ...
app/views/projects/_tree_file.html.haml
1   -- require "utils"
2 1 .view_file
3 2 .view_file_header
4 3 %strong
... ... @@ -6,14 +5,13 @@
6 5 = link_to "raw", blob_project_path(@project, :commit_id => @commit.id, :path => params[:path] ), :class => "right", :target => "_blank"
7 6 = link_to "history", project_commits_path(@project, :path => params[:path]), :class => "right", :style => "margin-right:10px;"
8 7 %br/
9   - - if file.mime_type =~ /application|text/ && !Utils.binary?(file.data)
  8 + - if file.text?
10 9 .view_file_content
11   - - ft = handle_file_type(file.name, file.mime_type)
12 10 :erb
13   - <%= raw Albino.colorize(content, ft, :html, 'utf-8', "linenos=True") %>
14   - - elsif file.mime_type =~ /image/
  11 + <%= raw file.colorize %>
  12 + - elsif file.image?
15 13 .view_file_content_image
16   - %img{ :src => "data:image/jpeg;base64,#{Base64.encode64(file.data)}"}
  14 + %img{ :src => "data:#{file.mime_type};base64,#{Base64.encode64(file.data)}"}
17 15 - else
18 16 %p
19 17 %center No preview for this file type
... ...
app/views/snippets/show.html.haml
... ... @@ -7,9 +7,8 @@
7 7 = @snippet.file_name
8 8 %br/
9 9 .view_file_content
10   - - ft = handle_file_type(@snippet.file_name)
11 10 :erb
12   - <%= raw Albino.colorize(@snippet.content, ft, :html, 'utf-8', "linenos=True") %>
  11 + <%= raw @snippet.colorize %>
13 12  
14 13 - if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user
15 14 = link_to 'Edit', edit_project_snippet_path(@project, @snippet), :class => "lbutton positive"
... ...
config/initializers/grit_ext.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +require 'grit'
  2 +require 'albino'
  3 +require "utils"
  4 +
  5 +Grit::Blob.class_eval do
  6 + include Utils::FileHelper
  7 + include Utils::Colorize
  8 +end
... ...
lib/utils.rb
1 1 module Utils
2   - def self.binary?(string)
3   - string.each_byte do |x|
4   - x.nonzero? or return true
  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 + ft = handle_file_type(name, mime_type)
  22 + Albino.colorize(data, ft, :html, 'utf-8', "linenos=True")
  23 + end
  24 +
  25 + def handle_file_type(file_name, mime_type = nil)
  26 + if file_name =~ /(\.rb|\.ru|\.rake|Rakefile|\.gemspec|\.rbx|Gemfile)$/
  27 + :ruby
  28 + elsif file_name =~ /\.py$/
  29 + :python
  30 + elsif file_name =~ /(\.pl|\.scala|\.c|\.cpp|\.java|\.haml|\.html|\.sass|\.scss|\.xml|\.php|\.erb)$/
  31 + $1[1..-1].to_sym
  32 + elsif file_name =~ /\.js$/
  33 + :javascript
  34 + elsif file_name =~ /\.sh$/
  35 + :bash
  36 + elsif file_name =~ /\.coffee$/
  37 + :coffeescript
  38 + elsif file_name =~ /\.yml$/
  39 + :yaml
  40 + elsif file_name =~ /\.md$/
  41 + :minid
  42 + else
  43 + :text
  44 + end
5 45 end
6   - false
7 46 end
8 47 end
... ...