Skip to content

Commit 92b07d6

Browse files
authored
Stop generating a separate page file for main_page (#1624)
When a page is used as `main_page` (e.g. `README.md`), its content is already displayed on index.html. Generating a separate page file (e.g. `README_md.html`) is redundant. Skip the main_page in: - darkfish generator (page file generation) - json search index (avoids linking to a nonexistent page) - aliki sidebar pages template
1 parent 44b6976 commit 92b07d6

File tree

8 files changed

+177
-4
lines changed

8 files changed

+177
-4
lines changed

lib/rdoc/code_object/top_level.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,15 @@ def object_class
221221
# Path to this file for use with HTML generator output.
222222

223223
def path
224+
base = if options.main_page == full_name
225+
'index.html'
226+
else
227+
http_url
228+
end
229+
224230
prefix = options.file_path_prefix
225-
return http_url unless prefix
226-
File.join(prefix, http_url)
231+
return base unless prefix
232+
File.join(prefix, base)
227233
end
228234

229235
def pretty_print(q) # :nodoc:

lib/rdoc/generator/darkfish.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ def generate_file_files
393393
@files.each do |file|
394394
current = file
395395

396+
next if file.text? && file.full_name == @options.main_page
397+
396398
if file.text? and page_file.exist? then
397399
generate_page file
398400
next

lib/rdoc/generator/json_index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def index_pages
250250
debug_msg " generating pages search index"
251251

252252
pages = @files.select do |file|
253-
file.text?
253+
file.text? && file.full_name != @options.main_page
254254
end
255255

256256
pages.each do |page|

lib/rdoc/generator/template/aliki/_sidebar_pages.rhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- simple_files = @files.select { |f| f.text? } %>
1+
<%- simple_files = @files.select { |f| f.text? && f.full_name != @options.main_page } %>
22

33
<%- if defined?(current) && current.respond_to?(:page_name) %>
44
<%- dir = current.full_name[%r{\A[^/]+(?=/)}] || current.page_name %>

test/rdoc/generator/darkfish_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,60 @@ def test_generate_index_with_main_page
178178
assert_not_include index_html, 'This is the API documentation for My awesome Ruby project.'
179179
end
180180

181+
def test_generate_does_not_create_page_file_for_main_page
182+
top_level = @store.add_file("README.rdoc", parser: RDoc::Parser::Simple)
183+
top_level.comment = "= Main Page\nThis is the main page content."
184+
185+
other_page = @store.add_file("OTHER.rdoc", parser: RDoc::Parser::Simple)
186+
other_page.comment = "= Other Page\nThis is another page."
187+
188+
@options.main_page = "README.rdoc"
189+
190+
@g.generate
191+
192+
assert_file "index.html"
193+
refute File.exist?("README_rdoc.html"), "main_page should not be generated as a separate page"
194+
assert_file "OTHER_rdoc.html"
195+
end
196+
197+
def test_generate_sidebar_links_main_page_to_index_html
198+
top_level = @store.add_file("README.rdoc", parser: RDoc::Parser::Simple)
199+
top_level.comment = "= Main Page\nThis is the main page content."
200+
201+
other_page = @store.add_file("OTHER.rdoc", parser: RDoc::Parser::Simple)
202+
other_page.comment = "= Other Page\nThis is another page."
203+
204+
@options.main_page = "README.rdoc"
205+
@store.options.main_page = "README.rdoc"
206+
207+
@g.generate
208+
209+
other_html = File.binread("OTHER_rdoc.html")
210+
211+
# The sidebar should link README to index.html, not README_rdoc.html
212+
assert_match %r{href="[^"]*index\.html"[^>]*>\s*README}m, other_html
213+
assert_not_match %r{href="[^"]*README_rdoc\.html"}, other_html
214+
end
215+
216+
def test_generate_cross_reference_to_main_page_links_to_index_html
217+
readme = @store.add_file("README.rdoc", parser: RDoc::Parser::Simple)
218+
readme.comment = "= Main Page\nThis is the main page content."
219+
220+
other_page = @store.add_file("OTHER.rdoc", parser: RDoc::Parser::Simple)
221+
other_page.comment = "= Other Page\nSee README.rdoc for more info."
222+
223+
@options.main_page = "README.rdoc"
224+
@store.options.main_page = "README.rdoc"
225+
226+
@g.generate
227+
228+
other_html = File.binread("OTHER_rdoc.html")
229+
230+
# Cross-reference to main_page should point to index.html
231+
assert_match %r{<a href="[^"]*index\.html">README</a>}, other_html
232+
assert_not_match %r{<a href="[^"]*README_rdoc\.html">README</a>}, other_html
233+
end
234+
181235
def test_generate_index_without_main_page
182236
top_level = @store.add_file 'file.rb'
183237
top_level.comment = <<~RDOC

test/rdoc/generator/json_index_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,22 @@ def test_index_pages
356356
assert_equal expected, @g.index
357357
end
358358

359+
def test_index_pages_excludes_main_page
360+
@options.main_page = "page.rdoc"
361+
362+
@g.reset @top_levels, @klasses
363+
364+
@g.index_pages
365+
366+
expected = {
367+
searchIndex: [],
368+
longSearchIndex: [],
369+
info: [],
370+
}
371+
372+
assert_equal expected, @g.index
373+
end
374+
359375
def test_search_string
360376
assert_equal 'cd', @g.search_string('C d')
361377
end

test/rdoc/markup/to_html_crossref_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def test_convert_CROSSREF_label_for_md
8484
assert_equal para("<a href=\"EXAMPLE_md.html#foo\">foo at <code>EXAMPLE</code></a>"), result
8585
end
8686

87+
def test_convert_CROSSREF_label_for_main_page
88+
@options.main_page = 'EXAMPLE.md'
89+
90+
result = @to.convert 'EXAMPLE@foo'
91+
assert_equal para("<a href=\"index.html#foo\">foo at <code>EXAMPLE</code></a>"), result
92+
end
93+
8794
def test_convert_CROSSREF_label_period
8895
result = @to.convert 'C1@foo.'
8996
assert_equal para("<a href=\"C1.html#class-c1-foo\">foo at <code>C1</code></a>."), result
@@ -299,6 +306,20 @@ def test_handle_regexp_HYPERLINK_rdoc
299306
assert_equal '<a href="README_txt.html">README.txt</a>', link
300307
end
301308

309+
def test_handle_regexp_HYPERLINK_rdoc_main_page
310+
readme = @store.add_file 'README.txt'
311+
readme.parser = RDoc::Parser::Simple
312+
313+
@options.main_page = 'README.txt'
314+
315+
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c2,
316+
hyperlink_all: true, warn_missing_rdoc_ref: true
317+
318+
link = @to.handle_regexp_HYPERLINK hyper 'README.txt'
319+
320+
assert_equal '<a href="index.html">README.txt</a>', link
321+
end
322+
302323
def test_handle_TIDYLINK_rdoc
303324
readme = @store.add_file 'README.txt'
304325
readme.parser = RDoc::Parser::Simple
@@ -323,6 +344,20 @@ def test_handle_TIDYLINK_rdoc
323344
assert_equal '<a href="README_txt.html">tidy</a>', link
324345
end
325346

347+
def test_handle_TIDYLINK_rdoc_main_page
348+
readme = @store.add_file 'README.txt'
349+
readme.parser = RDoc::Parser::Simple
350+
351+
@options.main_page = 'README.txt'
352+
353+
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c2,
354+
hyperlink_all: true, warn_missing_rdoc_ref: true
355+
356+
link = @to.to_html tidy 'README.txt'
357+
358+
assert_equal '<a href="index.html">tidy</a>', link
359+
end
360+
326361
def test_handle_regexp_TIDYLINK_label
327362
link = @to.to_html tidy 'C1#m@foo'
328363

test/rdoc/rdoc_top_level_test.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,53 @@ def test_http_url
157157
assert_equal 'path_other/level_rb.html', other_level.http_url
158158
end
159159

160+
def test_http_url_unaffected_by_main_page
161+
page = @store.add_file 'README.md'
162+
page.parser = RDoc::Parser::Simple
163+
164+
@store.main = 'README.md'
165+
166+
# http_url always returns the file-based URL; main_page redirect is in #path
167+
assert_equal 'README_md.html', page.http_url
168+
end
169+
160170
def test_path
161171
assert_equal 'path/top_level_rb.html', @top_level.path
162172

163173
@options.file_path_prefix = 'file'
164174
assert_equal 'file/path/top_level_rb.html', @top_level.path
165175
end
166176

177+
def test_path_main_page
178+
page = @store.add_file 'README.md'
179+
page.parser = RDoc::Parser::Simple
180+
181+
@options.main_page = 'README.md'
182+
183+
assert_equal 'index.html', page.path
184+
end
185+
186+
def test_path_main_page_with_prefix
187+
page = @store.add_file 'README.md'
188+
page.parser = RDoc::Parser::Simple
189+
190+
@options.main_page = 'README.md'
191+
@options.file_path_prefix = 'file'
192+
193+
assert_equal 'file/index.html', page.path
194+
end
195+
196+
def test_path_non_main_page_unaffected
197+
page = @store.add_file 'README.md'
198+
page.parser = RDoc::Parser::Simple
199+
other = @store.add_file 'OTHER.md'
200+
other.parser = RDoc::Parser::Simple
201+
202+
@options.main_page = 'README.md'
203+
204+
assert_equal 'OTHER_md.html', other.path
205+
end
206+
167207
def test_marshal_dump
168208
page = @store.add_file 'README.txt'
169209
page.parser = RDoc::Parser::Simple
@@ -259,6 +299,26 @@ def test_search_record_page
259299
assert_equal expected, page.search_record
260300
end
261301

302+
def test_search_record_main_page
303+
page = @store.add_file 'README.txt'
304+
page.parser = RDoc::Parser::Simple
305+
page.comment = 'This is a comment.'
306+
307+
@options.main_page = 'README.txt'
308+
309+
expected = [
310+
'README',
311+
'',
312+
'README',
313+
'',
314+
'index.html',
315+
'',
316+
"<p>This is a comment.\n",
317+
]
318+
319+
assert_equal expected, page.search_record
320+
end
321+
262322
def test_text_eh
263323
refute @xref_data.text?
264324

0 commit comments

Comments
 (0)