diff options
author | John Barnette <jbarnette@gmail.com> | 2009-09-29 10:48:00 -0700 |
---|---|---|
committer | John Barnette <jbarnette@gmail.com> | 2009-09-29 10:48:00 -0700 |
commit | 28fcaf328982d06ff006d2b6b13ef75d125cab23 (patch) | |
tree | 020a6180df7556372ef1682b04b472577e29222d /lib | |
parent | 6d840a576b6cc627a34cd5c46a07a6aee8d54c1c (diff) | |
parent | d2bf942ac38b1b6e91116a06855a00d32b95d27b (diff) | |
download | psych-28fcaf328982d06ff006d2b6b13ef75d125cab23.zip |
Merge branch 'master' of git@github.com:tenderlove/psych
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych.rb | 8 | ||||
-rw-r--r-- | lib/psych/emitter.rb | 4 | ||||
-rw-r--r-- | lib/psych/nodes/document.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/node.rb | 8 | ||||
-rw-r--r-- | lib/psych/nodes/scalar.rb | 2 | ||||
-rw-r--r-- | lib/psych/tree_builder.rb | 5 | ||||
-rw-r--r-- | lib/psych/visitors.rb | 1 | ||||
-rw-r--r-- | lib/psych/visitors/emitter.rb | 31 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 14 |
9 files changed, 66 insertions, 9 deletions
diff --git a/lib/psych.rb b/lib/psych.rb index bdc2458..bbb8662 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -19,7 +19,11 @@ module Psych VERSION = '1.0.0' LIBYAML_VERSION = Psych.libyaml_version.join '.' - def self.parse thing - Psych::Parser.new.parse thing + ### + # Load +yaml+ in to a Ruby data structure + def self.load yaml + parser = Psych::Parser.new(TreeBuilder.new) + parser.parse yaml + parser.handler.root.children.first.to_ruby end end diff --git a/lib/psych/emitter.rb b/lib/psych/emitter.rb new file mode 100644 index 0000000..b06ad3a --- /dev/null +++ b/lib/psych/emitter.rb @@ -0,0 +1,4 @@ +module Psych + class Emitter < Psych::Handler + end +end diff --git a/lib/psych/nodes/document.rb b/lib/psych/nodes/document.rb index 0bd58b2..be786c6 100644 --- a/lib/psych/nodes/document.rb +++ b/lib/psych/nodes/document.rb @@ -10,7 +10,7 @@ module Psych # Was this document implicitly created? attr_accessor :implicit - def initialize version = [], tag_directives = [], implicit = true + def initialize version = [], tag_directives = [], implicit = false super() @version = version @tag_directives = tag_directives diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index b5ae131..a63fc60 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -1,3 +1,5 @@ +require 'stringio' + module Psych module Nodes ### @@ -15,6 +17,12 @@ module Psych def to_ruby Visitors::ToRuby.new.accept self end + + def to_yaml + io = StringIO.new + Visitors::Emitter.new(io).accept self + io.string + end end end end diff --git a/lib/psych/nodes/scalar.rb b/lib/psych/nodes/scalar.rb index b84e0ed..fac4d69 100644 --- a/lib/psych/nodes/scalar.rb +++ b/lib/psych/nodes/scalar.rb @@ -29,7 +29,7 @@ module Psych # The style of this scalar attr_accessor :style - def initialize value, anchor = nil, tag = nil, plain = true, quoted = true, style = ANY + def initialize value, anchor = nil, tag = nil, plain = true, quoted = false, style = ANY super() @value = value @anchor = anchor diff --git a/lib/psych/tree_builder.rb b/lib/psych/tree_builder.rb index c158368..efbeaf9 100644 --- a/lib/psych/tree_builder.rb +++ b/lib/psych/tree_builder.rb @@ -23,31 +23,26 @@ module Psych }.each do |node| class_eval %{ def start_#{node.downcase}(*args) - super n = Nodes::#{node}.new(*args) @stack.last.children << n @stack.push n end def end_#{node.downcase}(*args) - super @stack.pop end } end def start_stream encoding - super @stack.push Nodes::Stream.new(encoding) end def scalar(*args) - super @stack.last.children << Nodes::Scalar.new(*args) end def alias(*args) - super @stack.last.children << Nodes::Alias.new(*args) end end diff --git a/lib/psych/visitors.rb b/lib/psych/visitors.rb index 45b4b58..df58e2e 100644 --- a/lib/psych/visitors.rb +++ b/lib/psych/visitors.rb @@ -1,2 +1,3 @@ require 'psych/visitors/visitor' require 'psych/visitors/to_ruby' +require 'psych/visitors/emitter' diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb new file mode 100644 index 0000000..a0ff9d9 --- /dev/null +++ b/lib/psych/visitors/emitter.rb @@ -0,0 +1,31 @@ +module Psych + module Visitors + class Emitter < Psych::Visitors::Visitor + def initialize io + @handler = Psych::Emitter.new io + end + + visitor_for(Nodes::Stream) do |o| + @handler.start_stream o.encoding + o.children.each { |c| c.accept self } + @handler.end_stream + end + + visitor_for(Nodes::Document) do |o| + @handler.start_document o.version, o.tag_directives, o.implicit + o.children.each { |c| c.accept self } + @handler.end_document o.implicit + end + + visitor_for(Nodes::Scalar) do |o| + @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style + end + + visitor_for(Nodes::Sequence) do |o| + @handler.start_sequence o.anchor, o.tag, o.implicit, o.style + o.children.each { |c| c.accept self } + @handler.end_sequence + end + end + end +end diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index a4e6152..8848c16 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -3,7 +3,13 @@ module Psych ### # This class walks a YAML AST, converting each node to ruby class ToRuby < Psych::Visitors::Visitor + def initialize + super + @st = {} + end + visitor_for(Nodes::Scalar) do |o| + @st[o.anchor] = o.value if o.anchor o.value end @@ -18,6 +24,14 @@ module Psych visitor_for(Nodes::Document) do |o| o.root.accept self end + + visitor_for(Nodes::Stream) do |o| + o.children.map { |c| c.accept self } + end + + visitor_for(Nodes::Alias) do |o| + @st[o.anchor] + end end end end |