Skip to content

Commit

Permalink
Add :autolink: directive to suppress auto linking
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jan 1, 2025
1 parent dcde9a1 commit ee5f96a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/rdoc/code_object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true



##
# Base class for the RDoc code tree.
#
Expand Down Expand Up @@ -424,4 +427,19 @@ def suppressed?
@suppressed
end

##
# Autolink in cross reference?

def autolink?
autolink = @metadata["autolink"]
if autolink
RDoc::Options.boolean(autolink, "autolink")
else
true
end
end

RDoc::Markup::PreProcess.register "autolink" do |directive, param|
""
end
end
9 changes: 5 additions & 4 deletions lib/rdoc/markup/to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def init_link_notation_regexp_handlings
# Creates a link to the reference +name+ if the name exists. If +text+ is
# given it is used as the link text, otherwise +name+ is used.

def cross_reference name, text = nil, code = true, rdoc_ref: false
def cross_reference name, text = nil, code = true, rdoc_ref: false, autolink: true
lookup = name

name = name[1..-1] unless @show_hash if name[0, 1] == '#'
Expand All @@ -70,7 +70,7 @@ def cross_reference name, text = nil, code = true, rdoc_ref: false
text ||= name
end

link lookup, text, code, rdoc_ref: rdoc_ref
link lookup, text, code, rdoc_ref: rdoc_ref, autolink: autolink
end

##
Expand All @@ -94,7 +94,7 @@ def handle_regexp_CROSSREF(target)
return name if name =~ /\A[a-z]*\z/
end

cross_reference name, rdoc_ref: false
cross_reference name, rdoc_ref: false, autolink: false
end

##
Expand Down Expand Up @@ -147,7 +147,7 @@ def gen_url url, text
##
# Creates an HTML link to +name+ with the given +text+.

def link name, text, code = true, rdoc_ref: false
def link name, text, code = true, rdoc_ref: false, autolink: true
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/
name = $1
label = $'
Expand All @@ -165,6 +165,7 @@ def link name, text, code = true, rdoc_ref: false
path = ref ? ref.as_href(@from_path) : +""

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

Expand Down
9 changes: 9 additions & 0 deletions test/rdoc/test_rdoc_code_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,13 @@ def test_suppress_eh
assert @co.suppressed?
end

def test_autolink
assert_predicate @c1, :autolink?, "default is true"

@c1.metadata["autolink"] = "false"
assert_not_predicate @c1, :autolink?

@c1.metadata["autolink"] = "true"
assert_predicate @c1, :autolink?
end
end
14 changes: 14 additions & 0 deletions test/rdoc/test_rdoc_markup_to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def test_convert_CROSSREF_ignored_excluded_words
assert_equal para("<a href=\"C1.html\">Constant</a>"), result
end

def test_convert_CROSSREF_no_autolink
@c1.metadata["autolink"] = "false"

result = @to.convert 'C1'
assert_equal para("C1"), result

result = @to.convert '+C1+'
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result

# Explicit linking with rdoc-ref is not ignored
result = @to.convert 'Constant[rdoc-ref:C1]'
assert_equal para("<a href=\"C1.html\">Constant</a>"), result
end

def test_convert_CROSSREF_method
result = @to.convert 'C1#m(foo, bar, baz)'

Expand Down

0 comments on commit ee5f96a

Please sign in to comment.