Skip to content

Commit b18b541

Browse files
committed
Added sections to the Table of Contents page
1 parent d90ce24 commit b18b541

File tree

8 files changed

+99
-17
lines changed

8 files changed

+99
-17
lines changed

TODO.rdoc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
Some file contains some things that might happen in RDoc, or might not
22

3-
=== RDoc 3.10
4-
5-
* Add sections to the ToC
6-
73
=== RDoc 3.11
84

95
* Reload the RDoc tree from an RI store

lib/rdoc/context.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,9 @@ def each_include # :yields: include
653653
# Iterator for methods
654654

655655
def each_method # :yields: method
656-
@method_list.sort.each {|m| yield m}
656+
return enum_for __method__ unless block_given?
657+
658+
@method_list.sort.each { |m| yield m }
657659
end
658660

659661
##
@@ -1007,10 +1009,10 @@ def remove_invisible_in array, min_visibility # :nodoc:
10071009
end
10081010

10091011
##
1010-
# Tries to resolve unmatched aliases when a method
1011-
# or attribute has just been added.
1012+
# Tries to resolve unmatched aliases when a method or attribute has just
1013+
# been added.
10121014

1013-
def resolve_aliases(added)
1015+
def resolve_aliases added
10141016
# resolve any pending unmatched aliases
10151017
key = added.pretty_name
10161018
unmatched_alias_list = @unmatched_alias_lists[key]
@@ -1022,6 +1024,31 @@ def resolve_aliases(added)
10221024
@unmatched_alias_lists.delete key
10231025
end
10241026

1027+
##
1028+
# Returns RDoc::Context::Section objects referenced in this context for use
1029+
# in a table of contents.
1030+
1031+
def section_contents
1032+
used_sections = {}
1033+
1034+
each_method do |method|
1035+
next unless method.display?
1036+
1037+
used_sections[method.section] = true
1038+
end
1039+
1040+
# order found sections
1041+
sections = sort_sections.select do |section|
1042+
used_sections[section]
1043+
end
1044+
1045+
# only the default section is used
1046+
return [] if
1047+
sections.length == 1 and not sections.first.title
1048+
1049+
sections
1050+
end
1051+
10251052
##
10261053
# Sections in this context
10271054

lib/rdoc/context/section.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def inspect # :nodoc:
102102
"#<%s:0x%x %p>" % [self.class, object_id, title]
103103
end
104104

105+
##
106+
# The section's title, or 'Top Section' if the title is nil.
107+
#
108+
# This is used by the table of contents template so the name is silly.
109+
110+
def plain_html
111+
@title || 'Top Section'
112+
end
113+
105114
##
106115
# Section sequence number (deprecated)
107116

lib/rdoc/generator/template/darkfish/table_of_contents.rhtml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
<% simple_files.sort.each do |file| %>
99
<li class="file">
1010
<a href="<%= file.path %>"><%= h file.page_name %></a>
11-
<% table = file.parse(file.comment).table_of_contents # HACK
11+
<%
12+
# HACK table_of_contents should not exist on Document
13+
table = file.parse(file.comment).table_of_contents
1214
unless table.empty? then %>
1315
<img class="toc-toggle" src="images/transparent.png" alt="" title="toggle headings">
1416
<ul class="initially-hidden">
@@ -27,12 +29,15 @@
2729
<% @modsort.each do |klass| %>
2830
<li class="<%= klass.type %>">
2931
<a href="<%= klass.path %>"><%= klass.full_name %></a>
30-
<% table = klass.parse(klass.comment).table_of_contents # HACK
32+
<% table = []
33+
table.push(*klass.parse(klass.comment).table_of_contents)
34+
table.push(*klass.section_contents)
35+
3136
unless table.empty? then %>
3237
<img class="toc-toggle" src="images/transparent.png" alt="" title="toggle headings">
3338
<ul class="initially-hidden">
34-
<% table.each do |heading| %>
35-
<li><a href="<%= klass.path %>#<%= heading.label %>"><%= heading.plain_html %></a>
39+
<% table.each do |item| %>
40+
<li><a href="<%= klass.path %>#<%= item.aref %>"><%= item.plain_html %></a>
3641
<% end %>
3742
</ul>
3843
<% end %>

lib/rdoc/markup/heading.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def accept visitor
4040
end
4141

4242
##
43-
# An HTML-safe label for this header.
43+
# An HTML-safe anchor reference for this header.
4444

45-
def label
45+
def aref
4646
"label-#{self.class.to_label.convert text.dup}"
4747
end
4848

lib/rdoc/markup/to_html.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def accept_blank_line(blank_line)
268268
def accept_heading heading
269269
level = [6, heading.level].min
270270

271-
label = heading.label
271+
label = heading.aref
272272
label = [@code_object.aref, label].compact.join '-' if
273273
@code_object and @code_object.respond_to? :aref
274274

test/test_rdoc_context.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ def test_equals2
364364
refute_equal @c2_c3, @c3
365365
end
366366

367+
def test_each_method_enumerator
368+
assert_kind_of Enumerator, @c1.each_method
369+
end
370+
367371
def test_each_section
368372
sects = []
369373
consts = []
@@ -392,6 +396,10 @@ def test_each_section
392396
assert_equal expected_attrs, attrs
393397
end
394398

399+
def test_each_section_enumerator
400+
assert_kind_of Enumerator, @c1.each_section
401+
end
402+
395403
def test_find_attribute_named
396404
assert_equal nil, @c1.find_attribute_named('none')
397405
assert_equal 'R', @c1.find_attribute_named('attr').rw
@@ -667,6 +675,43 @@ def test_remove_invisible_in_public_force
667675
assert_equal [@pub, @prot, @priv], methods
668676
end
669677

678+
def test_section_contents
679+
default = @context.sections.first
680+
@context.add_method RDoc::AnyMethod.new(nil, 'm1')
681+
682+
b = @context.add_section 'B'
683+
m = @context.add_method RDoc::AnyMethod.new(nil, 'm2')
684+
m.section = b
685+
686+
assert_equal [default, b], @context.section_contents
687+
end
688+
689+
def test_section_contents_no_default
690+
@context = RDoc::Context.new
691+
b = @context.add_section 'B'
692+
m = @context.add_method RDoc::AnyMethod.new(nil, 'm')
693+
m.section = b
694+
695+
assert_equal [b], @context.section_contents
696+
end
697+
698+
def test_section_contents_only_default
699+
@context = RDoc::Context.new
700+
701+
@context.add_method RDoc::AnyMethod.new(nil, 'm')
702+
703+
assert_empty @context.section_contents
704+
end
705+
706+
def test_section_contents_unused
707+
@context = RDoc::Context.new
708+
709+
@context.add_method RDoc::AnyMethod.new(nil, 'm')
710+
b = @context.add_section 'B'
711+
712+
assert_empty @context.section_contents
713+
end
714+
670715
def test_set_current_section
671716
default_section = @context.sections.first
672717

test/test_rdoc_markup_heading.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def setup
88
@h = RDoc::Markup::Heading.new 1, 'Hello *Friend*!'
99
end
1010

11-
def test_label
12-
assert_equal 'label-Hello+Friend%21', @h.label
11+
def test_aref
12+
assert_equal 'label-Hello+Friend%21', @h.aref
1313
end
1414

1515
def test_plain_html

0 commit comments

Comments
 (0)