# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved. # License:: LGPL # $Id: Yaml.rb 571 2005-04-13 14:41:22Z polrop $ class Symbol # :nodoc: alias_method :old_to_yaml, :to_yaml def to_yaml ( opts={} ) # FIXME # if opts[:ttk] self.to_s.to_yaml(opts) # else # old_to_yaml(opts) # end end end class Class # :nodoc: alias_method :old_to_yaml, :to_yaml def to_yaml ( opts={} ) self.to_s.sub(/^TTK::Strategies::/, '').to_yaml(opts) end end class Regexp # :nodoc: alias_method :old_to_yaml_type, :to_yaml_type alias_method :old_to_yaml, :to_yaml def to_yaml_type '!re' end def to_yaml ( opts={} ) "#{to_yaml_type} #{source}" end end YAML.add_builtin_type('!re') do |type, val| Regexp.new(val.to_s) end class Pathname # :nodoc: alias_method :old_to_yaml, :to_yaml def to_yaml ( opts={} ) if opts[:ttk] self.to_s.to_yaml(opts) else old_to_yaml(opts) end end end YAML.add_builtin_type('!path') do |type, val| Pathname.new(val.to_s) end class OHash # :nodoc: def to_yaml ( opts={} ) if opts[:ttk] YAML::quick_emit(self.object_id, opts) do |out| out.seq('') do |seq| self.each do |k,v| seq.add( { k => v } ) end end end else super end end end module TTK module Dumpers class Yaml < Dumper include Concrete MAP_SHIFT_WIDTH = 2 SEQ_SHIFT_WIDTH = 4 def initialize ( *a, &b ) super @indent = '' @stack_indent = [] @endl = true @flushable = @io.respond_to?(:flush) @io.puts '---' @opts = {:ttk => true} end def new_node ( path ) super type = (path.nil? or path.size < 2)? :map : path[-2].last node = path.last.first @io.puts unless @endl @stack_indent << @indent.size case type when :map val = "#{clean_to_yaml(node)}:" @io << @indent << val @indent += ' ' * MAP_SHIFT_WIDTH when :seq val = "- #{clean_to_yaml(node)}:" @io << @indent << val @indent += ' ' * SEQ_SHIFT_WIDTH end @endl = false end protected :new_node def new_leaf ( path, node ) super val = node if val.is_a?(String) and val =~ /\n/ val = "|\n#{val}" val.chomp! else return if val.respond_to?(:hidden) val = clean_to_yaml(val) end val.gsub!(/\s*\n/, "\n#@indent") @io.print ' ', val, "\n" @endl = true end protected :new_leaf def up ( path ) super @indent = ' ' * @stack_indent.pop end protected :up def clean_to_yaml ( val ) val = val.to_yaml(@opts) val.sub!(/^--- /, '') val.gsub!(/(^\s*)- \n\s+([^\s-])/, '\1- \2') val = '""' if val == "''" val end private :clean_to_yaml def update ( *a, &b ) super @io.flush if @flushable and (not @io.closed?) end end # class Yaml end # module Dumpers end # module TTK