Skip to content

Commit 3f1cd4d

Browse files
authored
Merge pull request #660 from nobu/feature/link-to-code
Mark up code inside link text as <code>
2 parents 06bca95 + b1bba63 commit 3f1cd4d

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,19 @@ def initialize(options, from_path, context, markup = nil)
4949
# Creates a link to the reference +name+ if the name exists. If +text+ is
5050
# given it is used as the link text, otherwise +name+ is used.
5151

52-
def cross_reference name, text = nil
52+
def cross_reference name, text = nil, code = true
5353
lookup = name
5454

5555
name = name[1..-1] unless @show_hash if name[0, 1] == '#'
5656

57-
name = "#{CGI.unescape $'} at #{$1}" if name =~ /(.*[^#:])@/
58-
59-
text = name unless text
57+
if name =~ /(.*[^#:])@/
58+
text ||= "#{CGI.unescape $'} at <code>#{$1}</code>"
59+
code = false
60+
else
61+
text ||= name
62+
end
6063

61-
link lookup, text
64+
link lookup, text, code
6265
end
6366

6467
##
@@ -119,13 +122,14 @@ def handle_regexp_RDOCLINK target
119122
def gen_url url, text
120123
return super unless url =~ /\Ardoc-ref:/
121124

122-
cross_reference $', text
125+
name = $'
126+
cross_reference name, text, name == text
123127
end
124128

125129
##
126130
# Creates an HTML link to +name+ with the given +text+.
127131

128-
def link name, text
132+
def link name, text, code = true
129133
if name =~ /(.*[^#:])@/ then
130134
name = $1
131135
label = $'
@@ -139,6 +143,10 @@ def link name, text
139143
else
140144
path = ref.as_href @from_path
141145

146+
if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref)
147+
text = "<code>#{text}</code>"
148+
end
149+
142150
if path =~ /#/ then
143151
path << "-label-#{label}"
144152
elsif ref.sections and

test/test_rdoc_markup_to_html_crossref.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,58 @@ def setup
1414
def test_convert_CROSSREF
1515
result = @to.convert 'C1'
1616

17-
assert_equal para("<a href=\"C1.html\">C1</a>"), result
17+
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
1818
end
1919

2020
def test_convert_CROSSREF_label
2121
result = @to.convert 'C1@foo'
22-
assert_equal para("<a href=\"C1.html#label-foo\">foo at C1</a>"), result
22+
assert_equal para("<a href=\"C1.html#label-foo\">foo at <code>C1</code></a>"), result
2323

2424
result = @to.convert 'C1#m@foo'
25-
assert_equal para("<a href=\"C1.html#method-i-m-label-foo\">foo at C1#m</a>"),
25+
assert_equal para("<a href=\"C1.html#method-i-m-label-foo\">foo at <code>C1#m</code></a>"),
2626
result
2727
end
2828

2929
def test_convert_CROSSREF_label_period
3030
result = @to.convert 'C1@foo.'
31-
assert_equal para("<a href=\"C1.html#label-foo\">foo at C1</a>."), result
31+
assert_equal para("<a href=\"C1.html#label-foo\">foo at <code>C1</code></a>."), result
3232
end
3333

3434
def test_convert_CROSSREF_label_space
3535
result = @to.convert 'C1@foo+bar'
36-
assert_equal para("<a href=\"C1.html#label-foo+bar\">foo bar at C1</a>"),
36+
assert_equal para("<a href=\"C1.html#label-foo+bar\">foo bar at <code>C1</code></a>"),
3737
result
3838
end
3939

4040
def test_convert_CROSSREF_section
4141
@c1.add_section 'Section'
4242

4343
result = @to.convert 'C1@Section'
44-
assert_equal para("<a href=\"C1.html#Section\">Section at C1</a>"), result
44+
assert_equal para("<a href=\"C1.html#Section\">Section at <code>C1</code></a>"), result
4545
end
4646

4747
def test_convert_CROSSREF_constant
4848
result = @to.convert 'C1::CONST'
4949

50-
assert_equal para("<a href=\"C1.html#CONST\">C1::CONST</a>"), result
50+
assert_equal para("<a href=\"C1.html#CONST\"><code>C1::CONST</code></a>"), result
5151
end
5252

5353
def test_convert_RDOCLINK_rdoc_ref
5454
result = @to.convert 'rdoc-ref:C1'
5555

56-
assert_equal para("<a href=\"C1.html\">C1</a>"), result
56+
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
5757
end
5858

5959
def test_convert_RDOCLINK_rdoc_ref_method
6060
result = @to.convert 'rdoc-ref:C1#m'
6161

62-
assert_equal para("<a href=\"C1.html#method-i-m\">C1#m</a>"), result
62+
assert_equal para("<a href=\"C1.html#method-i-m\"><code>C1#m</code></a>"), result
6363
end
6464

6565
def test_convert_RDOCLINK_rdoc_ref_method_label
6666
result = @to.convert 'rdoc-ref:C1#m@foo'
6767

68-
assert_equal para("<a href=\"C1.html#method-i-m-label-foo\">foo at C1#m</a>"),
68+
assert_equal para("<a href=\"C1.html#method-i-m-label-foo\">foo at <code>C1#m</code></a>"),
6969
result, 'rdoc-ref:C1#m@foo'
7070
end
7171

@@ -75,13 +75,13 @@ def test_convert_RDOCLINK_rdoc_ref_method_percent
7575

7676
result = @to.convert 'rdoc-ref:C1#%'
7777

78-
assert_equal para("<a href=\"C1.html#method-i-25\">C1#%</a>"), result
78+
assert_equal para("<a href=\"C1.html#method-i-25\"><code>C1#%</code></a>"), result
7979

8080
m.singleton = true
8181

8282
result = @to.convert 'rdoc-ref:C1::%'
8383

84-
assert_equal para("<a href=\"C1.html#method-c-25\">C1::%</a>"), result
84+
assert_equal para("<a href=\"C1.html#method-c-25\"><code>C1::%</code></a>"), result
8585
end
8686

8787
def test_convert_RDOCLINK_rdoc_ref_method_percent_label
@@ -90,21 +90,21 @@ def test_convert_RDOCLINK_rdoc_ref_method_percent_label
9090

9191
result = @to.convert 'rdoc-ref:C1#%@f'
9292

93-
assert_equal para("<a href=\"C1.html#method-i-25-label-f\">f at C1#%</a>"),
93+
assert_equal para("<a href=\"C1.html#method-i-25-label-f\">f at <code>C1#%</code></a>"),
9494
result
9595

9696
m.singleton = true
9797

9898
result = @to.convert 'rdoc-ref:C1::%@f'
9999

100-
assert_equal para("<a href=\"C1.html#method-c-25-label-f\">f at C1::%</a>"),
100+
assert_equal para("<a href=\"C1.html#method-c-25-label-f\">f at <code>C1::%</code></a>"),
101101
result
102102
end
103103

104104
def test_convert_RDOCLINK_rdoc_ref_label
105105
result = @to.convert 'rdoc-ref:C1@foo'
106106

107-
assert_equal para("<a href=\"C1.html#label-foo\">foo at C1</a>"), result,
107+
assert_equal para("<a href=\"C1.html#label-foo\">foo at <code>C1</code></a>"), result,
108108
'rdoc-ref:C1@foo'
109109
end
110110

@@ -117,18 +117,18 @@ def test_gen_url
117117
end
118118

119119
def test_handle_regexp_CROSSREF
120-
assert_equal "<a href=\"C2/C3.html\">C2::C3</a>", REGEXP_HANDLING('C2::C3')
120+
assert_equal "<a href=\"C2/C3.html\"><code>C2::C3</code></a>", REGEXP_HANDLING('C2::C3')
121121
end
122122

123123
def test_handle_regexp_CROSSREF_label
124-
assert_equal "<a href=\"C1.html#method-i-m-label-foo\">foo at C1#m</a>",
124+
assert_equal "<a href=\"C1.html#method-i-m-label-foo\">foo at <code>C1#m</code></a>",
125125
REGEXP_HANDLING('C1#m@foo')
126126
end
127127

128128
def test_handle_regexp_CROSSREF_show_hash_false
129129
@to.show_hash = false
130130

131-
assert_equal "<a href=\"C1.html#method-i-m\">m</a>",
131+
assert_equal "<a href=\"C1.html#method-i-m\"><code>m</code></a>",
132132
REGEXP_HANDLING('#m')
133133
end
134134

@@ -140,11 +140,11 @@ def test_handle_regexp_HYPERLINK_rdoc
140140

141141
link = @to.handle_regexp_HYPERLINK hyper 'C2::C3'
142142

143-
assert_equal '<a href="C2/C3.html">C2::C3</a>', link
143+
assert_equal '<a href="C2/C3.html"><code>C2::C3</code></a>', link
144144

145145
link = @to.handle_regexp_HYPERLINK hyper 'C4'
146146

147-
assert_equal '<a href="C4.html">C4</a>', link
147+
assert_equal '<a href="C4.html"><code>C4</code></a>', link
148148

149149
link = @to.handle_regexp_HYPERLINK hyper 'README.txt'
150150

@@ -200,16 +200,16 @@ def test_to_html_CROSSREF_email_hyperlink_all
200200
def test_link
201201
assert_equal 'n', @to.link('n', 'n')
202202

203-
assert_equal '<a href="C1.html#method-c-m">m</a>', @to.link('m', 'm')
203+
assert_equal '<a href="C1.html#method-c-m"><code>m</code></a>', @to.link('m', 'm')
204204
end
205205

206206
def test_link_for_method_traverse
207207
@to = RDoc::Markup::ToHtmlCrossref.new @options, 'C2.html', @c9
208-
assert_equal '<a href="C9/A.html#method-i-foo">C9::B#foo</a>', @to.link('C9::B#foo', 'C9::B#foo')
208+
assert_equal '<a href="C9/A.html#method-i-foo"><code>C9::B#foo</code></a>', @to.link('C9::B#foo', 'C9::B#foo')
209209
end
210210

211211
def test_link_class_method_full
212-
assert_equal '<a href="Parent.html#method-c-m">Parent::m</a>',
212+
assert_equal '<a href="Parent.html#method-c-m"><code>Parent::m</code></a>',
213213
@to.link('Parent::m', 'Parent::m')
214214
end
215215

0 commit comments

Comments
 (0)