Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more

In Files

  • psych/emitter.c

Psych::Emitter

Public Class Methods

Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS) click to toggle source

Create a new Psych::Emitter that writes to io.

 
               static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
    yaml_emitter_t * emitter;
    VALUE io, options;
    VALUE line_width;
    VALUE indent;
    VALUE canonical;

    Data_Get_Struct(self, yaml_emitter_t, emitter);

    if (rb_scan_args(argc, argv, "11", &io, &options) == 2) {
        line_width = rb_funcall(options, id_line_width, 0);
        indent     = rb_funcall(options, id_indentation, 0);
        canonical  = rb_funcall(options, id_canonical, 0);

        yaml_emitter_set_width(emitter, NUM2INT(line_width));
        yaml_emitter_set_indent(emitter, NUM2INT(indent));
        yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0);
    }

    yaml_emitter_set_output(emitter, writer, (void *)io);

    return self;
}
            

Public Instance Methods

alias(anchor) click to toggle source

Emit an alias with anchor.

See Psych::Handler#alias

 
               static VALUE alias(VALUE self, VALUE anchor)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

#ifdef HAVE_RUBY_ENCODING_H
    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, rb_utf8_encoding());
    }
#endif

    yaml_alias_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor))
            );

    emit(emitter, &event);

    return self;
}
            
canonical click to toggle source

Get the output style, canonical or not.

 
               static VALUE canonical(VALUE self)
{
    yaml_emitter_t * emitter;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    return (emitter->canonical == 0) ? Qfalse : Qtrue;
}
            
canonical = true click to toggle source

Set the output style to canonical, or not.

 
               static VALUE set_canonical(VALUE self, VALUE style)
{
    yaml_emitter_t * emitter;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_emitter_set_canonical(emitter, Qtrue == style ? 1 : 0);

    return style;
}
            
end_document(implicit) click to toggle source

End a document emission with an implicit ending.

See Psych::Handler#end_document

 
               static VALUE end_document(VALUE self, VALUE imp)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_document_end_event_initialize(&event, imp ? 1 : 0);

    emit(emitter, &event);

    return self;
}
            
end_mapping click to toggle source

Emit the end of a mapping.

See Psych::Handler#end_mapping

 
               static VALUE end_mapping(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_mapping_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}
            
end_sequence click to toggle source

End sequence emission.

See Psych::Handler#end_sequence

 
               static VALUE end_sequence(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_sequence_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}
            
end_stream click to toggle source

End a stream emission

See Psych::Handler#end_stream

 
               static VALUE end_stream(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_stream_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}
            
indentation click to toggle source

Get the indentation level.

 
               static VALUE indentation(VALUE self)
{
    yaml_emitter_t * emitter;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    return INT2NUM(emitter->best_indent);
}
            
indentation = level click to toggle source

Set the indentation level to level. The level must be less than 10 and greater than 1.

 
               static VALUE set_indentation(VALUE self, VALUE level)
{
    yaml_emitter_t * emitter;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_emitter_set_indent(emitter, NUM2INT(level));

    return level;
}
            
line_width click to toggle source

Get the preferred line width.

 
               static VALUE line_width(VALUE self)
{
    yaml_emitter_t * emitter;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    return INT2NUM(emitter->best_width);
}
            
line_width = width click to toggle source

Set the preferred line with to width.

 
               static VALUE set_line_width(VALUE self, VALUE width)
{
    yaml_emitter_t * emitter;
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_emitter_set_width(emitter, NUM2INT(width));

    return width;
}
            
scalar(value, anchor, tag, plain, quoted, style) click to toggle source

Emit a scalar with value, anchor, tag, and a plain or quoted string type with style.

See Psych::Handler#scalar

 
               static VALUE scalar(
        VALUE self,
        VALUE value,
        VALUE anchor,
        VALUE tag,
        VALUE plain,
        VALUE quoted,
        VALUE style
        ) {
    yaml_emitter_t * emitter;
    yaml_event_t event;
#ifdef HAVE_RUBY_ENCODING_H
    rb_encoding *encoding;
#endif
    Data_Get_Struct(self, yaml_emitter_t, emitter);

    Check_Type(value, T_STRING);

#ifdef HAVE_RUBY_ENCODING_H
    encoding = rb_utf8_encoding();

    value = rb_str_export_to_enc(value, encoding);

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, encoding);
    }

    if(!NIL_P(tag)) {
        Check_Type(tag, T_STRING);
        tag = rb_str_export_to_enc(tag, encoding);
    }
