forked from dim/retrospectiva
/
browse_helper.rb
91 lines (78 loc) · 2.87 KB
/
browse_helper.rb
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
89
90
91
module BrowseHelper
include DiffHelper
include RepositoriesHelper
def browseable_path(last_element_clickable = true)
joined_path = []
path_links = params[:path].map do |token|
joined_path << token.dup
if params[:path].last == token && !last_element_clickable
token
else
link_to_browse token, joined_path, params[:rev]
end
end
path_links.unshift link_to_browse('root', [], params[:rev])
content_tag('h2', path_links.join('/'), :class => 'browseable-path')
end
def format_properties(property_hash)
property_hash.keys.sort_by(&:to_s).map do |name|
"<em class=\"loud\">#{h(name)}</em>: #{h(property_hash[name])}"
end.join(', ')
end
def format_code_with_line_numbers(node)
content = syntax_highlight(node)
content.force_encoding('utf-8') if RUBY_VERSION.to_f >= 1.9
lines, code = [], []
num = 0
content.each_line do |line|
lines << link_to_code_line(num += 1)
code << line.gsub(/\r?\n/, '')
end
content_tag :table, %Q(
<colgroup><col class="code-line"/><col class="code-full"/></colgroup>
<tbody>
<tr>
<th><pre>#{lines.join("\n")}</pre></th>
<td><pre>#{code.join("\n")}</pre></td>
</tr>
</tbody>
), :class => 'code'
end
def link_to_diff(label, current_node, path_tokens, revision)
if current_node.dir? || current_node.binary?
' '
elsif revision == current_node.revision
"<em>#{_('Current')}</em>"
else
link_to label,
project_diff_path(Project.current, path_tokens, :rev => current_node.revision, :compare_with => revision),
:title => _('Compare [{{revision_a}}] with [{{revision_b}}]', :revision_a => current_node.revision, :revision_b => revision)
end
end
def link_to_revisions
link_to _('Revisions'), project_revisions_path(Project.current, params[:path], :rev => params[:rev])
end
def node_download_links(*formats)
[formats].flatten.uniq.map do |format|
label, path = case format
when :raw
[ _('Raw'), project_download_path(Project.current, params[:path], :rev => params[:rev]) ]
when :text
[ _('Text'), project_browse_path(Project.current, params[:path], :rev => params[:rev], :format => 'text') ]
end
label && path ? link_to(label, path) : nil
end.compact.join(' | ')
end
protected
def link_to_code_line(line_number)
anchor = "ln#{line_number}"
link_to line_number.to_s,
project_browse_path(Project.current, params[:path], :rev => @node.selected_revision, :anchor => anchor),
:title => _('Line {{number}}', :number => line_number),
:class => 'block', :id => anchor
end
def syntax_highlight(node)
syntax = CodeRay::FileType.fetch node.path, :plaintext, false
CodeRay.scan(node.content, syntax).html
end
end