Skip to content

Display main page's Table of Contents when viewed from the index page #1250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,14 @@ def generate_index
asset_rel_prefix = rel_prefix + @asset_rel_path

@title = @options.title
@main_page = @files.find { |f| f.full_name == @options.main_page }

render_template template_file, out_file do |io|
here = binding
# suppress 1.9.3 warning
here.local_variable_set(:asset_rel_prefix, asset_rel_prefix)
# some partials rely on the presence of current variable to render
here.local_variable_set(:current, @main_page) if @main_page
here
end
rescue => e
Expand Down
8 changes: 4 additions & 4 deletions lib/rdoc/generator/template/darkfish/index.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<%= render '_sidebar_search.rhtml' %>
</div>

<%= render '_sidebar_table_of_contents.rhtml' if defined?(current) %>
<%= render '_sidebar_pages.rhtml' %>
<%= render '_sidebar_classes.rhtml' %>

<%= render '_footer.rhtml' %>
</nav>

<main role="main">
<%- if @options.main_page and
main_page = @files.find { |f| f.full_name == @options.main_page } then %>
<%= main_page.description %>
<%- if @main_page %>
<%= @main_page.description %>
<%- else -%>
<p>This is the API documentation for <%= h @title %>.
<p>This is the API documentation for <%= h @title %>.
<%- end -%>
</main>
82 changes: 82 additions & 0 deletions test/rdoc/test_rdoc_generator_darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,88 @@ def test_generate
)
end

def test_generate_index_with_main_page
top_level = @store.add_file 'file.rb'
top_level.comment = <<~RDOC
= Heading 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
== Heading 1.1
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
=== Heading 1.1.1
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
==== Heading 1.1.1.1
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
== Heading 1.2
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
== Heading 1.3
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
=== Heading 1.3.1
etc etc...
RDOC

@options.main_page = 'file.rb'
@options.title = 'My awesome Ruby project'

@g.generate

assert_file 'index.html'
assert_file 'table_of_contents.html'
assert_file 'js/search_index.js'

assert_hard_link 'css/rdoc.css'
assert_hard_link 'css/fonts.css'

assert_hard_link 'fonts/SourceCodePro-Bold.ttf'
assert_hard_link 'fonts/SourceCodePro-Regular.ttf'

index_html = File.binread('index.html')

assert_include index_html, "<h3>Table of Contents</h3>"
assert_include index_html, '<h1 id="label-Heading+1">Heading 1'
# When there's a main page, the default description should not be shown
assert_not_include index_html, 'This is the API documentation for My awesome Ruby project.'
end

def test_generate_index_without_main_page
top_level = @store.add_file 'file.rb'
top_level.comment = <<~RDOC
= Heading 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
== Heading 1.1
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
=== Heading 1.1.1
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
==== Heading 1.1.1.1
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
== Heading 1.2
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
== Heading 1.3
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
=== Heading 1.3.1
etc etc...
RDOC

@options.title = 'My awesome Ruby project'

@g.generate

assert_file 'index.html'
assert_file 'table_of_contents.html'
assert_file 'js/search_index.js'

assert_hard_link 'css/rdoc.css'
assert_hard_link 'css/fonts.css'

assert_hard_link 'fonts/SourceCodePro-Bold.ttf'
assert_hard_link 'fonts/SourceCodePro-Regular.ttf'

index_html = File.binread('index.html')

# If there is no main page, the index page should not have a table of contents
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not though? Is there a reason why we can't or don't want to display the table of contents every time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of main page here means it'll literally only have one title This is the API documentation for My awesome Ruby project. and nothing else.

assert_not_include index_html, "<h3>Table of Contents</h3>"
assert_include index_html, 'This is the API documentation for My awesome Ruby project.'
end

def test_generate_page
@store.add_file 'outer.rdoc', parser: RDoc::Parser::Simple
@store.add_file 'outer/inner.rdoc', parser: RDoc::Parser::Simple
Expand Down
Loading