Skip to content

Add autolink_excluded_words option to ignore cross-references #1259

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

Merged
merged 1 commit into from
Dec 31, 2024
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
2 changes: 2 additions & 0 deletions lib/rdoc/markup/to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def cross_reference name, text = nil, code = true, rdoc_ref: false
def handle_regexp_CROSSREF(target)
name = target.text

return name if @options.autolink_excluded_words&.include?(name)

return name if name =~ /@[\w-]+\.[\w-]/ # labels that look like emails

unless @hyperlink_all then
Expand Down
20 changes: 18 additions & 2 deletions lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ class RDoc::Options
# Exclude the default patterns as well if true.
attr_reader :apply_default_exclude

##
# Words to be ignored in autolink cross-references
attr_accessor :autolink_excluded_words

def initialize loaded_options = nil # :nodoc:
init_ivars
override loaded_options if loaded_options
Expand All @@ -370,6 +374,7 @@ def initialize loaded_options = nil # :nodoc:
]

def init_ivars # :nodoc:
@autolink_excluded_words = []
@dry_run = false
@embed_mixins = false
@exclude = []
Expand Down Expand Up @@ -437,7 +442,9 @@ def init_with map # :nodoc:
@title = map['title']
@visibility = map['visibility']
@webcvs = map['webcvs']
@apply_default_exclude = map['apply_default_exclude']

@apply_default_exclude = map['apply_default_exclude']
@autolink_excluded_words = map['autolink_excluded_words']

@rdoc_include = sanitize_path map['rdoc_include']
@static_path = sanitize_path map['static_path']
Expand Down Expand Up @@ -471,6 +478,7 @@ def override map # :nodoc:
@title = map['title'] if map.has_key?('title')
@visibility = map['visibility'] if map.has_key?('visibility')
@webcvs = map['webcvs'] if map.has_key?('webcvs')
@autolink_excluded_words = map['autolink_excluded_words'] if map.has_key?('autolink_excluded_words')
@apply_default_exclude = map['apply_default_exclude'] if map.has_key?('apply_default_exclude')

@warn_missing_rdoc_ref = map['warn_missing_rdoc_ref'] if map.has_key?('warn_missing_rdoc_ref')
Expand Down Expand Up @@ -503,7 +511,8 @@ def == other # :nodoc:
@title == other.title and
@visibility == other.visibility and
@webcvs == other.webcvs and
@apply_default_exclude == other.apply_default_exclude
@apply_default_exclude == other.apply_default_exclude and
@autolink_excluded_words == other.autolink_excluded_words
end

##
Expand Down Expand Up @@ -989,6 +998,13 @@ def parse argv

opt.separator nil

opt.on("--autolink-excluded-words=WORDS", Array,
"Words to be ignored in autolink cross-references") do |value|
@autolink_excluded_words.concat value
end

opt.separator nil

opt.on("--hyperlink-all", "-A",
"Generate hyperlinks for all words that",
"correspond to known methods, even if they",
Expand Down
17 changes: 17 additions & 0 deletions test/rdoc/test_rdoc_markup_to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def test_convert_CROSSREF
result = @to.convert '+C1+'
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result

result = @to.convert 'Constant[rdoc-ref:C1]'
assert_equal para("<a href=\"C1.html\">Constant</a>"), result

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

Expand All @@ -30,6 +33,20 @@ def test_convert_CROSSREF
assert_equal para("<code># :stopdoc:</code>:"), result
end

def test_convert_CROSSREF_ignored_excluded_words
@options.autolink_excluded_words = ['C1']

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
1 change: 1 addition & 0 deletions test/rdoc/test_rdoc_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def test_to_yaml
'webcvs' => nil,
'skip_tests' => true,
'apply_default_exclude' => true,
'autolink_excluded_words' => [],
}

assert_equal expected, coder
Expand Down
Loading