Commit 496be9eec2b721fdcd0fcf1c6ee2cef7a76f8f13

render blobs with line numbers

Commit diff

app/helpers/browse_helper.rb

 
112112 out
113113 end
114114
115 def with_line_numbers(&block)
116 out = []
117 #yield.split("\n").each_with_index{ |s,i| out << "#{i+1}: #{s}" }
118 out << %Q{<table>}
119 yield.to_s.split("\n").each_with_index do |line, count|
120 lineno = count + 1
121 out << "<tr>"
122 out << %Q{<td class="line-numbers"><a href="#line#{lineno}" name="line#{lineno}">#{lineno}</a></td>}
123 out << %Q{<td class="code">#{line}</td>}
124 out << "</tr>"
125 end
126 out << "</table>"
127 out.join("\n")
128
129 end
130
115131end
toggle raw diff

app/views/browse/blob.html.erb

 
33 Blob of <code><%= current_path.join("/") -%></code>
44 <small>(<%= link_to "raw blob data", raw_blob_path(@blob.sha, current_path) -%>)</small>
55</h1>
6<pre><%=h @blob.contents -%></pre>
6<pre><%= with_line_numbers{ h(@blob.contents) } -%></pre>
77
88<%= render :partial => "submenu" -%>
toggle raw diff

spec/helpers/browse_helper_spec.rb

 
6868 build_tree_path("three").should == ["one", "two", "three"]
6969 end
7070
71 describe "with_line_numbers" do
72 it "renders something with line numbers" do
73 numbered = with_line_numbers { "foo\nbar\nbaz" }
74 numbered.should include(%Q{<td class="line-numbers"><a href="#line2" name="line2">2</a></td>})
75 numbered.should include(%Q{<td class="code">bar</td>})
76 end
77
78 it "renders one line with line numbers" do
79 numbered = with_line_numbers { "foo" }
80 numbered.should include(%Q{<td class="line-numbers"><a href="#line1" name="line1">1</a></td>})
81 numbered.should include(%Q{<td class="code">foo</td>})
82 end
83
84 it "doesn't blow up when with_line_numbers receives nil" do
85 proc{
86 with_line_numbers{ nil }.should == "<table>\n</table>"
87 }.should_not raise_error
88 end
89 end
90
7191 # it "builds breadcrumbs of the current_path" do
7292 # stub!(:current_path).and_return(["one", "two", "tree"])
7393 # breadcrumb_path.should include(%Q{<ul class="path_breadcrumbs">})
toggle raw diff