2003-05-09 17:25:50 -04:00
|
|
|
#
|
|
|
|
# Output classes and methods
|
|
|
|
#
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
require 'yaml/baseemitter'
|
2003-05-09 17:25:50 -04:00
|
|
|
require 'yaml/encoding'
|
|
|
|
|
|
|
|
module YAML
|
|
|
|
|
|
|
|
#
|
|
|
|
# Emit a set of values
|
|
|
|
#
|
|
|
|
|
|
|
|
class Emitter
|
2003-07-11 18:52:14 -04:00
|
|
|
|
|
|
|
include BaseEmitter
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
attr_accessor :options
|
2003-07-11 18:52:14 -04:00
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
def initialize( opts )
|
|
|
|
opts = {} if opts.class != Hash
|
|
|
|
@options = YAML::DEFAULTS.dup.update( opts )
|
|
|
|
@headless = 0
|
|
|
|
@seq_map = false
|
|
|
|
@anchors = {}
|
|
|
|
@anchor_extras = {}
|
|
|
|
@active_anchors = []
|
|
|
|
@level = -1
|
|
|
|
self.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
@buffer = []
|
|
|
|
end
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
def level
|
|
|
|
@level
|
|
|
|
end
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
#
|
|
|
|
# Version string
|
|
|
|
#
|
|
|
|
def version_s
|
|
|
|
" %YAML:#{@options[:Version]}" if @options[:UseVersion]
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Header
|
|
|
|
#
|
|
|
|
def header
|
|
|
|
if @headless.nonzero?
|
|
|
|
""
|
|
|
|
else
|
|
|
|
"---#{version_s} "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Concatenate to the buffer
|
|
|
|
#
|
|
|
|
def <<( str )
|
|
|
|
#p [ self.id, @level, str ]
|
|
|
|
@buffer.last << str
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Monitor objects and allow references
|
|
|
|
#
|
|
|
|
def start_object( oid )
|
|
|
|
@level += 1
|
|
|
|
@buffer.push( "" )
|
|
|
|
#p [ self.id, @level, :OPEN ]
|
|
|
|
idx = nil
|
|
|
|
if oid
|
|
|
|
if @anchors.has_key?( oid )
|
|
|
|
idx = @active_anchors.index( oid )
|
|
|
|
unless idx
|
|
|
|
idx = @active_anchors.length
|
|
|
|
af_str = "&#{@options[:AnchorFormat]} " % [ idx + 1 ]
|
|
|
|
af_str += @anchor_extras[ @anchors[ oid ] ].to_s
|
|
|
|
@buffer[ @anchors[ oid ] ][0,0] = af_str
|
|
|
|
@headless = 0 if @anchors[ oid ].zero?
|
|
|
|
end
|
|
|
|
idx += 1
|
|
|
|
@active_anchors.push( oid )
|
|
|
|
else
|
|
|
|
@anchors[ oid ] = @buffer.length - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return idx
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Output method
|
|
|
|
#
|
|
|
|
def end_object
|
|
|
|
@level -= 1
|
|
|
|
@buffer.push( "" )
|
|
|
|
#p [ self.id, @level, :END ]
|
|
|
|
if @level < 0
|
2003-06-17 21:06:00 -04:00
|
|
|
header + @buffer.to_s[@headless..-1].to_s
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|