2003-07-11 19:56:42 -04:00
|
|
|
#
|
|
|
|
# BaseEmitter
|
|
|
|
#
|
|
|
|
|
|
|
|
require 'yaml/constants'
|
|
|
|
require 'yaml/encoding'
|
|
|
|
require 'yaml/error'
|
|
|
|
|
|
|
|
module YAML
|
|
|
|
|
|
|
|
module BaseEmitter
|
|
|
|
|
|
|
|
def options( opt = nil )
|
|
|
|
if opt
|
|
|
|
@options[opt] || YAML::DEFAULTS[opt]
|
|
|
|
else
|
|
|
|
@options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def options=( opt )
|
|
|
|
@options = opt
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Emit binary data
|
|
|
|
#
|
|
|
|
def binary_base64( value )
|
|
|
|
self << "!binary "
|
|
|
|
self.node_text( [value].pack("m"), '|' )
|
|
|
|
end
|
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Emit plain, normal flowing text
|
|
|
|
#
|
|
|
|
def node_text( value, block = nil )
|
2003-07-11 19:56:42 -04:00
|
|
|
@seq_map = false
|
2004-05-16 11:29:30 -04:00
|
|
|
valx = value.dup
|
2004-05-06 02:29:56 -04:00
|
|
|
unless block
|
2004-05-16 11:29:30 -04:00
|
|
|
block =
|
|
|
|
if options(:UseBlock)
|
|
|
|
'|'
|
|
|
|
elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
|
|
|
|
'|'
|
|
|
|
else
|
|
|
|
'>'
|
|
|
|
end
|
2004-05-25 10:57:25 -04:00
|
|
|
|
|
|
|
indt = $&.to_i if block =~ /\d+/
|
2004-08-18 16:42:09 -04:00
|
|
|
if valx =~ /(\A\n*[ \t#]|^---\s+)/
|
2004-05-25 10:57:25 -04:00
|
|
|
indt = options(:Indent) unless indt.to_i > 0
|
|
|
|
block += indt.to_s
|
2004-05-06 02:29:56 -04:00
|
|
|
end
|
2004-05-25 10:57:25 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
block +=
|
|
|
|
if valx =~ /\n\Z\n/
|
|
|
|
"+"
|
|
|
|
elsif valx =~ /\Z\n/
|
|
|
|
""
|
|
|
|
else
|
|
|
|
"-"
|
|
|
|
end
|
2004-05-06 02:29:56 -04:00
|
|
|
end
|
2004-08-18 16:42:09 -04:00
|
|
|
block += "\n"
|
|
|
|
if block[0] == ?"
|
|
|
|
esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
|
|
|
|
valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp
|
|
|
|
self << '"' + indent_text( valx, indt, false )
|
|
|
|
else
|
|
|
|
if block[0] == ?>
|
|
|
|
valx = fold( valx )
|
|
|
|
end
|
|
|
|
#p [block, indt]
|
|
|
|
self << block + indent_text( valx, indt )
|
|
|
|
end
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2003-07-11 19:56:42 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Emit a simple, unqouted string
|
|
|
|
#
|
|
|
|
def simple( value )
|
2003-07-11 19:56:42 -04:00
|
|
|
@seq_map = false
|
|
|
|
self << value.to_s
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Emit double-quoted string
|
|
|
|
#
|
|
|
|
def double( value )
|
|
|
|
"\"#{YAML.escape( value )}\""
|
|
|
|
end
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Emit single-quoted string
|
|
|
|
#
|
|
|
|
def single( value )
|
|
|
|
"'#{value}'"
|
|
|
|
end
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Write a text block with the current indent
|
|
|
|
#
|
2004-08-18 16:42:09 -04:00
|
|
|
def indent_text( text, mod, first_line = true )
|
2004-05-16 11:29:30 -04:00
|
|
|
return "" if text.to_s.empty?
|
|
|
|
spacing = indent( mod )
|
2004-08-18 16:42:09 -04:00
|
|
|
text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
|
|
|
|
return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Write a current indent
|
|
|
|
#
|
2004-05-14 23:11:28 -04:00
|
|
|
def indent( mod = nil )
|
2004-05-16 11:29:30 -04:00
|
|
|
#p [ self.id, level, mod, :INDENT ]
|
|
|
|
if level <= 0
|
2004-05-14 23:11:28 -04:00
|
|
|
mod ||= 0
|
|
|
|
else
|
|
|
|
mod ||= options(:Indent)
|
|
|
|
mod += ( level - 1 ) * options(:Indent)
|
|
|
|
end
|
|
|
|
return " " * mod
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Add indent to the buffer
|
|
|
|
#
|
|
|
|
def indent!
|
|
|
|
self << indent
|
|
|
|
end
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Folding paragraphs within a column
|
|
|
|
#
|
|
|
|
def fold( value )
|
2004-08-18 16:42:09 -04:00
|
|
|
value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do |s|
|
2004-05-25 10:57:25 -04:00
|
|
|
$1 || $2 + ( $3 || "\n" )
|
|
|
|
end
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2003-07-11 19:56:42 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Quick mapping
|
|
|
|
#
|
|
|
|
def map( type, &e )
|
|
|
|
val = Mapping.new
|
|
|
|
e.call( val )
|
2004-05-16 11:29:30 -04:00
|
|
|
self << "#{type} " if type.length.nonzero?
|
2003-07-11 19:56:42 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Empty hashes
|
|
|
|
#
|
|
|
|
if val.length.zero?
|
|
|
|
self << "{}"
|
2003-07-11 19:56:42 -04:00
|
|
|
@seq_map = false
|
2004-05-16 11:29:30 -04:00
|
|
|
else
|
2003-07-11 19:56:42 -04:00
|
|
|
# FIXME
|
|
|
|
# if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
|
2004-05-16 11:29:30 -04:00
|
|
|
# @headless = 1
|
2003-07-11 19:56:42 -04:00
|
|
|
# end
|
|
|
|
|
|
|
|
defkey = @options.delete( :DefaultKey )
|
|
|
|
if defkey
|
|
|
|
seq_map_shortcut
|
|
|
|
self << "= : "
|
|
|
|
defkey.to_yaml( :Emitter => self )
|
|
|
|
end
|
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Emit the key and value
|
|
|
|
#
|
2003-07-11 19:56:42 -04:00
|
|
|
val.each { |v|
|
|
|
|
seq_map_shortcut
|
|
|
|
if v[0].is_complex_yaml?
|
|
|
|
self << "? "
|
|
|
|
end
|
|
|
|
v[0].to_yaml( :Emitter => self )
|
|
|
|
if v[0].is_complex_yaml?
|
|
|
|
self << "\n"
|
|
|
|
indent!
|
|
|
|
end
|
|
|
|
self << ": "
|
|
|
|
v[1].to_yaml( :Emitter => self )
|
|
|
|
}
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2003-07-11 19:56:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def seq_map_shortcut
|
|
|
|
# FIXME: seq_map needs to work with the new anchoring system
|
|
|
|
# if @seq_map
|
|
|
|
# @anchor_extras[@buffer.length - 1] = "\n" + indent
|
|
|
|
# @seq_map = false
|
|
|
|
# else
|
|
|
|
self << "\n"
|
|
|
|
indent!
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Quick sequence
|
|
|
|
#
|
|
|
|
def seq( type, &e )
|
|
|
|
@seq_map = false
|
|
|
|
val = Sequence.new
|
|
|
|
e.call( val )
|
2004-05-16 11:29:30 -04:00
|
|
|
self << "#{type} " if type.length.nonzero?
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Empty arrays
|
|
|
|
#
|
|
|
|
if val.length.zero?
|
|
|
|
self << "[]"
|
|
|
|
else
|
2003-07-11 19:56:42 -04:00
|
|
|
# FIXME
|
|
|
|
# if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
|
2004-05-16 11:29:30 -04:00
|
|
|
# @headless = 1
|
2003-07-11 19:56:42 -04:00
|
|
|
# end
|
|
|
|
|
2004-05-16 11:29:30 -04:00
|
|
|
#
|
|
|
|
# Emit the key and value
|
|
|
|
#
|
2003-07-11 19:56:42 -04:00
|
|
|
val.each { |v|
|
|
|
|
self << "\n"
|
|
|
|
indent!
|
|
|
|
self << "- "
|
|
|
|
@seq_map = true if v.class == Hash
|
|
|
|
v.to_yaml( :Emitter => self )
|
|
|
|
}
|
2004-05-16 11:29:30 -04:00
|
|
|
end
|
2003-07-11 19:56:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Emitter helper classes
|
|
|
|
#
|
|
|
|
class Mapping < Array
|
|
|
|
def add( k, v )
|
|
|
|
push [k, v]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Sequence < Array
|
|
|
|
def add( v )
|
|
|
|
push v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|