Skip to content

Commit

Permalink
Enable cross reference in code (#1240)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nobu authored Dec 17, 2024
1 parent 94b9858 commit 7d7efb0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/rdoc/markup/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,39 @@ def in_tt?
@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

##
# 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
Expand Down
39 changes: 39 additions & 0 deletions lib/rdoc/markup/to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,43 @@ def link name, text, code = true, rdoc_ref: false
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
12 changes: 12 additions & 0 deletions test/rdoc/test_rdoc_markup_to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def test_convert_CROSSREF
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
Expand Down

0 comments on commit 7d7efb0

Please sign in to comment.