Commit 496be9eec2b721fdcd0fcf1c6ee2cef7a76f8f13
- Date: Mon Jan 14 23:24:46 +0000 2008
- Committer: Johan Sørensen (johan@johansorensen.com)
- Author: Johan Sørensen (johan@johansorensen.com)
- Commit SHA1: 496be9eec2b721fdcd0fcf1c6ee2cef7a76f8f13
- Tree SHA1: 33e4190eb3937541fbb903f0322f237673890849
render blobs with line numbers
Commit diff
| |   |
| 112 | 112 | out |
| 113 | 113 | end |
| 114 | 114 | |
| 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 | |
| 115 | 131 | end |
| toggle raw diff |
--- a/app/helpers/browse_helper.rb
+++ b/app/helpers/browse_helper.rb
@@ -112,4 +112,20 @@ module BrowseHelper
out
end
+ def with_line_numbers(&block)
+ out = []
+ #yield.split("\n").each_with_index{ |s,i| out << "#{i+1}: #{s}" }
+ out << %Q{<table>}
+ yield.to_s.split("\n").each_with_index do |line, count|
+ lineno = count + 1
+ out << "<tr>"
+ out << %Q{<td class="line-numbers"><a href="#line#{lineno}" name="line#{lineno}">#{lineno}</a></td>}
+ out << %Q{<td class="code">#{line}</td>}
+ out << "</tr>"
+ end
+ out << "</table>"
+ out.join("\n")
+
+ end
+
end |
| |   |
| 3 | 3 | Blob of <code><%= current_path.join("/") -%></code> |
| 4 | 4 | <small>(<%= link_to "raw blob data", raw_blob_path(@blob.sha, current_path) -%>)</small> |
| 5 | 5 | </h1> |
| 6 | | <pre><%=h @blob.contents -%></pre> |
| 6 | <pre><%= with_line_numbers{ h(@blob.contents) } -%></pre> |
| 7 | 7 | |
| 8 | 8 | <%= render :partial => "submenu" -%> |
| toggle raw diff |
--- a/app/views/browse/blob.html.erb
+++ b/app/views/browse/blob.html.erb
@@ -3,6 +3,6 @@
Blob of <code><%= current_path.join("/") -%></code>
<small>(<%= link_to "raw blob data", raw_blob_path(@blob.sha, current_path) -%>)</small>
</h1>
-<pre><%=h @blob.contents -%></pre>
+<pre><%= with_line_numbers{ h(@blob.contents) } -%></pre>
<%= render :partial => "submenu" -%> |
| |   |
| 68 | 68 | build_tree_path("three").should == ["one", "two", "three"] |
| 69 | 69 | end |
| 70 | 70 | |
| 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 | |
| 71 | 91 | # it "builds breadcrumbs of the current_path" do |
| 72 | 92 | # stub!(:current_path).and_return(["one", "two", "tree"]) |
| 73 | 93 | # breadcrumb_path.should include(%Q{<ul class="path_breadcrumbs">}) |
| toggle raw diff |
--- a/spec/helpers/browse_helper_spec.rb
+++ b/spec/helpers/browse_helper_spec.rb
@@ -68,6 +68,26 @@ describe BrowseHelper do
build_tree_path("three").should == ["one", "two", "three"]
end
+ describe "with_line_numbers" do
+ it "renders something with line numbers" do
+ numbered = with_line_numbers { "foo\nbar\nbaz" }
+ numbered.should include(%Q{<td class="line-numbers"><a href="#line2" name="line2">2</a></td>})
+ numbered.should include(%Q{<td class="code">bar</td>})
+ end
+
+ it "renders one line with line numbers" do
+ numbered = with_line_numbers { "foo" }
+ numbered.should include(%Q{<td class="line-numbers"><a href="#line1" name="line1">1</a></td>})
+ numbered.should include(%Q{<td class="code">foo</td>})
+ end
+
+ it "doesn't blow up when with_line_numbers receives nil" do
+ proc{
+ with_line_numbers{ nil }.should == "<table>\n</table>"
+ }.should_not raise_error
+ end
+ end
+
# it "builds breadcrumbs of the current_path" do
# stub!(:current_path).and_return(["one", "two", "tree"])
# breadcrumb_path.should include(%Q{<ul class="path_breadcrumbs">}) |