Skip to content

Commit 4a5206a

Browse files
authored
Print warnings for rdoc-ref links that can't be resolved (#1241)
1 parent 5a8820a commit 4a5206a

File tree

5 files changed

+81
-34
lines changed

5 files changed

+81
-34
lines changed

.rdoc_options

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ exclude:
66
op_dir: _site # for GitHub Pages and should match the config of RDoc task in Rakefile
77
title: rdoc Documentation
88
main_page: README.rdoc
9+
warn_missing_rdoc_ref: true

lib/rdoc/markup/to_html_crossref.rb

+22-12
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
61+
def cross_reference name, text = nil, code = true, rdoc_ref: false
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
7070
text ||= name
7171
end
7272

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

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

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

9898
##
9999
# Handles <tt>rdoc-ref:</tt> scheme links and allows RDoc::Markup::ToHtml to
100100
# handle other schemes.
101101

102102
def handle_regexp_HYPERLINK target
103-
return cross_reference $' if target.text =~ /\Ardoc-ref:/
103+
url = target.text
104104

105-
super
105+
case url
106+
when /\Ardoc-ref:/
107+
cross_reference $', rdoc_ref: true
108+
else
109+
super
110+
end
106111
end
107112

108113
##
@@ -117,8 +122,8 @@ def handle_regexp_RDOCLINK target
117122
url = target.text
118123

119124
case url
120-
when /\Ardoc-ref:/ then
121-
cross_reference $'
125+
when /\Ardoc-ref:/
126+
cross_reference $', rdoc_ref: true
122127
else
123128
super
124129
end
@@ -129,16 +134,18 @@ def handle_regexp_RDOCLINK target
129134
# RDoc::Markup::ToHtml to handle other schemes.
130135

131136
def gen_url url, text
132-
return super unless url =~ /\Ardoc-ref:/
133-
134-
name = $'
135-
cross_reference name, text, name == text
137+
if url =~ /\Ardoc-ref:/
138+
name = $'
139+
cross_reference name, text, name == text, rdoc_ref: true
140+
else
141+
super
142+
end
136143
end
137144

138145
##
139146
# Creates an HTML link to +name+ with the given +text+.
140147

141-
def link name, text, code = true
148+
def link name, text, code = true, rdoc_ref: false
142149
if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/
143150
name = $1
144151
label = $'
@@ -148,6 +155,9 @@ def link name, text, code = true
148155

149156
case ref
150157
when String then
158+
if rdoc_ref && @options.warn_missing_rdoc_ref
159+
puts "#{@from_path}: `rdoc-ref:#{name}` can't be resolved for `#{text}`"
160+
end
151161
ref
152162
else
153163
path = ref ? ref.as_href(@from_path) : +""

lib/rdoc/options.rb

+16
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ class RDoc::Options
325325

326326
attr_accessor :verbosity
327327

328+
##
329+
# Warn if rdoc-ref links can't be resolved
330+
# Default is +false+
331+
332+
attr_accessor :warn_missing_rdoc_ref
333+
328334
##
329335
# URL of web cvs frontend
330336

@@ -393,6 +399,7 @@ def init_ivars # :nodoc:
393399
@update_output_dir = true
394400
@verbosity = 1
395401
@visibility = :protected
402+
@warn_missing_rdoc_ref = false
396403
@webcvs = nil
397404
@write_options = false
398405
@encoding = Encoding::UTF_8
@@ -457,6 +464,8 @@ def override map # :nodoc:
457464
@visibility = map['visibility'] if map.has_key?('visibility')
458465
@webcvs = map['webcvs'] if map.has_key?('webcvs')
459466

467+
@warn_missing_rdoc_ref = map['warn_missing_rdoc_ref'] if map.has_key?('warn_missing_rdoc_ref')
468+
460469
if map.has_key?('rdoc_include')
461470
@rdoc_include = sanitize_path map['rdoc_include']
462471
end
@@ -1104,6 +1113,13 @@ def parse argv
11041113

11051114
opt.separator nil
11061115

