From ee5f96aaccb56c5ee60ad77f703d5bf3175181bb Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 31 Dec 2024 18:06:13 +0900 Subject: [PATCH] Add `:autolink:` directive to suppress auto linking --- lib/rdoc/code_object.rb | 18 ++++++++++++++++++ lib/rdoc/markup/to_html_crossref.rb | 9 +++++---- test/rdoc/test_rdoc_code_object.rb | 9 +++++++++ test/rdoc/test_rdoc_markup_to_html_crossref.rb | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/rdoc/code_object.rb b/lib/rdoc/code_object.rb index 83997c2580..46e3d4762f 100644 --- a/lib/rdoc/code_object.rb +++ b/lib/rdoc/code_object.rb @@ -1,4 +1,7 @@ # frozen_string_literal: true + + + ## # Base class for the RDoc code tree. # @@ -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 diff --git a/lib/rdoc/markup/to_html_crossref.rb b/lib/rdoc/markup/to_html_crossref.rb index 06fee91a11..16516ae0d6 100644 --- a/lib/rdoc/markup/to_html_crossref.rb +++ b/lib/rdoc/markup/to_html_crossref.rb @@ -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] == '#' @@ -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 ## @@ -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 ## @@ -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 = $' @@ -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 = "#{CGI.escapeHTML text}" end diff --git a/test/rdoc/test_rdoc_code_object.rb b/test/rdoc/test_rdoc_code_object.rb index 24e228cce1..2392741381 100644 --- a/test/rdoc/test_rdoc_code_object.rb +++ b/test/rdoc/test_rdoc_code_object.rb @@ -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 diff --git a/test/rdoc/test_rdoc_markup_to_html_crossref.rb b/test/rdoc/test_rdoc_markup_to_html_crossref.rb index 5826f3d095..f88aad318b 100644 --- a/test/rdoc/test_rdoc_markup_to_html_crossref.rb +++ b/test/rdoc/test_rdoc_markup_to_html_crossref.rb @@ -47,6 +47,20 @@ def test_convert_CROSSREF_ignored_excluded_words assert_equal para("Constant"), 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("C1"), result + + # Explicit linking with rdoc-ref is not ignored + result = @to.convert 'Constant[rdoc-ref:C1]' + assert_equal para("Constant"), result + end + def test_convert_CROSSREF_method result = @to.convert 'C1#m(foo, bar, baz)'