[ruby/rdoc] Allow empty name rdoc-ref as a local link

https://github.com/ruby/rdoc/commit/914a6af137
This commit is contained in:
Nobuyoshi Nakada 2023-12-31 23:57:09 +09:00 committed by git
parent b4adc1bbab
commit 569a06aa2f
2 changed files with 24 additions and 13 deletions

View File

@ -63,8 +63,8 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
name = name[1..-1] unless @show_hash if name[0, 1] == '#' name = name[1..-1] unless @show_hash if name[0, 1] == '#'
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])@/ if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/
text ||= "#{CGI.unescape $'} at <code>#{$1}</code>" text ||= [CGI.unescape($'), (" at <code>#{$1}</code>" if $~.begin(1))].join("")
code = false code = false
else else
text ||= name text ||= name
@ -139,35 +139,34 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
# Creates an HTML link to +name+ with the given +text+. # Creates an HTML link to +name+ with the given +text+.
def link name, text, code = true def link name, text, code = true
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])@/ if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/
name = $1 name = $1
label = $' label = $'
end end
ref = @cross_reference.resolve name, text ref = @cross_reference.resolve name, text if name
case ref case ref
when String then when String then
ref ref
else else
path = ref.as_href @from_path path = ref ? ref.as_href(@from_path) : +""
if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref) if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref)
text = "<code>#{CGI.escapeHTML text}</code>" text = "<code>#{CGI.escapeHTML text}</code>"
end end
if path =~ /#/ then if label
path << "-label-#{label}" if path =~ /#/
elsif ref.sections and path << "-label-#{label}"
ref.sections.any? { |section| label == section.title } then elsif ref&.sections&.any? { |section| label == section.title }
path << "##{label}" path << "##{label}"
else elsif ref.respond_to?(:aref)
if ref.respond_to?(:aref)
path << "##{ref.aref}-label-#{label}" path << "##{ref.aref}-label-#{label}"
else else
path << "#label-#{label}" path << "#label-#{label}"
end end
end if label end
"<a href=\"#{path}\">#{text}</a>" "<a href=\"#{path}\">#{text}</a>"
end end

View File

@ -133,6 +133,18 @@ class TestRDocMarkupToHtmlCrossref < XrefTestCase
'rdoc-ref:C1@foo' 'rdoc-ref:C1@foo'
end end
def test_convert_RDOCLINK_rdoc_ref_label_in_current_file
result = @to.convert 'rdoc-ref:@foo'
assert_equal para("<a href=\"#label-foo\">foo</a>"), result,
'rdoc-ref:@foo'
result = @to.convert '{Foo}[rdoc-ref:@foo]'
assert_equal para("<a href=\"#label-foo\">Foo</a>"), result,
'{Foo}[rdoc-ref:@foo]'
end
def test_gen_url def test_gen_url
assert_equal '<a href="C1.html">Some class</a>', assert_equal '<a href="C1.html">Some class</a>',
@to.gen_url('rdoc-ref:C1', 'Some class') @to.gen_url('rdoc-ref:C1', 'Some class')