Skip to content
Open
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
129 changes: 60 additions & 69 deletions lib/rdoc/markup/verbatim.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 0 additions & 15 deletions test/rdoc/markup/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 11 additions & 5 deletions test/rdoc/markup/verbatim_test.rb
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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