Skip to content

Commit a4d79e8

Browse files
committed
Add hide option to hide generated pages from the sidebar listing
Some pages are primarily used as a linked reference but currently still take up space in the sidebar listing. RDoc should provide a way to hide them so we don't waste space on them.
1 parent 108d617 commit a4d79e8

File tree

9 files changed

+181
-2
lines changed

9 files changed

+181
-2
lines changed

.rdoc_options

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ exclude:
1111
- CLAUDE.md
1212
- lib/rdoc/markdown.kpeg
1313

14+
hide:
15+
- CVE-2013-0256.rdoc
16+
- LEGAL.rdoc
17+
- LICENSE.rdoc
18+
1419
footer_content:
1520
DOCUMENTATION:
1621
Home: index.html

lib/rdoc/code_object/top_level.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class RDoc::TopLevel < RDoc::Context
3838

3939
attr_reader :parser
4040

41+
##
42+
# Is this page hidden from the sidebar listing?
43+
44+
attr_accessor :hidden
45+
4146
##
4247
# Creates a new TopLevel for the file at +absolute_name+. If documentation
4348
# is being generated outside the source dir +relative_name+ is relative to
@@ -49,6 +54,7 @@ def initialize(absolute_name, relative_name = absolute_name)
4954
@absolute_name = absolute_name
5055
@relative_name = relative_name
5156
@parser = nil
57+
@hidden = false
5258

5359
if relative_name
5460
@base_name = File.basename(relative_name)

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.hidden
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.hidden } %>
22

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

lib/rdoc/options.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ class RDoc::Options
168168

169169
attr_writer :exclude
170170

171+
##
172+
# Pages matching this pattern will be hidden from listings but still generated
173+
174+
attr_writer :hide
175+
171176
##
172177
# The list of files to be processed
173178

@@ -409,6 +414,7 @@ def init_ivars # :nodoc:
409414
@embed_mixins = false
410415
@exclude = []
411416
@files = nil
417+
@hide = []
412418
@force_output = false
413419
@force_update = true
414420
@generator_name = "darkfish"
@@ -461,6 +467,7 @@ def init_with(map) # :nodoc:
461467
@charset = map['charset']
462468
@embed_mixins = map['embed_mixins']
463469
@exclude = map['exclude']
470+
@hide = map['hide']
464471
@generator_name = map['generator_name']
465472
@hyperlink_all = map['hyperlink_all']
466473
@line_numbers = map['line_numbers']
@@ -497,6 +504,7 @@ def override(map) # :nodoc:
497504
@charset = map['charset'] if map.has_key?('charset')
498505
@embed_mixins = map['embed_mixins'] if map.has_key?('embed_mixins')
499506
@exclude = map['exclude'] if map.has_key?('exclude')
507+
@hide = map['hide'] if map.has_key?('hide')
500508
@generator_name = map['generator_name'] if map.has_key?('generator_name')
501509
@hyperlink_all = map['hyperlink_all'] if map.has_key?('hyperlink_all')
502510
@line_numbers = map['line_numbers'] if map.has_key?('line_numbers')
@@ -628,6 +636,19 @@ def exclude
628636
end
629637
end
630638

639+
##
640+
# Create a regexp for #hide
641+
642+
def hide
643+
if @hide.nil? || @hide.is_a?(Regexp)
644+
@hide
645+
elsif @hide.empty?
646+
nil
647+
else
648+
Regexp.new(@hide.join("|"))
649+
end
650+
end
651+
631652
##
632653
# Completes any unfinished option setup business such as filtering for
633654
# existent files, creating a regexp for #exclude and setting a default
@@ -647,6 +668,7 @@ def finish
647668
end
648669

649670
@exclude = self.exclude
671+
@hide = self.hide
650672

651673
finish_page_dir
652674

@@ -865,6 +887,15 @@ def parse(argv)
865887

866888
opt.separator nil
867889

890+
opt.on("--hide=PATTERN", "-H", Regexp,
891+
"Hide pages matching PATTERN from the",
892+
"sidebar listing. Pages are still",
893+
"generated and linkable.") do |value|
894+
@hide << value
895+
end
896+
897+
opt.separator nil
898+
868899
opt.on("--no-skipping-tests", nil,
869900
"Don't skip generating documentation for test and spec files") do |value|
870901
@skip_tests = false

lib/rdoc/rdoc.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ def parse_file(filename)
358358
cm.done_documenting = false
359359
end
360360

361+
# Mark page as hidden if it matches hide patterns
362+
if @options.hide&.match?(top_level.relative_name)
363+
top_level.hidden = true
364+
end
365+
361366
top_level
362367

363368
rescue Errno::EACCES => e
@@ -426,6 +431,32 @@ def remove_unparseable(files)
426431
end
427432
end
428433

