From c474911e5b78749c5a85db39256240db5fb9e3be Mon Sep 17 00:00:00 2001 From: why Date: Sat, 15 May 2004 03:11:28 +0000 Subject: * lib/yaml.rb: removed fallback to pure Ruby parser. * lib/yaml/baseemitter.rb (node_text): rewriting folded scalars. * ext/syck/syck.h: reports style of scalars now, be they plain, block single-, or double-quoted. * ext/syck/syck.c: ditto. * ext/syck/gram.c: ditto. * ext/syck/node.c: ditto. * ext/syck/token.c: ditto. * ext/syck/rubyext.c (yaml_org_handler): symbols loaded only if scalar style is plain. * test/yaml/test_yaml.rb (test_perl_regexp): updated test to match new regexp serialization. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6315 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/yaml/baseemitter.rb | 249 +++++++++++++++++++++++++----------------------- lib/yaml/rubytypes.rb | 8 +- 2 files changed, 132 insertions(+), 125 deletions(-) (limited to 'lib/yaml') diff --git a/lib/yaml/baseemitter.rb b/lib/yaml/baseemitter.rb index 1072f75533..c2c68752a8 100644 --- a/lib/yaml/baseemitter.rb +++ b/lib/yaml/baseemitter.rb @@ -30,114 +30,121 @@ module YAML self.node_text( [value].pack("m"), '|' ) end - # - # Emit plain, normal flowing text - # - def node_text( value, block = '>' ) + # + # Emit plain, normal flowing text + # + def node_text( value, block = '>' ) @seq_map = false - valx = value.dup + valx = value.dup unless block - block = - if options(:UseBlock) - '|' - elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/ - '|' - else - '>' - end - block += - if valx =~ /\n\Z\n/ - "+" - elsif valx =~ /\Z\n/ - "" - else - "-" - end + block = + if options(:UseBlock) + '|' + elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/ + '|' + else + '>' + end if valx =~ /\A[ \t#]/ block += options(:Indent).to_s end + block += + if valx =~ /\n\Z\n/ + "+" + elsif valx =~ /\Z\n/ + "" + else + "-" + end + end + if valx =~ /#{YAML::ESCAPE_CHAR}/ + valx = YAML::escape( valx ) + end + if block[0] == ?> + valx = fold( valx ) end - if valx =~ /#{YAML::ESCAPE_CHAR}/ - valx = YAML::escape( valx ) - end - if block[0] == ?> - valx = fold( valx ) - end indt = nil indt = $&.to_i if block =~ /\d+/ - self << block + indent_text( valx, indt ) + "\n" - end + self << block + indent_text( valx, indt ) + "\n" + end - # - # Emit a simple, unqouted string - # - def simple( value ) + # + # Emit a simple, unqouted string + # + def simple( value ) @seq_map = false self << value.to_s - end - - # - # Emit double-quoted string - # - def double( value ) - "\"#{YAML.escape( value )}\"" - end - - # - # Emit single-quoted string - # - def single( value ) - "'#{value}'" - end - - # - # Write a text block with the current indent - # - def indent_text( text, indt = nil ) - return "" if text.to_s.empty? - spacing = " " * ( level * ( indt || options(:Indent) ) ) - return "\n" + text.gsub( /^([^\n])/, "#{spacing}\\1" ) - end - - # - # Write a current indent - # - def indent + end + + # + # Emit double-quoted string + # + def double( value ) + "\"#{YAML.escape( value )}\"" + end + + # + # Emit single-quoted string + # + def single( value ) + "'#{value}'" + end + + # + # Write a text block with the current indent + # + def indent_text( text, indt = nil ) + return "" if text.to_s.empty? + indt ||= 0 + spacing = indent( indt ) + return "\n" + text.gsub( /^([^\n])/, "#{spacing}\\1" ) + end + + # + # Write a current indent + # + def indent( mod = nil ) #p [ self.id, @level, :INDENT ] - return " " * ( level * options(:Indent) ) - end - - # - # Add indent to the buffer - # - def indent! - self << indent - end - - # - # Folding paragraphs within a column - # - def fold( value ) - value.gsub!( /\A\n+/, '' ) - folded = $&.to_s - width = (0..options(:BestWidth)) - while not value.empty? - last = value.index( /(\n+)/ ) - chop_s = false - if width.include?( last ) - last += $1.length - 1 - elsif width.include?( value.length ) - last = value.length - else - last = value.rindex( /[ \t]/, options(:BestWidth) ) - chop_s = true - end - folded += value.slice!( 0, width.include?( last ) ? last + 1 : options(:BestWidth) ) - folded.chop! if chop_s - folded += "\n" unless value.empty? - end - folded - end + if level.zero? + mod ||= 0 + else + mod ||= options(:Indent) + mod += ( level - 1 ) * options(:Indent) + end + return " " * mod + end + + # + # Add indent to the buffer + # + def indent! + self << indent + end + + # + # Folding paragraphs within a column + # + def fold( value ) + value.gsub!( /\A\n+/, '' ) + folded = $&.to_s + width = (0..options(:BestWidth)) + while not value.empty? + last = value.index( /(\n+)/ ) + chop_s = false + if width.include?( last ) + last += $1.length - 1 + elsif width.include?( value.length ) + last = value.length + else + last = value.rindex( /[ \t]/, options(:BestWidth) ) + chop_s = true + end + folded += value.slice!( 0, width.include?( last ) ? last + 1 : options(:BestWidth) ) + folded.chop! if chop_s + folded += "\n" unless value.empty? + end + folded + end # # Quick mapping @@ -145,18 +152,18 @@ module YAML def map( type, &e ) val = Mapping.new e.call( val ) - self << "#{type} " if type.length.nonzero? + self << "#{type} " if type.length.nonzero? - # - # Empty hashes - # - if val.length.zero? - self << "{}" + # + # Empty hashes + # + if val.length.zero? + self << "{}" @seq_map = false - else + else # FIXME # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? - # @headless = 1 + # @headless = 1 # end defkey = @options.delete( :DefaultKey ) @@ -166,9 +173,9 @@ module YAML defkey.to_yaml( :Emitter => self ) end - # - # Emit the key and value - # + # + # Emit the key and value + # val.each { |v| seq_map_shortcut if v[0].is_complex_yaml? @@ -182,7 +189,7 @@ module YAML self << ": " v[1].to_yaml( :Emitter => self ) } - end + end end def seq_map_shortcut @@ -203,22 +210,22 @@ module YAML @seq_map = false val = Sequence.new e.call( val ) - self << "#{type} " if type.length.nonzero? - - # - # Empty arrays - # - if val.length.zero? - self << "[]" - else + self << "#{type} " if type.length.nonzero? + + # + # Empty arrays + # + if val.length.zero? + self << "[]" + else # FIXME # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? - # @headless = 1 + # @headless = 1 # end - # - # Emit the key and value - # + # + # Emit the key and value + # val.each { |v| self << "\n" indent! @@ -226,7 +233,7 @@ module YAML @seq_map = true if v.class == Hash v.to_yaml( :Emitter => self ) } - end + end end end diff --git a/lib/yaml/rubytypes.rb b/lib/yaml/rubytypes.rb index adebf9439e..1f7f49633c 100644 --- a/lib/yaml/rubytypes.rb +++ b/lib/yaml/rubytypes.rb @@ -290,7 +290,7 @@ class String ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) end def to_yaml_type - "!ruby/string#{ if self.class != ::String; ":#{ self.class }"; end }" + "!ruby/string#{ ":#{ self.class }" if self.class != ::String }" end def to_yaml_fold nil @@ -315,8 +315,8 @@ class String } elsif self.is_binary_data? out.binary_base64( self ) - elsif self =~ /^ |#{YAML::ESCAPE_CHAR}| $/ - complex = false + # elsif self =~ /^ |#{YAML::ESCAPE_CHAR}| $/ + # complex = false else out.node_text( self, to_yaml_fold ) end @@ -326,7 +326,7 @@ class String self elsif empty? "''" - elsif self =~ /^[^#{YAML::WORD_CHAR}]| \#|#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |$)| $|\n|\'/ + elsif self =~ /^[^#{YAML::WORD_CHAR}\/]| \#|#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |$)| $|\n|\'/ "\"#{YAML.escape( self )}\"" elsif YAML.detect_implicit( self ) != 'str' "\"#{YAML.escape( self )}\"" -- cgit v1.2.3