Skip to content

Commit ce77f51

Browse files
authored
Add autolink_excluded_words option to ignore cross-references (#1259)
This config will be handy when the project name is the same as a class or module name, which is often the case for most of the projects.
1 parent 5dfccda commit ce77f51

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

lib/rdoc/markup/to_html_crossref.rb

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def cross_reference name, text = nil, code = true, rdoc_ref: false
8383
def handle_regexp_CROSSREF(target)
8484
name = target.text
8585

86+
return name if @options.autolink_excluded_words&.include?(name)
87+
8688
return name if name =~ /@[\w-]+\.[\w-]/ # labels that look like emails
8789

8890
unless @hyperlink_all then

lib/rdoc/options.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ class RDoc::Options
359359
# Exclude the default patterns as well if true.
360360
attr_reader :apply_default_exclude
361361

362+
##
363+
# Words to be ignored in autolink cross-references
364+
attr_accessor :autolink_excluded_words
365+
362366
def initialize loaded_options = nil # :nodoc:
363367
init_ivars
364368
override loaded_options if loaded_options
@@ -370,6 +374,7 @@ def initialize loaded_options = nil # :nodoc:
370374
]
371375

372376
def init_ivars # :nodoc:
377+
@autolink_excluded_words = []
373378
@dry_run = false
374379
@embed_mixins = false
375380
@exclude = []
@@ -437,7 +442,9 @@ def init_with map # :nodoc:
437442
@title = map['title']
438443
@visibility = map['visibility']
439444
@webcvs = map['webcvs']
440-
@apply_default_exclude = map['apply_default_exclude']
445+
446+
@apply_default_exclude = map['apply_default_exclude']
447+
@autolink_excluded_words = map['autolink_excluded_words']
441448

442449
@rdoc_include = sanitize_path map['rdoc_include']
443450
@static_path = sanitize_path map['static_path']
@@ -471,6 +478,7 @@ def override map # :nodoc:
471478
@title = map['title'] if map.has_key?('title')
472479
@visibility = map['visibility'] if map.has_key?('visibility')
473480
@webcvs = map['webcvs'] if map.has_key?('webcvs')
481+
@autolink_excluded_words = map['autolink_excluded_words'] if map.has_key?('autolink_excluded_words')
474482
@apply_default_exclude = map['apply_default_exclude'] if map.has_key?('apply_default_exclude')
475483

476484
@warn_missing_rdoc_ref = map['warn_missing_rdoc_ref'] if map.has_key?('warn_missing_rdoc_ref')
@@ -503,7 +511,8 @@ def == other # :nodoc:
503511
@title == other.title and
504512
@visibility == other.visibility and
505513
@webcvs == other.webcvs and
506-
@apply_default_exclude == other.apply_default_exclude
514+
@apply_default_exclude == other.apply_default_exclude and
515+
@autolink_excluded_words == other.autolink_excluded_words
507516
end
508517

509518
##
@@ -989,6 +998,13 @@ def parse argv
989998

990999
opt.separator nil
9911000

1001+
opt.on("--autolink-excluded-words=WORDS", Array,
1002+
"Words to be ignored in autolink cross-references") do |value|
1003+
@autolink_excluded_words.concat value
1004+
end
1005+
1006+
opt.separator nil
1007+
9921008
opt.on("--hyperlink-all", "-A",
9931009
"Generate hyperlinks for all words that",
9941010
"correspond to known methods, even if they",

test/rdoc/test_rdoc_markup_to_html_crossref.rb

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def test_convert_CROSSREF
2020
result = @to.convert '+C1+'
2121
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
2222

23+
result = @to.convert 'Constant[rdoc-ref:C1]'
24+
assert_equal para("<a href=\"C1.html\">Constant</a>"), result
25+
2326
result = @to.convert 'FOO'
2427
assert_equal para("FOO"), result
2528

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

36+
def test_convert_CROSSREF_ignored_excluded_words
37+
@options.autolink_excluded_words = ['C1']
38+
39+
result = @to.convert 'C1'
40+
assert_equal para("C1"), result
41+
42+
result = @to.convert '+C1+'
43+
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
44+
45+
# Explicit linking with rdoc-ref is not ignored
46+
result = @to.convert 'Constant[rdoc-ref:C1]'
47+
assert_equal para("<a href=\"C1.html\">Constant</a>"), result
48+
end
49+
3350
def test_convert_CROSSREF_method
3451
result = @to.convert 'C1#m(foo, bar, baz)'
3552

test/rdoc/test_rdoc_options.rb

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_to_yaml
8686
'webcvs' => nil,
8787
'skip_tests' => true,
8888
'apply_default_exclude' => true,
89+
'autolink_excluded_words' => [],
8990
}
9091

9192
assert_equal expected, coder

0 commit comments

Comments
 (0)