Skip to content

Commit 956c7fb

Browse files
committed
Add :autolink: directive to suppress auto linking
1 parent 8c55283 commit 956c7fb

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

lib/rdoc/code_object.rb

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# frozen_string_literal: true
2+
3+
4+
25
##
36
# Base class for the RDoc code tree.
47
#
@@ -424,4 +427,19 @@ def suppressed?
424427
@suppressed
425428
end
426429

430+
##
431+
# Autolink in cross reference?
432+
433+
def autolink?
434+
autolink = @metadata["autolink"]
435+
if autolink
436+
RDoc::Options.boolean(autolink, "autolink")
437+
else
438+
true
439+
end
440+
end
441+
442+
RDoc::Markup::PreProcess.register "autolink" do |directive, param|
443+
""
444+
end
427445
end

lib/rdoc/markup/to_html_crossref.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def init_link_notation_regexp_handlings
5858
# Creates a link to the reference +name+ if the name exists. If +text+ is
5959
# given it is used as the link text, otherwise +name+ is used.
6060

61-
def cross_reference name, text = nil, code = true, rdoc_ref: false
61+
def cross_reference name, text = nil, code = true, rdoc_ref: false, autolink: true
6262
lookup = name
6363

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

73-
link lookup, text, code, rdoc_ref: rdoc_ref
73+
link lookup, text, code, rdoc_ref: rdoc_ref, autolink: autolink
7474
end
7575

7676
##
@@ -92,7 +92,7 @@ def handle_regexp_CROSSREF(target)
9292
return name if name =~ /\A[a-z]*\z/
9393
end
9494

95-
cross_reference name, rdoc_ref: false
95+
cross_reference name, rdoc_ref: false, autolink: false
9696
end
9797

9898
##
@@ -145,7 +145,7 @@ def gen_url url, text
145145
##
146146
# Creates an HTML link to +name+ with the given +text+.
147147

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

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

test/rdoc/test_rdoc_code_object.rb

+9
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,13 @@ def test_suppress_eh
437437
assert @co.suppressed?
438438
end
439439

440+
def test_autolink
441+
assert_predicate @c1, :autolink?, "default is true"
442+
443+
@c1.metadata["autolink"] = "false"
444+
assert_not_predicate @c1, :autolink?
445+
446+
@c1.metadata["autolink"] = "true"
447+
assert_predicate @c1, :autolink?
448+
end
440449
end

test/rdoc/test_rdoc_markup_to_html_crossref.rb

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ def test_convert_CROSSREF
3030
assert_equal para("<code># :stopdoc:</code>:"), result
3131
end
3232

33+
def test_convert_CROSSREF_no_autolink
34+
@c1.metadata["autolink"] = "false"
35+
36+
result = @to.convert 'C1'
37+
assert_equal para("C1"), result
38+
39+
result = @to.convert '+C1+'
40+
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
41+
42+
# Explicit linking with rdoc-ref is not ignored
43+
result = @to.convert 'Constant[rdoc-ref:C1]'
44+
assert_equal para("<a href=\"C1.html\">Constant</a>"), result
45+
end
46+
3347
def test_convert_CROSSREF_method
3448
result = @to.convert 'C1#m(foo, bar, baz)'
3549

0 commit comments

Comments
 (0)