diff options
-rw-r--r-- | ext/psych/emitter.c | 45 | ||||
-rw-r--r-- | lib/psych/nodes/document.rb | 2 | ||||
-rw-r--r-- | lib/psych/tree_builder.rb | 5 | ||||
-rw-r--r-- | lib/psych/visitors/emitter.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_emitter.rb | 13 |
5 files changed, 55 insertions, 14 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index 1199a31..6083052 100644 --- a/ext/psych/emitter.c +++ b/ext/psych/emitter.c @@ -5,7 +5,7 @@ VALUE cPsychEmitter; static int writer(void *ctx, unsigned char *buffer, size_t size) { VALUE io = (VALUE)ctx; - VALUE str = rb_str_new(buffer, size); + VALUE str = rb_str_new((const char *)buffer, (long)size); VALUE wrote = rb_funcall(io, rb_intern("write"), 1, str); return (int)NUM2INT(wrote); } @@ -20,7 +20,7 @@ static VALUE allocate(VALUE klass) { yaml_emitter_t * emitter = malloc(sizeof(yaml_emitter_t)); yaml_emitter_initialize(emitter); - Data_Wrap_Struct(cPsychEmitter, 0, dealloc, emitter); + return Data_Wrap_Struct(cPsychEmitter, 0, dealloc, emitter); } static VALUE initialize(VALUE self, VALUE io) @@ -39,7 +39,7 @@ static VALUE start_stream(VALUE self, VALUE encoding) Data_Get_Struct(self, yaml_emitter_t, emitter); yaml_event_t event; - yaml_stream_start_event_initialize(&event, NUM2INT(encoding)); + yaml_stream_start_event_initialize(&event, (yaml_encoding_t)NUM2INT(encoding)); yaml_emitter_emit(emitter, &event); return self; } @@ -51,6 +51,7 @@ static VALUE end_stream(VALUE self) yaml_event_t event; yaml_stream_end_event_initialize(&event); + yaml_emitter_emit(emitter, &event); return self; } @@ -63,8 +64,8 @@ static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp) yaml_version_directive_t version_directive; if(RARRAY_LEN(version) > 0) { - VALUE major = rb_ary_entry(version, 0); - VALUE minor = rb_ary_entry(version, 1); + VALUE major = rb_ary_entry(version, (long)0); + VALUE minor = rb_ary_entry(version, (long)1); version_directive.major = NUM2INT(major); version_directive.minor = NUM2INT(minor); @@ -73,7 +74,7 @@ static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp) yaml_event_t event; yaml_document_start_event_initialize( &event, - &version_directive, + (RARRAY_LEN(version) > 0) ? &version_directive : NULL, NULL, NULL, imp == Qtrue ? 1 : 0 @@ -89,8 +90,6 @@ static VALUE end_document(VALUE self, VALUE imp) yaml_emitter_t * emitter; Data_Get_Struct(self, yaml_emitter_t, emitter); - yaml_version_directive_t version_directive; - yaml_event_t event; yaml_document_end_event_initialize(&event, imp == Qtrue ? 1 : 0); @@ -99,6 +98,35 @@ static VALUE end_document(VALUE self, VALUE imp) return self; } +static VALUE scalar( + VALUE self, + VALUE value, + VALUE anchor, + VALUE tag, + VALUE plain, + VALUE quoted, + VALUE style +) { + yaml_emitter_t * emitter; + Data_Get_Struct(self, yaml_emitter_t, emitter); + + yaml_event_t event; + yaml_scalar_event_initialize( + &event, + (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(anchor)), + (yaml_char_t *)(Qnil == tag ? NULL : StringValuePtr(tag)), + (yaml_char_t*)StringValuePtr(value), + (int)RSTRING_LEN(value), + Qtrue == plain ? 1 : 0, + Qtrue == quoted ? 1 : 0, + (yaml_scalar_style_t)NUM2INT(style) + ); + + yaml_emitter_emit(emitter, &event); + + return self; +} + void Init_psych_emitter() { VALUE psych = rb_define_module("Psych"); @@ -112,4 +140,5 @@ void Init_psych_emitter() rb_define_method(cPsychEmitter, "end_stream", end_stream, 0); rb_define_method(cPsychEmitter, "start_document", start_document, 3); rb_define_method(cPsychEmitter, "end_document", end_document, 1); + rb_define_method(cPsychEmitter, "scalar", scalar, 6); } 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/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/emitter.rb b/lib/psych/visitors/emitter.rb index d71ce2a..e2dd0be 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -16,6 +16,10 @@ module Psych 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 end end end diff --git a/test/visitors/test_emitter.rb b/test/visitors/test_emitter.rb index a6b8f25..3c93cc7 100644 --- a/test/visitors/test_emitter.rb +++ b/test/visitors/test_emitter.rb @@ -21,6 +21,19 @@ module Psych @visitor.accept s assert_equal '', @io.string end + + def test_scalar + s = Nodes::Stream.new + doc = Nodes::Document.new + scalar = Nodes::Scalar.new 'hello' + + doc.children << scalar + s.children << doc + + @visitor.accept s + + assert_match(/hello/, @io.string) + end end end end |