Refactored Diff::Display classes
[gitorious:yousource.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   
3   def browse_path
4     project_repository_browse_path(@project, @repository)
5   end
6   
7   def log_path(args={})
8     project_repository_log_path(@project, @repository, args)
9   end
10   
11   def tree_path(sha1=nil, path=[])
12     project_repository_tree_path(@project, @repository, sha1, path)    
13   end
14   
15   def commit_path(sha1)
16     project_repository_commit_path(@project, @repository, sha1)    
17   end
18   
19   def blob_path(sha1, path)
20     project_repository_blob_path(@project, @repository, sha1, path)    
21   end
22   
23   def raw_blob_path(sha1, path)
24     project_repository_raw_blob_path(@project, @repository, sha1, path)
25   end
26   
27   def diff_path(sha1, other_sha1)
28     project_repository_diff_path(@project, @repository, sha1, other_sha1)    
29   end
30   
31   def current_path
32     params[:path].dup
33   end
34   
35   def build_tree_path(path)
36     current_path << path
37   end
38   
39   # def breadcrumb_path
40   #   out = %Q{<ul class="path_breadcrumbs">\n}
41   #   visited_path = []
42   #   out <<  %Q{  <li>/ #{link_to("root", tree_path(params[:sha], []))}</li>\n}
43   #   current_path.each_with_index do |path, index|
44   #     visited_path << path
45   #     out << %Q{  <li>/ #{link_to(path, tree_path(params[:sha], path))}</li>\n}
46   #   end
47   #   out << "</ul>"
48   #   out
49   # end
50     
51   def render_tag_box_if_match(sha, tags_per_sha)
52     tags = tags_per_sha[sha]
53     return if tags.blank?
54     out = ""
55     tags.each do |tagname|
56       out << %Q{<span class="tag"><code>}
57       out << tagname
58       out << %Q{</code></span>}
59     end
60     out
61   end  
62   
63   # Takes a unified diff as input and renders it as html
64   def render_diff(udiff, src_sha, dst_sha, display_mode = "inline")
65     return if udiff.blank?
66
67     case display_mode
68     when "sidebyside"
69       render_sidebyside_diff(udiff, src_sha, dst_sha)
70     else
71       render_inline_diff(udiff, src_sha, dst_sha)
72     end
73   end
74   
75   #diff = Diff::Display::Unified.new(load_diff("simple"))
76   #diff.render(Diff::Renderer::Base.new)
77   def render_inline_diff(udiff, src_sha, dst_sha)
78     differ = Diff::Display::Unified.new(udiff)
79     out = %Q{<table class="codediff inline">\n}
80     out << "<thead>\n"
81     out << "<tr>"
82     out << %Q{<td class="line-numbers">#{src_sha}</td>}
83     out << %Q{<td class="line-numbers">#{dst_sha}</td>}
84     out << "<td>&nbsp</td></tr>\n"
85     out << "</thead>\n"
86     out << differ.render(Gitorious::Diff::InlineTableCallback.new)
87     out << "</table>"
88     out
89   end
90   
91   def render_sidebyside_diff(udiff, src_sha, dst_sha)
92     differ = Diff::Display::Unified.new(udiff)
93     out = %Q{<table class="codediff sidebyside">\n}
94     out << %Q{<colgroup class="left"><col class="lines"/><col class="code"/></colgroup>}
95     out << %Q{<colgroup class="right"><col class="lines"/><col class="code"/></colgroup>}
96     out << %Q{<thead><th colspan="2">#{src_sha}</th>}
97     out << %Q{<th colspan="2">#{dst_sha}</th></thead>}
98     out << differ.render(Gitorious::Diff::SidebysideTableCallback.new)
99     out << "</table>"
100     out
101   end
102   
103   def render_diffmode_selector
104     out = %Q{<ul class="mode_selector">}
105     out << %Q{<li class="list_header">Diff rendering mode:</li>}
106     if @diffmode == "sidebyside"
107       out << %Q{<li><a href="?diffmode=inline">inline</a></li>}
108       out << %Q{<li class="selected">side by side</li>}
109     else
110       out << %Q{<li class="selected">inline</li>}
111       out << %Q{<li><a href="?diffmode=sidebyside">side by side</a></li>}
112     end      
113     out << "</ul>"
114     out
115   end
116   
117   def with_line_numbers(&block)
118     out = []
119     #yield.split("\n").each_with_index{ |s,i| out << "#{i+1}: #{s}" }
120     out << %Q{<table id="codeblob">}
121     yield.to_s.split("\n").each_with_index do |line, count|
122       lineno = count + 1
123       out << %Q{<tr id="line#{lineno}">}
124       out << %Q{<td class="line-numbers"><a href="#line#{lineno}" name="line#{lineno}">#{lineno}</a></td>} 
125       out << %Q{<td class="code">#{line}</td>}
126       out << "</tr>"
127     end
128     out << "</table>"
129     out.join("\n")
130     
131   end
132   
133 end