#endif

    yaml_scalar_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
            (yaml_char_t*)StringValuePtr(value),
            (int)RSTRING_LEN(value),
            plain ? 1 : 0,
            quoted ? 1 : 0,
            (yaml_scalar_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}
            
start_document(version, tags, implicit) click to toggle source

Start a document emission with YAML version, tags, and an implicit start.

See Psych::Handler#start_document

 
               static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
{
    yaml_emitter_t * emitter;
    yaml_tag_directive_t * head = NULL;
    yaml_tag_directive_t * tail = NULL;
    yaml_event_t event;
    yaml_version_directive_t version_directive;
    Data_Get_Struct(self, yaml_emitter_t, emitter);


    Check_Type(version, T_ARRAY);

    if(RARRAY_LEN(version) > 0) {
        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);
    }

    if(RTEST(tags)) {
        int i = 0;
#ifdef HAVE_RUBY_ENCODING_H
        rb_encoding * encoding = rb_utf8_encoding();
#endif

        Check_Type(tags, T_ARRAY);

        head  = xcalloc((size_t)RARRAY_LEN(tags), sizeof(yaml_tag_directive_t));
        tail  = head;

        for(i = 0; i < RARRAY_LEN(tags); i++) {
            VALUE tuple = RARRAY_PTR(tags)[i];
            VALUE name;
            VALUE value;

            Check_Type(tuple, T_ARRAY);

            if(RARRAY_LEN(tuple) < 2) {
                xfree(head);
                rb_raise(rb_eRuntimeError, "tag tuple must be of length 2");
            }
            name  = RARRAY_PTR(tuple)[0];
            value = RARRAY_PTR(tuple)[1];
#ifdef HAVE_RUBY_ENCODING_H
            name = rb_str_export_to_enc(name, encoding);
            value = rb_str_export_to_enc(value, encoding);
#endif

            tail->handle = (yaml_char_t *)StringValuePtr(name);
            tail->prefix = (yaml_char_t *)StringValuePtr(value);

            tail++;
        }
    }

    yaml_document_start_event_initialize(
            &event,
            (RARRAY_LEN(version) > 0) ? &version_directive : NULL,
            head,
            tail,
            imp ? 1 : 0
            );

    emit(emitter, &event);

    if(head) xfree(head);

    return self;
}
            
start_mapping(anchor, tag, implicit, style) click to toggle source

Start emitting a YAML map with anchor, tag, an implicit start and end, and style.

See Psych::Handler#start_mapping

 
               static VALUE start_mapping(
        VALUE self,
        VALUE anchor,
        VALUE tag,
        VALUE implicit,
        VALUE style
        ) {
    yaml_emitter_t * emitter;
    yaml_event_t event;
#ifdef HAVE_RUBY_ENCODING_H
    rb_encoding *encoding;
#endif
    Data_Get_Struct(self, yaml_emitter_t, emitter);

#ifdef HAVE_RUBY_ENCODING_H
    encoding = rb_utf8_encoding();

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, encoding);
    }

    if(!NIL_P(tag)) {
        Check_Type(tag, T_STRING);
        tag = rb_str_export_to_enc(tag, encoding);
    }
#endif

    yaml_mapping_start_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
            implicit ? 1 : 0,
            (yaml_mapping_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}
            
start_sequence(anchor, tag, implicit, style) click to toggle source

Start emitting a sequence with anchor, a tag, implicit sequence start and end, along with style.

See Psych::Handler#start_sequence

 
               static VALUE start_sequence(
        VALUE self,
        VALUE anchor,
        VALUE tag,
        VALUE implicit,
        VALUE style
        ) {
    yaml_emitter_t * emitter;
    yaml_event_t event;

#ifdef HAVE_RUBY_ENCODING_H
    rb_encoding * encoding = rb_utf8_encoding();

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, encoding);
    }

    if(!NIL_P(tag)) {
        Check_Type(tag, T_STRING);
        tag = rb_str_export_to_enc(tag, encoding);
    }
#endif

    Data_Get_Struct(self, yaml_emitter_t, emitter);

    yaml_sequence_start_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
            implicit ? 1 : 0,
            (yaml_sequence_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}
            
start_stream(encoding) click to toggle source

Start a stream emission with encoding

See Psych::Handler#start_stream

 
               static VALUE start_stream(VALUE self, VALUE encoding)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    Data_Get_Struct(self, yaml_emitter_t, emitter);
    Check_Type(encoding, T_FIXNUM);

    yaml_stream_start_event_initialize(&event, (yaml_encoding_t)NUM2INT(encoding));

    emit(emitter, &event);

    return self;
}
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.