5 require 'yaml/constants'
6 require 'yaml/encoding'
13 def options( opt = nil )
15 @options[opt] || YAML::DEFAULTS[opt]
28 def binary_base64( value )
30 self.node_text( [value].pack("m"), '|' )
34 # Emit plain, normal flowing text
36 def node_text( value, block = nil )
43 elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
49 indt = $&.to_i if block =~ /\d+/
50 if valx =~ /(\A\n*[ \t#]|^---\s+)/
51 indt = options(:Indent) unless indt.to_i > 0
66 esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
67 valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp
68 self << '"' + indent_text( valx, indt, false )
74 self << block + indent_text( valx, indt )
79 # Emit a simple, unqouted string
87 # Emit double-quoted string
90 "\"#{YAML.escape( value )}\""
94 # Emit single-quoted string
101 # Write a text block with the current indent
103 def indent_text( text, mod, first_line = true )
104 return "" if text.to_s.empty?
105 spacing = indent( mod )
106 text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
107 return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
111 # Write a current indent
113 def indent( mod = nil )
114 #p [ self.id, level, mod, :INDENT ]
118 mod ||= options(:Indent)
119 mod += ( level - 1 ) * options(:Indent)
125 # Add indent to the buffer
132 # Folding paragraphs within a column
135 value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do
136 $1 || $2 + ( $3 || "\n" )
146 self << "#{type} " if type.length.nonzero?
156 # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
160 defkey = @options.delete( :DefaultKey )
164 defkey.to_yaml( :Emitter => self )
168 # Emit the key and value
172 if v[0].is_complex_yaml?
175 v[0].to_yaml( :Emitter => self )
176 if v[0].is_complex_yaml?
181 v[1].to_yaml( :Emitter => self )
187 # FIXME: seq_map needs to work with the new anchoring system
189 # @anchor_extras[@buffer.length - 1] = "\n" + indent
204 self << "#{type} " if type.length.nonzero?
213 # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
218 # Emit the key and value
224 @seq_map = true if v.class == Hash
225 v.to_yaml( :Emitter => self )
233 # Emitter helper classes
235 class Mapping < Array
241 class Sequence < Array