# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: /w/fey/uttk/trunk/lib/uttk/dumpers/Yaml.rb 24396 2006-07-10T08:23:37.784417Z ertai $ class Symbol alias_method :old_to_yaml, :to_yaml def to_yaml ( opts={} ) if YAML.have_option? opts, :uttk self.to_s.to_yaml(opts) else old_to_yaml(opts) end end end class OHash def to_yaml ( opts={} ) if YAML.have_option? opts, :uttk 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 Uttk module Dumpers class Yaml < Dumper include Concrete MAP_INDENT = 2 MAP_SHIFT = ' ' * MAP_INDENT OMAP_SHIFT = ' ' * 4 def initialize ( *a, &b ) super @indent = '' @stack_indent = [] @endl = true @start = false @opts = {:uttk => true, :Inline => true} end def new_node ( path, node ) super puts_header options = nil if ! path.nil? and ! path.empty? options = path.last.options end puts unless @endl @stack_indent << @indent self << @indent if options.nil? or not options[:ordered] @indent += MAP_SHIFT else self << '- ' @indent += OMAP_SHIFT end self << clean_to_yaml(node.segment) << ':' if type = node.options[:type] if type.is_a? Class # FIXME handle this better if HAVE_YAML_TAGURI self << ' !' << type.taguri.sub(/^tag:.*?:/, '') else self << ' ' << type.to_yaml_type end else self << ' !' << type.to_s end end @endl = false end protected :new_node def new_leaf ( path, leaf ) super puts_header val = leaf return if val.respond_to?(:hidden) if val.is_a?(String) and val =~ /[\n"]/ if val =~ /\A[ \t]/ val = "|#{MAP_INDENT}\n#{val}" else val = "|\n#{val}" end val.chomp! else val = clean_to_yaml(val) val = "\n" + val if val =~ /\n/ and val !~ /^\n/ end val.gsub!(/[ \t]*\n/, "\n#@indent") 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.chomp! val.gsub!(/(^\s*)- \n\s+([^\s-])/, '\1- \2') val = '""' if val == "''" val end private :clean_to_yaml def puts_header unless @start puts '---' @start = true end end def update ( *a, &b ) super flush end end # class Yaml end # module Dumpers end # module Uttk