Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty name rdoc-ref as a local link #1073

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions lib/rdoc/markup/to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def cross_reference name, text = nil, code = true

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

if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])@/
text ||= "#{CGI.unescape $'} at <code>#{$1}</code>"
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/
text ||= [CGI.unescape($'), (" at <code>#{$1}</code>" if $~.begin(1))].join("")
code = false
else
text ||= name
Expand Down Expand Up @@ -139,35 +139,34 @@ def gen_url url, text
# Creates an HTML link to +name+ with the given +text+.

def link name, text, code = true
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])@/
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/
name = $1
label = $'
end

ref = @cross_reference.resolve name, text
ref = @cross_reference.resolve name, text if name

case ref
when String then
ref
else
path = ref.as_href @from_path
path = ref ? ref.as_href(@from_path) : +""

if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref)
text = "<code>#{CGI.escapeHTML text}</code>"
end

if path =~ /#/ then
path << "-label-#{label}"
elsif ref.sections and
ref.sections.any? { |section| label == section.title } then
path << "##{label}"
else
if ref.respond_to?(:aref)
if label
if path =~ /#/
path << "-label-#{label}"
elsif ref&.sections&.any? { |section| label == section.title }
path << "##{label}"
elsif ref.respond_to?(:aref)
path << "##{ref.aref}-label-#{label}"
else
path << "#label-#{label}"
end
end if label
end

"<a href=\"#{path}\">#{text}</a>"
end
Expand Down
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 @@ -133,6 +133,18 @@ def test_convert_RDOCLINK_rdoc_ref_label
'rdoc-ref:C1@foo'
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
assert_equal '<a href="C1.html">Some class</a>',
@to.gen_url('rdoc-ref:C1', 'Some class')
Expand Down