434+
##
435+
# Validates that hide patterns match files that will actually be generated.
436+
# Emits warnings for patterns that match files that are excluded or not
437+
# included for documentation.
438+
439+
def validate_hide_patterns
440+
processed_files = @store.all_files.map(&:relative_name)
441+
442+
# Find all files that match hide patterns by checking what was actually marked hidden
443+
hidden_files = @store.all_files.select(&:hidden).map(&:relative_name)
444+
445+
# Check each file that matches hide pattern to see if it was actually processed
446+
Dir.glob("**/*").each do |file|
447+
next unless File.file?(file)
448+
next unless @options.hide.match?(file)
449+
next if hidden_files.include?(file) || processed_files.include?(file)
450+
451+
# File matches hide pattern but wasn't processed
452+
if @options.exclude&.match?(file)
453+
warn "WARNING: '#{file}' is in `hide` but won't be generated because it's also excluded"
454+
else
455+
warn "WARNING: '#{file}' is in `hide` but won't be generated because it's not included for documentation"
456+
end
457+
end
458+
end
459+
429460
##
430461
# Generates documentation or a coverage report depending upon the settings
431462
# in +options+.
@@ -466,6 +497,8 @@ def document(options)
466497

467498
file_info = parse_files @options.files
468499

500+
validate_hide_patterns if @options.hide
501+
469502
@options.default_title = "RDoc Documentation"
470503

471504
@store.complete @options.visibility

test/rdoc/rdoc_options_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def test_to_yaml
6767
'encoding' => encoding,
6868
'embed_mixins' => false,
6969
'exclude' => [],
70+
'hide' => [],
7071
'hyperlink_all' => false,
7172
'line_numbers' => false,
7273
'locale_dir' => 'locale',
@@ -968,6 +969,28 @@ def test_exclude_option_without_default
968969
assert_not_match exclude, "foo~"
969970
end
970971

972+
def test_hide_option
973+
@options.parse %w[--hide=doc/internal/.*]
974+
hide = @options.hide
975+
assert_kind_of Regexp, hide
976+
assert_match hide, "doc/internal/foo.md"
977+
assert_not_match hide, "doc/public/bar.md"
978+
end
979+
980+
def test_hide_option_multiple
981+
@options.parse %w[--hide=doc/internal/.* --hide=doc/fragments/.*]
982+
hide = @options.hide
983+
assert_kind_of Regexp, hide
984+
assert_match hide, "doc/internal/foo.md"
985+
assert_match hide, "doc/fragments/bar.md"
986+
assert_not_match hide, "doc/public/baz.md"
987+
end
988+
989+
def test_hide_option_empty
990+
@options.parse %w[]
991+
assert_nil @options.hide
992+
end
993+
971994
class DummyCoder < Hash
972995
alias add :[]=
973996
def tag=(tag)

test/rdoc/rdoc_rdoc_test.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,72 @@ def test_normalized_file_list_removes_created_rid_dir
579579
assert_empty output
580580
end
581581
end
582+
583+
def test_parse_file_hide
584+
@rdoc.store = RDoc::Store.new(@options)
585+
@rdoc.options.hide = /internal/
586+
587+
temp_dir do
588+
@rdoc.options.root = Pathname(Dir.pwd)
589+
590+
File.write 'internal.md', '# Hidden Page'
591+
592+
top_level = @rdoc.parse_file 'internal.md'
593+
594+
assert top_level.hidden, "Expected internal.md to be hidden"
595+
end
596+
end
597+
598+
def test_parse_file_not_hide
599+
@rdoc.store = RDoc::Store.new(@options)
600+
@rdoc.options.hide = /internal/
601+
602+
temp_dir do
603+
@rdoc.options.root = Pathname(Dir.pwd)
604+
605+
File.write 'public.md', '# Public Page'
606+
607+
top_level = @rdoc.parse_file 'public.md'
608+
609+
refute top_level.hidden, "Expected public.md to not be hidden"
610+
end
611+
end
612+
613+
def test_validate_hide_patterns_warns_for_excluded_file
614+
@rdoc.store = RDoc::Store.new(@options)
615+
@rdoc.options.hide = /secret\.md/
616+
@rdoc.options.exclude = /secret/
617+
618+
temp_dir do
619+
@rdoc.options.root = Pathname(Dir.pwd)
620+
621+
File.write 'secret.md', '# Secret'
622+
623+
_, err = capture_output do
624+
@rdoc.validate_hide_patterns
625+
end
626+
627+
assert_match(/secret\.md.*hide.*exclude/, err)
628+
end
629+
end
630+
631+
def test_validate_hide_patterns_warns_for_unprocessed_file
632+
@rdoc.store = RDoc::Store.new(@options)
633+
@rdoc.options.hide = /notincluded\.md/
634+
635+
temp_dir do
636+
@rdoc.options.root = Pathname(Dir.pwd)
637+
638+
# Create a file that won't be processed (not in .document)
639+
FileUtils.mkdir 'subdir'
640+
File.write 'subdir/notincluded.md', '# Not included'
641+
File.write 'subdir/.document', '' # empty .document excludes everything
642+
643+
_, err = capture_output do
644+
@rdoc.validate_hide_patterns
645+
end
646+
647+
assert_match(/notincluded\.md.*hide.*not included for documentation/, err)
648+
end
649+
end
582650
end

test/rdoc/rdoc_top_level_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,17 @@ def test_text_eh_no_parser
281281
refute rd.text?
282282
end
283283

284+
def test_hidden_default
285+
t = RDoc::TopLevel.new 'path/file.rb'
286+
287+
refute t.hidden
288+
end
289+
290+
def test_hidden_set
291+
t = RDoc::TopLevel.new 'path/file.rb'
292+
t.hidden = true
293+
294+
assert t.hidden
295+
end
296+
284297
end

0 commit comments

Comments
 (0)