Skip to content

Commit b499478

Browse files
committed
Display main page's Table of Contents when viewed from the index page
When the main page document is viewed as a standalone page, Darkfish displays the Table of Contents. When viewed from the index page, it does not. This page changes the behaviour to also display the Table of Contents when the main page document is viewed from the index page.
1 parent cda767b commit b499478

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

lib/rdoc/generator/darkfish.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,14 @@ def generate_index
316316
asset_rel_prefix = rel_prefix + @asset_rel_path
317317

318318
@title = @options.title
319+
@main_page = @files.find { |f| f.full_name == @options.main_page }
319320

320321
render_template template_file, out_file do |io|
321322
here = binding
322323
# suppress 1.9.3 warning
323324
here.local_variable_set(:asset_rel_prefix, asset_rel_prefix)
325+
# some partials rely on the presence of current variable to render
326+
here.local_variable_set(:current, @main_page) if @main_page
324327
here
325328
end
326329
rescue => e

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
<%= render '_sidebar_search.rhtml' %>
88
</div>
99

10+
<%= render '_sidebar_table_of_contents.rhtml' if defined?(current) %>
1011
<%= render '_sidebar_pages.rhtml' %>
1112
<%= render '_sidebar_classes.rhtml' %>
1213

1314
<%= render '_footer.rhtml' %>
1415
</nav>
1516

1617
<main role="main">
17-
<%- if @options.main_page and
18-
main_page = @files.find { |f| f.full_name == @options.main_page } then %>
19-
<%= main_page.description %>
18+
<%- if @main_page %>
19+
<%= @main_page.description %>
2020
<%- else -%>
21-
<p>This is the API documentation for <%= h @title %>.
21+
<p>This is the API documentation for <%= h @title %>.
2222
<%- end -%>
2323
</main>

test/rdoc/test_rdoc_generator_darkfish.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,88 @@ def test_generate
134134
)
135135
end
136136

137+
def test_generate_index_with_main_page
138+
top_level = @store.add_file 'file.rb'
139+
top_level.comment = <<~RDOC
140+
= Heading 1
141+
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
142+
== Heading 1.1
143+
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
144+
=== Heading 1.1.1
145+
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
146+
==== Heading 1.1.1.1
147+
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
148+
== Heading 1.2
149+
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
150+
== Heading 1.3
151+
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
152+
=== Heading 1.3.1
153+
etc etc...
154+
RDOC
155+
156+
@options.main_page = 'file.rb'
157+
@options.title = 'My awesome Ruby project'
158+
159+
@g.generate
160+
161+
assert_file 'index.html'
162+
assert_file 'table_of_contents.html'
163+
assert_file 'js/search_index.js'
164+
165+
assert_hard_link 'css/rdoc.css'
166+
assert_hard_link 'css/fonts.css'
167+
168+
assert_hard_link 'fonts/SourceCodePro-Bold.ttf'
169+
assert_hard_link 'fonts/SourceCodePro-Regular.ttf'
170+
171+
index_html = File.binread('index.html')
172+
173+
assert_include index_html, "<h3>Table of Contents</h3>"
174+
assert_include index_html, '<h1 id="label-Heading+1">Heading 1'
175+
# When there's a main page, the default description should not be shown
176+
assert_not_include index_html, 'This is the API documentation for My awesome Ruby project.'
177+
end
178+
179+
def test_generate_index_without_main_page
180+
top_level = @store.add_file 'file.rb'
181+
top_level.comment = <<~RDOC
182+
= Heading 1
183+
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
184+
== Heading 1.1
185+
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
186+
=== Heading 1.1.1
187+
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
188+
==== Heading 1.1.1.1
189+
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
190+
== Heading 1.2
191+
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
192+
== Heading 1.3
193+
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
194+
=== Heading 1.3.1
195+
etc etc...
196+
RDOC
197+
198+
@options.title = 'My awesome Ruby project'
199+
200+
@g.generate
201+
202+
assert_file 'index.html'
203+
assert_file 'table_of_contents.html'
204+
assert_file 'js/search_index.js'
205+
206+
assert_hard_link 'css/rdoc.css'
207+
assert_hard_link 'css/fonts.css'
208+
209+
assert_hard_link 'fonts/SourceCodePro-Bold.ttf'
210+
assert_hard_link 'fonts/SourceCodePro-Regular.ttf'
211+
212+
index_html = File.binread('index.html')
213+
214+
# If there is no main page, the index page should not have a table of contents
215+
assert_not_include index_html, "<h3>Table of Contents</h3>"
216+
assert_include index_html, 'This is the API documentation for My awesome Ruby project.'
217+
end
218+
137219
def test_generate_page
138220
@store.add_file 'outer.rdoc', parser: RDoc::Parser::Simple
139221
@store.add_file 'outer/inner.rdoc', parser: RDoc::Parser::Simple

0 commit comments

Comments
 (0)