Skip to content

Commit 7d7efb0

Browse files
authored
Enable cross reference in code (#1240)
Some people like to mark up method names in MarkDown style block quotes, like this: ruby/ruby#12333. Currently, no links are created in the code in the RDoc, but such words most likely refer to methods. This PR makes a word a code cross-reference if the whole word can be resolved as a reference.
1 parent 94b9858 commit 7d7efb0

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

lib/rdoc/markup/formatter.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,39 @@ def in_tt?
195195
@in_tt > 0
196196
end
197197

198+
def tt_tag? attr_mask, reverse = false
199+
each_attr_tag(attr_mask, reverse) do |tag|
200+
return true if tt? tag
201+
end
202+
false
203+
end
204+
198205
##
199206
# Turns on tags for +item+ on +res+
200207

201208
def on_tags res, item
202-
attr_mask = item.turn_on
203-
return if attr_mask.zero?
204-
205-
@attr_tags.each do |tag|
206-
if attr_mask & tag.bit != 0 then
207-
res << annotate(tag.on)
208-
@in_tt += 1 if tt? tag
209-
end
209+
each_attr_tag(item.turn_on) do |tag|
210+
res << annotate(tag.on)
211+
@in_tt += 1 if tt? tag
210212
end
211213
end
212214

213215
##
214216
# Turns off tags for +item+ on +res+
215217

216218
def off_tags res, item
217-
attr_mask = item.turn_off
219+
each_attr_tag(item.turn_off, true) do |tag|
220+
@in_tt -= 1 if tt? tag
221+
res << annotate(tag.off)
222+
end
223+
end
224+
225+
def each_attr_tag attr_mask, reverse = false
218226
return if attr_mask.zero?
219227

220-
@attr_tags.reverse_each do |tag|
228+
@attr_tags.public_send(reverse ? :reverse_each : :each) do |tag|
221229
if attr_mask & tag.bit != 0 then
222-
@in_tt -= 1 if tt? tag
223-
res << annotate(tag.off)
230+
yield tag
224231
end
225232
end
226233
end

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,43 @@ def link name, text, code = true, rdoc_ref: false
182182
end
183183
end
184184

185+
def convert_flow(flow)
186+
res = []
187+
188+
i = 0
189+
while i < flow.size
190+
item = flow[i]
191+
i += 1
192+
case item
193+
when RDoc::Markup::AttrChanger then
194+
# Make "+Class#method+" a cross reference
195+
if tt_tag?(item.turn_on) and
196+
String === (str = flow[i]) and
197+
RDoc::Markup::AttrChanger === flow[i+1] and
198+
tt_tag?(flow[i+1].turn_off, true) and
199+
(@options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP).match?(str) and
200+
(text = cross_reference str) != str
201+
then
202+
text = yield text, res if defined?(yield)
203+
res << text
204+
i += 2
205+
next
206+
end
207+
off_tags res, item
208+
on_tags res, item
209+
when String then
210+
text = convert_string(item)
211+
text = yield text, res if defined?(yield)
212+
res << text
213+
when RDoc::Markup::RegexpHandling then
214+
text = convert_regexp_handling(item)
215+
text = yield text, res if defined?(yield)
216+
res << text
217+
else
218+
raise "Unknown flow element: #{item.inspect}"
219+
end
220+
end
221+
222+
res.join('')
223+
end
185224
end

test/rdoc/test_rdoc_markup_to_html_crossref.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ def test_convert_CROSSREF
1616
result = @to.convert 'C1'
1717

1818
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
19+
20+
result = @to.convert '+C1+'
21+
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
22+
23+
result = @to.convert 'FOO'
24+
assert_equal para("FOO"), result
25+
26+
result = @to.convert '+FOO+'
27+
assert_equal para("<code>FOO</code>"), result
28+
29+
result = @to.convert '<tt># :stopdoc:</tt>:'
30+
assert_equal para("<code># :stopdoc:</code>:"), result
1931
end
2032

2133
def test_convert_CROSSREF_method

0 commit comments

Comments
 (0)