Skip to content

Commit e57beff

Browse files
nevansnobu
authored andcommitted
Darkfish: Nest sidebar ToC as a tree of headings
This uses `<details><summary>heading</summary><ul>nested</ul></detail>`, similar to how the classes and pages lists are now nested.
1 parent 34a021c commit e57beff

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@
33
else
44
current.comment
55
end
6-
table = current.parse(comment).table_of_contents
6+
table = current.parse(comment).table_of_contents.dup
77

88
if table.length > 1 then %>
99
<div class="nav-section">
1010
<h3>Table of Contents</h3>
1111

12+
<%- display_link = proc do |heading| -%>
13+
<a href="#<%= heading.label current %>"><%= heading.plain_html %></a>
14+
<%- end -%>
15+
16+
<%- list_siblings = proc do -%>
17+
<%- level = table.first&.level -%>
18+
<%- while table.first && table.first.level >= level -%>
19+
<%- heading = table.shift -%>
20+
<%- if table.first.nil? || table.first.level <= heading.level -%>
21+
<li><% display_link.call heading -%>
22+
<%- else -%>
23+
<li>
24+
<details open>
25+
<summary><%- display_link.call heading -%></summary>
26+
<ul class="link-list" role="directory">
27+
<% list_siblings.call %>
28+
</ul>
29+
</details>
30+
</li>
31+
<%- end -%>
32+
<%- end -%>
33+
<%- end -%>
34+
1235
<ul class="link-list" role="directory">
13-
<%- table.each do |heading| -%>
14-
<li><a href="#<%= heading.label current %>"><%= heading.plain_html %></a>
15-
<%- end -%>
36+
<% list_siblings.call %>
1637
</ul>
1738
</div>
1839
<%- end -%>

test/rdoc/test_rdoc_generator_darkfish.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ def test_generate
7373
top_level = @store.add_file 'file.rb'
7474
top_level.add_class @klass.class, @klass.name
7575
@klass.add_class RDoc::NormalClass, 'Inner'
76+
@klass.add_comment <<~RDOC, top_level
77+
= Heading 1
78+
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
79+
== Heading 1.1
80+
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
81+
=== Heading 1.1.1
82+
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
83+
==== Heading 1.1.1.1
84+
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
85+
== Heading 1.2
86+
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
87+
== Heading 1.3
88+
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
89+
=== Heading 1.3.1
90+
etc etc...
91+
RDOC
7692

7793
@g.generate
7894

@@ -97,6 +113,15 @@ def test_generate
97113
refute_match(/Ignored/, File.read('index.html'))
98114
summary = File.read('index.html')[%r[<summary.*Klass\.html.*</summary>.*</details>]m]
99115
assert_match(%r[Klass/Inner\.html".*>Inner<], summary)
116+
klassnav = File.read('Klass.html')[%r[<div class="nav-section">.*<div id="class-metadata">]m]
117+
assert_match(
118+
%r[<li>\s*<details open>\s*<summary>\s*<a href=\S+>Heading 1</a>\s*</summary>\s*<ul]m,
119+
klassnav
120+
)
121+
assert_match(
122+
%r[<li>\s*<a href=\S+>Heading 1.1.1.1</a>\s*</ul>\s*</details>\s*</li>]m,
123+
klassnav
124+
)
100125
end
101126

102127
def test_generate_page

0 commit comments

Comments
 (0)