Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reimplement heading suppression for compatibility

Changing the definition RDoc::Markup::Heading causes marshal load
errors.  This implementation implements it through a new field on
RDoc::Markup::Document.  This implementation also decreases the size of
dumped documents compared to Struct changes.
  • Loading branch information...
commit 65836c9a02e73b1de526c8ce3f18cd392151ef51 1 parent 9e496e7
@drbrain drbrain authored
View
16 lib/rdoc/markup/document.rb
@@ -12,6 +12,12 @@ class RDoc::Markup::Document
attr_reader :file
##
+ # If a heading is below the given level it will be omitted from the
+ # table_of_contents
+
+ attr_accessor :omit_headings_below
+
+ ##
# The parts of the Document
attr_reader :parts
@@ -24,6 +30,7 @@ def initialize *parts
@parts.concat parts
@file = nil
+ @omit_headings_from_table_of_contents_below = nil
end
##
@@ -57,14 +64,7 @@ def == other # :nodoc:
def accept visitor
visitor.start_accepting
- @parts.each do |item|
- case item
- when RDoc::Markup::Document then # HACK
- visitor.accept_document item
- else
- item.accept visitor
- end
- end
+ visitor.accept_document self
visitor.end_accepting
end
View
7 lib/rdoc/markup/formatter.rb
@@ -42,7 +42,12 @@ def initialize options, markup = nil
def accept_document document
document.parts.each do |item|
- item.accept self
+ case item
+ when RDoc::Markup::Document then # HACK
+ accept_document item
+ else
+ item.accept self
+ end
end
end
View
2  lib/rdoc/markup/heading.rb
@@ -2,7 +2,7 @@
# A heading with a level (1-6) and text
RDoc::Markup::Heading =
- Struct.new :level, :text, :omit_from_table_of_contents do
+ Struct.new :level, :text do
@to_html = nil
@to_label = nil
View
28 lib/rdoc/markup/to_table_of_contents.rb
@@ -18,15 +18,31 @@ def self.to_toc
attr_reader :res
+ ##
+ # Omits headings with a level less than the given level.
+
+ attr_accessor :omit_headings_below
+
def initialize # :nodoc:
super nil
+
+ @omit_headings_below = nil
+ end
+
+ ##
+ # Adds +document+ to the output, using its heading cutoff if present
+
+ def accept_document document
+ @omit_headings_below = document.omit_headings_below
+
+ super
end
##
# Adds +heading+ to the table of contents
def accept_heading heading
- @res << heading unless heading.omit_from_table_of_contents
+ @res << heading unless suppressed? heading
end
##
@@ -40,9 +56,19 @@ def end_accepting
# Prepares the visitor for text generation
def start_accepting
+ @omit_headings_below = nil
@res = []
end
+ ##
+ # Returns true if +heading+ is below the display threshold
+
+ def suppressed? heading
+ return false unless @omit_headings_below
+
+ heading.level > @omit_headings_below
+ end
+
# :stopdoc:
alias accept_block_quote ignore
alias accept_raw ignore
View
4 lib/rdoc/parser/changelog.rb
@@ -23,6 +23,7 @@ def continue_entry_body entry_body, continuation
def create_document groups
doc = RDoc::Markup::Document.new
+ doc.omit_headings_below = 2
doc.file = @top_level
doc << RDoc::Markup::Heading.new(1, File.basename(@file_name))
@@ -42,8 +43,7 @@ def create_entries entries
out = []
entries.each do |entry, items|
- heading = RDoc::Markup::Heading.new 3, entry, true
- out << heading
+ out << RDoc::Markup::Heading.new(3, entry)
out << RDoc::Markup::BlankLine.new
out << create_items(items)
View
4 lib/rdoc/test_case.rb
@@ -91,8 +91,8 @@ def hard_break
##
# Shortcut for RDoc::Markup::Heading.new with +level+ and +text+
- def head level, text, omit_from_table_of_contents = nil
- @RM::Heading.new level, text, omit_from_table_of_contents
+ def head level, text
+ @RM::Heading.new level, text
end
##
View
19 test/test_rdoc_markup_document.rb
@@ -191,5 +191,24 @@ def test_table_of_contents
assert_equal expected, doc.table_of_contents
end
+ def test_table_of_contents_omit_headings_below
+ document = doc(
+ head(1, 'A'),
+ para('B'),
+ head(2, 'C'),
+ para('D'),
+ head(1, 'E'),
+ para('F'))
+
+ document.omit_headings_below = 1
+
+ expected = [
+ head(1, 'A'),
+ head(1, 'E'),
+ ]
+
+ assert_equal expected, document.table_of_contents
+ end
+
end
View
21 test/test_rdoc_markup_to_table_of_contents.rb
@@ -91,16 +91,35 @@ def accept_heading_suppressed_crossref
alias list_verbatim empty
alias start_accepting empty
+ def test_accept_document_omit_headings_below
+ document = doc
+ document.omit_headings_below = 2
+
+ @to.accept_document document
+
+ assert_equal 2, @to.omit_headings_below
+ end
+
def test_accept_heading_suppressed
@to.start_accepting
+ @to.omit_headings_below = 4
suppressed = head 5, 'Hello'
- suppressed.omit_from_table_of_contents = true
@to.accept_heading suppressed
assert_empty @to.res
+ end
+
+ def test_suppressed_eh
+ @to.omit_headings_below = nil
+
+ refute @to.suppressed? head(1, '')
+
+ @to.omit_headings_below = 1
+ refute @to.suppressed? head(1, '')
+ assert @to.suppressed? head(2, '')
end
end
View
39 test/test_rdoc_parser_changelog.rb
@@ -82,21 +82,15 @@ def test_create_document
blank_line,
head(2, '2012-12-04'),
blank_line,
- head(3,
- 'Tue Dec 4 08:33:46 2012 Eric Hodel <drbrain@segment7.net>',
- true),
+ head(3, 'Tue Dec 4 08:33:46 2012 Eric Hodel <drbrain@segment7.net>'),
blank_line,
list(:NOTE, item('a', para('one')), item('b', para('two'))),
- head(3,
- 'Tue Dec 4 08:32:10 2012 Eric Hodel <drbrain@segment7.net>',
- true),
+ head(3, 'Tue Dec 4 08:32:10 2012 Eric Hodel <drbrain@segment7.net>'),
blank_line,
list(:NOTE, item('c', para('three')), item('d', para('four'))),
head(2, '2012-12-03'),
blank_line,
- head(3,
- 'Mon Dec 3 20:28:02 2012 Koichi Sasada <ko1@atdot.net>',
- true),
+ head(3, 'Mon Dec 3 20:28:02 2012 Koichi Sasada <ko1@atdot.net>'),
blank_line,
list(:NOTE, item('e', para('five')), item('f', para('six'))))
@@ -106,6 +100,8 @@ def test_create_document
assert_equal expected, document
+ assert_equal 2, document.omit_headings_below
+
headings = document.parts.select do |part|
RDoc::Markup::Heading === part and part.level == 2
end
@@ -124,26 +120,16 @@ def test_create_entries
]
expected = [
- head(3,
- 'Tue Dec 1 02:03:04 2012 Eric Hodel <drbrain@segment7.net>',
- true),
+ head(3, 'Tue Dec 1 02:03:04 2012 Eric Hodel <drbrain@segment7.net>'),
blank_line,
list(:NOTE, item('a', para('one')), item('b', para('two'))),
- head(3,
- 'Tue Dec 5 06:07:08 2012 Eric Hodel <drbrain@segment7.net>',
- true),
+ head(3, 'Tue Dec 5 06:07:08 2012 Eric Hodel <drbrain@segment7.net>'),
blank_line,
list(:NOTE, item('c', para('three')), item('d', para('four'))),
]
entries = parser.create_entries(entries)
assert_equal expected, entries
-
- headings = entries.select do |part|
- RDoc::Markup::Heading === part and part.level == 3
- end
-
- assert headings.all? { |heading| heading.omit_from_table_of_contents }
end
def test_create_entries_colons
@@ -156,8 +142,7 @@ def test_create_entries_colons
expected = [
head(3,
- 'Wed Dec 5 12:17:11 2012 Naohisa Goto <ngotogenome@gmail.com>',
- true),
+ 'Wed Dec 5 12:17:11 2012 Naohisa Goto <ngotogenome@gmail.com>'),
blank_line,
list(:NOTE,
item('func.rb (DL::Function#bind)',
@@ -282,9 +267,7 @@ def test_scan
blank_line,
head(2, '2012-12-04'),
blank_line,
- head(3,
- 'Tue Dec 4 08:32:10 2012 Eric Hodel <drbrain@segment7.net>',
- true),
+ head(3, 'Tue Dec 4 08:32:10 2012 Eric Hodel <drbrain@segment7.net>'),
blank_line,
list(:NOTE,
item('lib/rdoc/ri/driver.rb', para('Fixed ri page display for ' +
@@ -292,9 +275,7 @@ def test_scan
item('test/rdoc/test_rdoc_ri_driver.rb', para('Test for above'))),
head(2, '2012-12-03'),
blank_line,
- head(3,
- 'Mon Dec 3 20:37:22 2012 Koichi Sasada <ko1@atdot.net>',
- true),
+ head(3, 'Mon Dec 3 20:37:22 2012 Koichi Sasada <ko1@atdot.net>'),
blank_line,
list(:NOTE,
item('vm_exec.c', para('check VM_COLLECT_USAGE_DETAILS.'))))
Please sign in to comment.
Something went wrong with that request. Please try again.