Skip to content

Commit 7d0f901

Browse files
Add short_name helper
Similar to the `full_name` helper, the `short_name` helper escapes its input and wraps the result in a `<code>` tag. But, whereas `full_name` will call `RDoc::CodeObject#full_name` when given an `RDoc::CodeObject`, `short_name` will call `RDoc::CodeObject#name`. This commit also changes the `link_to` helper to no longer escape its input text. Thus it can be used together with the `short_name` helper.
1 parent b2ad389 commit 7d0f901

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

lib/rdoc/generator/template/rails/_context.rhtml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="sectiontitle">Sections</div>
2727
<ul>
2828
<% sections.each do |section| %>
29-
<li><%= link_to section.title, "##{section.aref}" %></li>
29+
<li><%= link_to h(section.title), "##{section.aref}" %></li>
3030
<% end %>
3131
</ul>
3232
<% end %>
@@ -43,7 +43,7 @@
4343
<%
4444
comma = methods.size == i+1 ? '' : ','
4545
%>
46-
<li><%= link_to method.name, method %><%= comma %></li>
46+
<li><%= link_to short_name(method), method %><%= comma %></li>
4747
<% end %>
4848
</ul>
4949
</dd>
@@ -89,7 +89,7 @@
8989
<table border='0' cellpadding='5'>
9090
<% constants.each do |const| %>
9191
<tr valign='top'>
92-
<td class="attr-name"><%= h const.name %></td>
92+
<td class="attr-name"><%= short_name const %></td>
9393
<td>=</td>
9494
<td class="attr-value"><%= h const.value %></td>
9595
</tr>
@@ -113,7 +113,7 @@
113113
<td class='attr-rw'>
114114
[<%= attrib.rw %>]
115115
</td>
116-
<td class='attr-name'><%= h attrib.name %></td>
116+
<td class='attr-name'><%= short_name attrib %></td>
117117
<td class='attr-desc'><%= attrib.description.strip %></td>
118118
</tr>
119119
<% end %>
@@ -144,14 +144,14 @@
144144
<p class="aka">
145145
Also aliased as:
146146
<%# Sometimes a parent cannot be determined. See ruby/rdoc@85ebfe13dc. %>
147-
<%= method.aliases.map { |aka| link_to_if aka.parent, aka.name, aka }.join(", ") %>.
147+
<%= method.aliases.map { |aka| link_to_if aka.parent, short_name(aka), aka }.join(", ") %>.
148148
</p>
149149
<% end %>
150150

151151
<% if alias_for = method.is_alias_for then %>
152152
<p class="aka">
153153
Alias for:
154-
<%= link_to alias_for.name, alias_for %>.
154+
<%= link_to short_name(alias_for), alias_for %>.
155155
</p>
156156
<% end %>
157157

lib/sdoc/helpers.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _link_url(url)
1515
end
1616

1717
def _link_body(text)
18-
text.is_a?(RDoc::CodeObject) ? full_name(text) : h(text)
18+
text.is_a?(RDoc::CodeObject) ? full_name(text) : text
1919
end
2020

2121
def link_to_if(condition, text, *args)
@@ -35,6 +35,11 @@ def full_name(named)
3535
"<code>#{named.split(%r"(?<=./|.::)").map { |part| h part }.join("<wbr>")}</code>"
3636
end
3737

38+
def short_name(named)
39+
named = named.name if named.is_a?(RDoc::CodeObject)
40+
"<code>#{h named}</code>"
41+
end
42+
3843
def base_tag_for_context(context)
3944
relative_root = "../" * context.path.count("/")
4045
%(<base href="./#{relative_root}" data-current-path="#{context.path}">)

spec/helpers_spec.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,14 @@
8686
must_equal %(<a href="bar" class="qux" data-hoge="fuga">foo</a>)
8787
end
8888

89-
it "escapes the link text and attributes" do
90-
_(@helpers.link_to("Bar < Foo", "qux", title: "Foo > Bar")).
91-
must_equal %(<a href="qux" title="Foo &gt; Bar">Bar &lt; Foo</a>)
89+
it "escapes the HTML attributes" do
90+
_(@helpers.link_to("Foo", "foo", title: "Foo < Object")).
91+
must_equal %(<a href="foo" title="Foo &lt; Object">Foo</a>)
92+
end
93+
94+
it "does not escape the link body" do
95+
_(@helpers.link_to("<code>Foo</code>", "foo")).
96+
must_equal %(<a href="foo"><code>Foo</code></a>)
9297
end
9398

9499
it "uses the first argument as the URL when no URL is specified" do
@@ -134,12 +139,12 @@ module Foo; class Bar; def qux; end; end; end
134139

135140
describe "#link_to_if" do
136141
it "returns the link's HTML when the condition is true" do
137-
args = ["Bar < Foo", "qux", title: "Foo > Bar"]
142+
args = ["<code>Foo</code>", "foo", title: "Foo < Object"]
138143
_(@helpers.link_to_if(true, *args)).must_equal @helpers.link_to(*args)
139144
end
140145

141146
it "returns the link's inner HTML when the condition is false" do
142-
_(@helpers.link_to_if(false, "Bar < Foo", "url")).must_equal ERB::Util.h("Bar < Foo")
147+
_(@helpers.link_to_if(false, "<code>Foo</code>", "url")).must_equal "<code>Foo</code>"
143148

144149
rdoc_module = rdoc_top_level_for(<<~RUBY).find_module_named("Foo::Bar")
145150
module Foo; class Bar; end; end
@@ -199,6 +204,24 @@ module Foo; module Bar; class Qux; end; end; end
199204
end
200205
end
201206

207+
describe "#short_name" do
208+
it "wraps name in <code>" do
209+
_(@helpers.short_name("foo")).must_equal "<code>foo</code>"
210+
end
211+
212+
it "escapes the name" do
213+
_(@helpers.short_name("<=>")).must_equal "<code>&lt;=&gt;</code>"
214+
end
215+
216+
it "uses RDoc::CodeObject#name when argument is an RDoc::CodeObject" do
217+
rdoc_method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false)
218+
module Foo; def bar; end; end
219+
RUBY
220+
221+
_(@helpers.short_name(rdoc_method)).must_equal "<code>bar</code>"
222+
end
223+
end
224+
202225
describe "#base_tag_for_context" do
203226
it "returns a <base> tag with an appropriate path for the given RDoc::Context" do
204227
top_level = rdoc_top_level_for <<~RUBY

0 commit comments

Comments
 (0)