* lib/yaml/baseemitter.rb (indent_text): was forcing a mod value

of zero at times, which kept some blocks from getting indentation.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6333 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
why 2004-05-16 15:29:30 +00:00
parent 246b33d108
commit 620549da3f
2 changed files with 120 additions and 115 deletions

View File

@ -1,3 +1,8 @@
Mon May 17 00:36:21 2004 why the lucky stiff <why@ruby-lang.org>
* lib/yaml/baseemitter.rb (indent_text): was forcing a mod value
of zero at times, which kept some blocks from getting indentation.
Mon May 17 00:07:00 2004 Gavin Sinclair <gsinclair@soyabean.com.au> Mon May 17 00:07:00 2004 Gavin Sinclair <gsinclair@soyabean.com.au>
* lib/drb/drb.rb: Cosmetic documentation changes. * lib/drb/drb.rb: Cosmetic documentation changes.

View File

@ -30,121 +30,121 @@ module YAML
self.node_text( [value].pack("m"), '|' ) self.node_text( [value].pack("m"), '|' )
end end
# #
# Emit plain, normal flowing text # Emit plain, normal flowing text
# #
def node_text( value, block = '>' ) def node_text( value, block = nil )
@seq_map = false @seq_map = false
valx = value.dup valx = value.dup
unless block unless block
block = block =
if options(:UseBlock) if options(:UseBlock)
'|' '|'
elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/ elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
'|' '|'
else else
'>' '>'
end end
if valx =~ /\A[ \t#]/ if valx =~ /\A[ \t#]/
block += options(:Indent).to_s block += options(:Indent).to_s
end end
block += block +=
if valx =~ /\n\Z\n/ if valx =~ /\n\Z\n/
"+" "+"
elsif valx =~ /\Z\n/ elsif valx =~ /\Z\n/
"" ""
else else
"-" "-"
end end
end
if valx =~ /#{YAML::ESCAPE_CHAR}/
valx = YAML::escape( valx )
end
if block[0] == ?>
valx = fold( valx )
end end
if valx =~ /#{YAML::ESCAPE_CHAR}/
valx = YAML::escape( valx )
end
if block[0] == ?>
valx = fold( valx )
end
indt = nil indt = nil
indt = $&.to_i if block =~ /\d+/ indt = $&.to_i if block =~ /\d+/
self << block + indent_text( valx, indt ) + "\n" #p [block, indt]
end self << block + indent_text( valx, indt ) + "\n"
end
# #
# Emit a simple, unqouted string # Emit a simple, unqouted string
# #
def simple( value ) def simple( value )
@seq_map = false @seq_map = false
self << value.to_s self << value.to_s
end end
# #
# Emit double-quoted string # Emit double-quoted string
# #
def double( value ) def double( value )
"\"#{YAML.escape( value )}\"" "\"#{YAML.escape( value )}\""
end end
# #
# Emit single-quoted string # Emit single-quoted string
# #
def single( value ) def single( value )
"'#{value}'" "'#{value}'"
end end
# #
# Write a text block with the current indent # Write a text block with the current indent
# #
def indent_text( text, indt = nil ) def indent_text( text, mod = nil )
return "" if text.to_s.empty? return "" if text.to_s.empty?
indt ||= 0 spacing = indent( mod )
spacing = indent( indt ) return "\n" + text.gsub( /^([^\n])/, "#{spacing}\\1" )
return "\n" + text.gsub( /^([^\n])/, "#{spacing}\\1" ) end
end
# #
# Write a current indent # Write a current indent
# #
def indent( mod = nil ) def indent( mod = nil )
#p [ self.id, @level, :INDENT ] #p [ self.id, level, mod, :INDENT ]
if level.zero? if level <= 0
mod ||= 0 mod ||= 0
else else
mod ||= options(:Indent) mod ||= options(:Indent)
mod += ( level - 1 ) * options(:Indent) mod += ( level - 1 ) * options(:Indent)
end end
return " " * mod return " " * mod
end end
# #
# Add indent to the buffer # Add indent to the buffer
# #
def indent! def indent!
self << indent self << indent
end end
# #
# Folding paragraphs within a column # Folding paragraphs within a column
# #
def fold( value ) def fold( value )
value.gsub!( /\A\n+/, '' ) value.gsub!( /\A\n+/, '' )
folded = $&.to_s folded = $&.to_s
width = (0..options(:BestWidth)) width = (0..options(:BestWidth))
while not value.empty? while not value.empty?
last = value.index( /(\n+)/ ) last = value.index( /(\n+)/ )
chop_s = false chop_s = false
if width.include?( last ) if width.include?( last )
last += $1.length - 1 last += $1.length - 1
elsif width.include?( value.length ) elsif width.include?( value.length )
last = value.length last = value.length
else else
last = value.rindex( /[ \t]/, options(:BestWidth) ) last = value.rindex( /[ \t]/, options(:BestWidth) )
chop_s = true chop_s = true
end end
folded += value.slice!( 0, width.include?( last ) ? last + 1 : options(:BestWidth) ) folded += value.slice!( 0, width.include?( last ) ? last + 1 : options(:BestWidth) )
folded.chop! if chop_s folded.chop! if chop_s
folded += "\n" unless value.empty? folded += "\n" unless value.empty?
end end
folded folded
end end
# #
# Quick mapping # Quick mapping
@ -152,18 +152,18 @@ module YAML
def map( type, &e ) def map( type, &e )
val = Mapping.new val = Mapping.new
e.call( val ) e.call( val )
self << "#{type} " if type.length.nonzero? self << "#{type} " if type.length.nonzero?
# #
# Empty hashes # Empty hashes
# #
if val.length.zero? if val.length.zero?
self << "{}" self << "{}"
@seq_map = false @seq_map = false
else else
# FIXME # FIXME
# if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
# @headless = 1 # @headless = 1
# end # end
defkey = @options.delete( :DefaultKey ) defkey = @options.delete( :DefaultKey )
@ -173,9 +173,9 @@ module YAML
defkey.to_yaml( :Emitter => self ) defkey.to_yaml( :Emitter => self )
end end
# #
# Emit the key and value # Emit the key and value
# #
val.each { |v| val.each { |v|
seq_map_shortcut seq_map_shortcut
if v[0].is_complex_yaml? if v[0].is_complex_yaml?
@ -189,7 +189,7 @@ module YAML
self << ": " self << ": "
v[1].to_yaml( :Emitter => self ) v[1].to_yaml( :Emitter => self )
} }
end end
end end
def seq_map_shortcut def seq_map_shortcut
@ -210,22 +210,22 @@ module YAML
@seq_map = false @seq_map = false
val = Sequence.new val = Sequence.new
e.call( val ) e.call( val )
self << "#{type} " if type.length.nonzero? self << "#{type} " if type.length.nonzero?
# #
# Empty arrays # Empty arrays
# #
if val.length.zero? if val.length.zero?
self << "[]" self << "[]"
else else
# FIXME # FIXME
# if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
# @headless = 1 # @headless = 1
# end # end
# #
# Emit the key and value # Emit the key and value
# #
val.each { |v| val.each { |v|
self << "\n" self << "\n"
indent! indent!
@ -233,7 +233,7 @@ module YAML
@seq_map = true if v.class == Hash @seq_map = true if v.class == Hash
v.to_yaml( :Emitter => self ) v.to_yaml( :Emitter => self )
} }
end end
end end
end end