diff --git a/lib/rdoc/markup/verbatim.rb b/lib/rdoc/markup/verbatim.rb index 606276dcf0..0eb67e9211 100644 --- a/lib/rdoc/markup/verbatim.rb +++ b/lib/rdoc/markup/verbatim.rb @@ -1,83 +1,74 @@ # frozen_string_literal: true -## -# A section of verbatim text -class RDoc::Markup::Verbatim < RDoc::Markup::Raw - - ## - # Format of this verbatim section - - attr_accessor :format - - def initialize *parts # :nodoc: - super - - @format = nil - end - - def ==(other) # :nodoc: - super and @format == other.format - end - - ## - # Calls #accept_verbatim on +visitor+ - - def accept(visitor) - visitor.accept_verbatim self - end - - ## - # Collapses 3+ newlines into two newlines - - def normalize - parts = [] - - newlines = 0 +module RDoc + class Markup + # A section of verbatim text + class Verbatim < Raw + # Format of this verbatim section + #: Symbol? + attr_accessor :format + + #: (*String) -> void + def initialize(*parts) # :nodoc: + super + @format = nil + end - @parts.each do |part| - case part - when /^\s*\n/ then - newlines += 1 - parts << part if newlines == 1 - else - newlines = 0 - parts << part + #: (top) -> bool + def ==(other) # :nodoc: + super && @format == other.format end - end - parts.pop if parts.last =~ /\A\r?\n\z/ + # Calls #accept_verbatim on +visitor+ + # @override + #: (untyped) -> void + def accept(visitor) + visitor.accept_verbatim self + end - @parts = parts - end + # Collapses 2+ newlines into a single one + #: () -> void + def normalize + # Chunk the parts into groups of entries that are only line breaks and other content. For line break groups, + # take only 1 + parts = @parts + .chunk { |part| part.match?(/^\s*\n/) } + .flat_map { |is_newline, group| is_newline ? group.take(1) : group } + + # Remove any trailing line breaks + parts.pop if @parts.last&.match?(/\A\r?\n\z/) + @parts = parts + end - def pretty_print(q) # :nodoc: - self.class.name =~ /.*::(\w{1,4})/i + # @override + #: (PP) -> void + def pretty_print(q) # :nodoc: + self.class.name =~ /.*::(\w{1,4})/i + + q.group(2, "[#{$1.downcase}: ", ']') do + if @format + q.text("format: #{@format}") + q.breakable + end + + q.seplist(@parts) do |part| + q.pp(part) + end + end + end - q.group 2, "[#{$1.downcase}: ", ']' do - if @format then - q.text "format: #{@format}" - q.breakable + # Is this verbatim section Ruby code? + #: () -> bool + def ruby? + @format ||= nil # TODO for older ri data, switch the tree to marshal_dump + @format == :ruby end - q.seplist @parts do |part| - q.pp part + # The text of the section + #: () -> String + def text + @parts.join end end end - - ## - # Is this verbatim section Ruby code? - - def ruby? - @format ||= nil # TODO for older ri data, switch the tree to marshal_dump - @format == :ruby - end - - ## - # The text of the section - - def text - @parts.join - end - end diff --git a/test/rdoc/markup/parser_test.rb b/test/rdoc/markup/parser_test.rb index 8b3884f031..f21fb2a7bc 100644 --- a/test/rdoc/markup/parser_test.rb +++ b/test/rdoc/markup/parser_test.rb @@ -1637,21 +1637,6 @@ def test_tokenize_verbatim_rule_fancy assert_equal expected, @RMP.tokenize(str) end - # HACK move to Verbatim test case - def test_verbatim_normalize - v = @RM::Verbatim.new "foo\n", "\n", "\n", "bar\n" - - v.normalize - - assert_equal ["foo\n", "\n", "bar\n"], v.parts - - v = @RM::Verbatim.new "foo\n", "\n" - - v.normalize - - assert_equal ["foo\n"], v.parts - end - def test_unget parser = util_parser diff --git a/test/rdoc/markup/verbatim_test.rb b/test/rdoc/markup/verbatim_test.rb index 59b40abaea..5ba86c57e9 100644 --- a/test/rdoc/markup/verbatim_test.rb +++ b/test/rdoc/markup/verbatim_test.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative '../helper' -class RDocMarkupVerbatimTest < RDoc::TestCase +require_relative "../helper" +class RDocMarkupVerbatimTest < RDoc::TestCase def test_equals2 v1 = verb('1 + 1') v2 = verb('1 + 1') @@ -11,19 +11,25 @@ def test_equals2 v4.format = :ruby assert_equal v1, v2 - refute_equal v1, v3 refute_equal v1, v4 end def test_ruby_eh verbatim = RDoc::Markup::Verbatim.new - refute verbatim.ruby? verbatim.format = :ruby - assert verbatim.ruby? end + def test_normalize + verbatim = verb("a\n", " \n", " \n", " \n", " \n", "more text\n") + verbatim.normalize + assert_equal(["a\n", " \n", "more text\n"], verbatim.parts) + + verbatim = verb("foo\n", "\n") + verbatim.normalize + assert_equal(["foo\n"], verbatim.parts) + end end