Skip to content

Commit 9e2b28c

Browse files
authored
Expand rdoc-ref targets at the end of ri output (#1141)
There have been several document refactors in ruby/ruby that extract individual methods/classes' documentation into separate files, like ruby/ruby#6567 Because RI is not capable of rendering those references, RI users are left with dramatically fewer documentation on those methods/classes. This commit adds a new option `--expand-ref` (default: true) to expand all the rdoc-ref targets at the end of the output.
1 parent 4004dff commit 9e2b28c

File tree

2 files changed

+102
-11
lines changed

2 files changed

+102
-11
lines changed

lib/rdoc/ri/driver.rb

+48-8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def self.default_options
7979
options[:interactive] = false
8080
options[:profile] = false
8181
options[:show_all] = false
82+
options[:expand_refs] = true
8283
options[:use_stdout] = !$stdout.tty?
8384
options[:width] = 72
8485

@@ -245,6 +246,12 @@ def self.process_args argv
245246

246247
opt.separator nil
247248

249+
opt.on("--[no-]expand-refs", "Expand rdoc-refs at the end of output") do |value|
250+
options[:expand_refs] = value
251+
end
252+
253+
opt.separator nil
254+
248255
opt.on("--help", "-h",
249256
"Show help and exit.") do
250257
puts opts
@@ -425,6 +432,7 @@ def initialize initial_options = {}
425432
@use_stdout = options[:use_stdout]
426433
@show_all = options[:show_all]
427434
@width = options[:width]
435+
@expand_refs = options[:expand_refs]
428436
end
429437

430438
##
@@ -549,11 +557,8 @@ def add_includes out, includes
549557
# Looks up the method +name+ and adds it to +out+
550558

551559
def add_method out, name
552-
filtered = lookup_method name
553-
554-
method_out = method_document name, filtered
555-
556-
out.concat method_out.parts
560+
filtered = lookup_method name
561+
method_document out, name, filtered
557562
end
558563

559564
##
@@ -645,6 +650,7 @@ def class_document name, found, klasses, includes, extends
645650

646651
add_also_in out, also_in
647652

653+
expand_rdoc_refs_at_the_bottom(out)
648654
out
649655
end
650656

@@ -824,6 +830,8 @@ def display_method name
824830

825831
add_method out, name
826832

833+
expand_rdoc_refs_at_the_bottom(out)
834+
827835
display out
828836
end
829837

@@ -1255,9 +1263,7 @@ def lookup_method name
12551263
##
12561264
# Builds a RDoc::Markup::Document from +found+, +klasses+ and +includes+
12571265

1258-
def method_document name, filtered
1259-
out = RDoc::Markup::Document.new
1260-
1266+
def method_document out, name, filtered
12611267
out << RDoc::Markup::Heading.new(1, name)
12621268
out << RDoc::Markup::BlankLine.new
12631269

@@ -1514,4 +1520,38 @@ def start_server
15141520
server.start
15151521
end
15161522

1523+
RDOC_REFS_REGEXP = /\[rdoc-ref:([\w.]+)(@.*)?\]/
1524+
1525+
def expand_rdoc_refs_at_the_bottom(out)
1526+
return unless @expand_refs
1527+
1528+
extracted_rdoc_refs = []
1529+
1530+
out.each do |part|
1531+
content = if part.respond_to?(:text)
1532+
part.text
1533+
else
1534+
next
1535+
end
1536+
1537+
rdoc_refs = content.scan(RDOC_REFS_REGEXP).uniq.map do |file_name, _anchor|
1538+
file_name
1539+
end
1540+
1541+
extracted_rdoc_refs.concat(rdoc_refs)
1542+
end
1543+
1544+
found_pages = extracted_rdoc_refs.map do |ref|
1545+
begin
1546+
@stores.first.load_page(ref)
1547+
rescue RDoc::Store::MissingFileError
1548+
end
1549+
end.compact
1550+
1551+
found_pages.each do |page|
1552+
out << RDoc::Markup::Heading.new(4, "Expanded from #{page.full_name}")
1553+
out << RDoc::Markup::BlankLine.new
1554+
out << page.comment
1555+
end
1556+
end
15171557
end

test/rdoc/test_rdoc_ri_driver.rb

+54-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
require_relative 'helper'
33

4-
class TestRDocRIDriver < RDoc::TestCase
4+
class RDocRIDriverTest < RDoc::TestCase
55

66
def setup
77
super
@@ -243,6 +243,29 @@ def test_add_method
243243
assert_equal expected, out
244244
end
245245

246+
def test_add_method_with_rdoc_ref_link
247+
util_store
248+
249+
out = doc
250+
251+
@driver.add_method out, 'Foo::Bar#blah_with_rdoc_ref'
252+
253+
expected =
254+
doc(
255+
head(1, 'Foo::Bar#blah_with_rdoc_ref'),
256+
blank_line,
257+
para("(from #{@rdoc_home})"),
258+
head(3, 'Implementation from Bar'),
259+
rule(1),
260+
verb("blah(5) => 5\n", "See also {Doc}[rdoc-ref:README.rdoc]\n"),
261+
rule(1),
262+
blank_line,
263+
blank_line
264+
)
265+
266+
assert_equal expected, out
267+
end
268+
246269
def test_add_method_that_is_alias_for_original
247270
util_store
248271

@@ -598,7 +621,7 @@ def test_display_class
598621
assert_match %r%^= Attributes:%, out
599622
assert_match %r%^ attr_accessor attr%, out
600623

601-
assert_equal 1, out.scan(/^-{50,}$/).length, out
624+
assert_equal 2, out.scan(/^-{50,}$/).length, out
602625

603626
refute_match %r%Foo::Bar#blah%, out
604627
end
@@ -622,9 +645,29 @@ def test_display_class_all
622645
assert_match %r%^= Attributes:%, out
623646
assert_match %r%^ attr_accessor attr%, out
624647

625-
assert_equal 6, out.scan(/^-{50,}$/).length, out
648+
assert_equal 9, out.scan(/^-{50,}$/).length, out
626649

627650
assert_match %r%Foo::Bar#blah%, out
651+
assert_match %r%Foo::Bar#blah_with_rdoc_ref%, out
652+
# From Foo::Bar and Foo::Bar#blah_with_rdoc_ref
653+
assert_equal 2, out.scan(/rdoc-ref:README.rdoc/).length
654+
# But README.rdoc should only be displayed once
655+
assert_equal 1, out.scan(/Expanded from README.rdoc/).length
656+
end
657+
658+
def test_rdoc_refs_expansion_can_be_disabled
659+
util_store
660+
661+
@driver.instance_variable_set :@expand_rdoc_refs, false
662+
663+
out, = capture_output do
664+
@driver.display_class 'Foo::Bar'
665+
end
666+
667+
# From Foo::Bar
668+
assert_equal 1, out.scan(/rdoc-ref:README.rdoc/).length
669+
# But README.rdoc should not be expanded
670+
assert_empty out.scan(/Expanded from README.rdoc/)
628671
end
629672

630673
def test_display_class_ambiguous
@@ -766,6 +809,7 @@ def test_display_name_not_found_method
766809
Foo::Bar#b not found, maybe you meant:
767810
768811
Foo::Bar#blah
812+
Foo::Bar#blah_with_rdoc_ref
769813
Foo::Bar#bother
770814
EXPECTED
771815

@@ -1141,6 +1185,7 @@ def test_list_methods_matching
11411185
assert_equal %w[
11421186
Foo::Bar#attr
11431187
Foo::Bar#blah
1188+
Foo::Bar#blah_with_rdoc_ref
11441189
Foo::Bar#bother
11451190
Foo::Bar::new
11461191
],
@@ -1516,11 +1561,17 @@ def util_store
15161561
@cFooInc.record_location @top_level
15171562

15181563
@cFoo_Bar = @cFoo.add_class RDoc::NormalClass, 'Bar'
1564+
@cFoo_Bar.add_comment "See also {Doc}[rdoc-ref:README.rdoc]", @top_level
1565+
@cFoo_Bar.record_location @top_level
15191566

15201567
@blah = @cFoo_Bar.add_method RDoc::AnyMethod.new(nil, 'blah')
15211568
@blah.call_seq = "blah(5) => 5\nblah(6) => 6\n"
15221569
@blah.record_location @top_level
15231570

1571+
@blah_with_rdoc_ref = @cFoo_Bar.add_method RDoc::AnyMethod.new(nil, 'blah_with_rdoc_ref')
1572+
@blah_with_rdoc_ref.call_seq = "blah(5) => 5\nSee also {Doc}[rdoc-ref:README.rdoc]"
1573+
@blah_with_rdoc_ref.record_location @top_level
1574+
15241575
@bother = @cFoo_Bar.add_method RDoc::AnyMethod.new(nil, 'bother')
15251576
@bother.block_params = "stuff"
15261577
@bother.params = "(things)"

0 commit comments

Comments
 (0)