1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# encoding: utf-8
#--
# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
# Copyright (C) 2008 Johan Sørensen <johan@johansorensen.com>
# Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
module BlobsHelper
include RepositoriesHelper
include TreesHelper
HIGHLIGHTER_TO_EXT = {
"list" => /\.(lisp|cl|l|mud|el|clj)$/,
"hs" => /\.hs$/,
"css" => /\.css$/,
"lua" => /\.lua$/,
"ml" => /\.(ml|mli)$/,
"proto" => /\.proto$/,
"sql" => /\.(sql|ddl|dml)$/,
"vb" => /\.vb$/,
"wiki" => /\.(mediawiki|wikipedia|wiki)$/,
}
ASCII_MIME_TYPES_EXCEPTIONS = [ /^text/ ]
def textual?(blob)
!binary?(blob)
end
def binary?(blob)
blob.binary?
end
def image?(blob)
blob.mime_type =~ /^image/
end
def highlightable?(blob)
if File.extname(blob.name) == ""
return false
end
if %w[.txt .textile .md .rdoc .markdown].include?(File.extname(blob.name))
return false
end
true
end
def language_of_file(filename)
if lang_tuple = HIGHLIGHTER_TO_EXT.find{|lang, matcher| filename =~ matcher }
return lang_tuple.first
end
end
def render_highlighted(text, filename, code_theme_class = nil)
out = []
lang_class = "lang" + File.extname(filename).sub('.', '-')
out << %Q{<table id="codeblob" class="highlighted #{lang_class}">}
text.to_s.split("\n").each_with_index do |line, count|
lineno = count + 1
out << %Q{<tr id="line#{lineno}">}
out << %Q{<td class="line-numbers"><a href="#line#{lineno}" name="line#{lineno}">#{lineno}</a></td>}
code_classes = "code"
code_classes << " #{code_theme_class}" if code_theme_class
ext = File.extname(filename).sub(/^\./, '')
out << %Q{<td class="#{code_classes}"><pre class="prettyprint lang-#{ext}">#{h(line)}</pre></td>}
out << "</tr>"
end
out << "</table>"
out.join("\n")
end
def too_big_to_render?(size)
size > 350.kilobytes
end
end
|