[ruby/rdoc] Enable cross reference in code

(https://github.com/ruby/rdoc/pull/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.

https://github.com/ruby/rdoc/commit/7d7efb0709
This commit is contained in:
Nobuyoshi Nakada 2024-12-18 06:48:27 +09:00 committed by git
parent 375fec7c53
commit fef8ecc708
3 changed files with 70 additions and 12 deletions

View File

@ -195,18 +195,20 @@ class RDoc::Markup::Formatter
@in_tt > 0
end
def tt_tag? attr_mask, reverse = false
each_attr_tag(attr_mask, reverse) do |tag|
return true if tt? tag
end
false
end
##
# Turns on tags for +item+ on +res+
def on_tags res, item
attr_mask = item.turn_on
return if attr_mask.zero?
@attr_tags.each do |tag|
if attr_mask & tag.bit != 0 then
res << annotate(tag.on)
@in_tt += 1 if tt? tag
end
each_attr_tag(item.turn_on) do |tag|
res << annotate(tag.on)
@in_tt += 1 if tt? tag
end
end
@ -214,13 +216,18 @@ class RDoc::Markup::Formatter
# Turns off tags for +item+ on +res+
def off_tags res, item
attr_mask = item.turn_off
each_attr_tag(item.turn_off, true) do |tag|
@in_tt -= 1 if tt? tag
res << annotate(tag.off)
end
end
def each_attr_tag attr_mask, reverse = false
return if attr_mask.zero?
@attr_tags.reverse_each do |tag|
@attr_tags.public_send(reverse ? :reverse_each : :each) do |tag|
if attr_mask & tag.bit != 0 then
@in_tt -= 1 if tt? tag
res << annotate(tag.off)
yield tag
end
end
end

View File

@ -182,4 +182,43 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
end
end
def convert_flow(flow)
res = []
i = 0
while i < flow.size
item = flow[i]
i += 1
case item
when RDoc::Markup::AttrChanger then
# Make "+Class#method+" a cross reference
if tt_tag?(item.turn_on) and
String === (str = flow[i]) and
RDoc::Markup::AttrChanger === flow[i+1] and
tt_tag?(flow[i+1].turn_off, true) and
(@options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP).match?(str) and
(text = cross_reference str) != str
then
text = yield text, res if defined?(yield)
res << text
i += 2
next
end
off_tags res, item
on_tags res, item
when String then
text = convert_string(item)
text = yield text, res if defined?(yield)
res << text
when RDoc::Markup::RegexpHandling then
text = convert_regexp_handling(item)
text = yield text, res if defined?(yield)
res << text
else
raise "Unknown flow element: #{item.inspect}"
end
end
res.join('')
end
end

View File

@ -16,6 +16,18 @@ class RDocMarkupToHtmlCrossrefTest < XrefTestCase
result = @to.convert 'C1'
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
result = @to.convert '+C1+'
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
result = @to.convert 'FOO'
assert_equal para("FOO"), result
result = @to.convert '+FOO+'
assert_equal para("<code>FOO</code>"), result
result = @to.convert '<tt># :stopdoc:</tt>:'
assert_equal para("<code># :stopdoc:</code>:"), result
end
def test_convert_CROSSREF_method