1116+
opt.on("--warn-missing-rdoc-ref",
1117+
"Warn if rdoc-ref links can't be resolved") do |value|
1118+
@warn_missing_rdoc_ref = value
1119+
end
1120+
1121+
opt.separator nil
1122+
11071123
opt.on("--[no-]ignore-invalid",
11081124
"Ignore invalid options and continue",
11091125
"(default true).") do |value|

test/rdoc/test_rdoc_markup_to_html_crossref.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# frozen_string_literal: true
22
require_relative 'xref_test_case'
33

4-
class TestRDocMarkupToHtmlCrossref < XrefTestCase
4+
class RDocMarkupToHtmlCrossrefTest < XrefTestCase
55

66
def setup
77
super
88

99
@options.hyperlink_all = true
10+
@options.warn_missing_rdoc_ref = true
1011

1112
@to = RDoc::Markup::ToHtmlCrossref.new @options, 'index.html', @c1
1213
end
@@ -67,6 +68,16 @@ def test_convert_RDOCLINK_rdoc_ref
6768
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
6869
end
6970

71+
def test_convert_RDOCLINK_rdoc_ref_not_found
72+
result = nil
73+
stdout, _ = capture_output do
74+
result = @to.convert 'rdoc-ref:FOO'
75+
end
76+
77+
assert_equal para("FOO"), result
78+
assert_include stdout, "index.html: `rdoc-ref:FOO` can't be resolved for `FOO`"
79+
end
80+
7081
def test_convert_RDOCLINK_rdoc_ref_method
7182
result = @to.convert 'rdoc-ref:C1#m'
7283

@@ -153,6 +164,14 @@ def test_gen_url
153164
@to.gen_url('http://example', 'HTTP example')
154165
end
155166

167+
def test_gen_url_rdoc_ref_not_found
168+
stdout, _ = capture_output do
169+
@to.gen_url 'rdoc-ref:FOO', 'FOO'
170+
end
171+
172+
assert_include stdout, "index.html: `rdoc-ref:FOO` can't be resolved for `FOO`"
173+
end
174+
156175
def test_handle_regexp_CROSSREF
157176
assert_equal "<a href=\"C2/C3.html\"><code>C2::C3</code></a>", REGEXP_HANDLING('C2::C3')
158177
end

test/rdoc/test_rdoc_options.rb

+22-21
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,28 @@ def test_to_yaml
6363
encoding = 'UTF-8'
6464

6565
expected = {
66-
'charset' => 'UTF-8',
67-
'encoding' => encoding,
68-
'embed_mixins' => false,
69-
'exclude' => %w[~\z \.orig\z \.rej\z \.bak\z \.gemspec\z],
70-
'hyperlink_all' => false,
71-
'line_numbers' => false,
72-
'locale_dir' => 'locale',
73-
'locale_name' => nil,
74-
'main_page' => nil,
75-
'markup' => 'rdoc',
76-
'output_decoration' => true,
77-
'page_dir' => nil,
78-
'rdoc_include' => [],
79-
'show_hash' => false,
80-
'static_path' => [],
81-
'tab_width' => 8,
82-
'template_stylesheets' => [],
83-
'title' => nil,
84-
'visibility' => :protected,
85-
'webcvs' => nil,
86-
'skip_tests' => true,
66+
'charset' => 'UTF-8',
67+
'encoding' => encoding,
68+
'embed_mixins' => false,
69+
'exclude' => %w[~\z \.orig\z \.rej\z \.bak\z \.gemspec\z],
70+
'hyperlink_all' => false,
71+
'line_numbers' => false,
72+
'locale_dir' => 'locale',
73+
'locale_name' => nil,
74+
'main_page' => nil,
75+
'markup' => 'rdoc',
76+
'output_decoration' => true,
77+
'page_dir' => nil,
78+
'rdoc_include' => [],
79+
'show_hash' => false,
80+
'static_path' => [],
81+
'tab_width' => 8,
82+
'template_stylesheets' => [],
83+
'title' => nil,
84+
'visibility' => :protected,
85+
'warn_missing_rdoc_ref' => false,
86+
'webcvs' => nil,
87+
'skip_tests' => true,
8788
}
8889

8990
assert_equal expected, coder

0 commit comments

Comments
 (0)