class Psych::Emitter

公共类方法

Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS) 点击切换源码

创建一个新的 Psych::Emitter,它将写入到 io

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

    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, 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);
    }

    rb_ivar_set(self, id_io, io);
    yaml_emitter_set_output(emitter, writer, (void *)self);

    return self;
}

公共实例方法

alias(anchor) 点击切换源码

使用 anchor 发射一个别名。

参见 Psych::Handler#alias

static VALUE alias(VALUE self, VALUE anchor)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

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

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

    emit(emitter, &event);

    return self;
}
canonical 点击切换源码

获取输出样式,是否为规范模式。

static VALUE canonical(VALUE self)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    return (emitter->canonical == 0) ? Qfalse : Qtrue;
}
canonical = true 点击切换源码

将输出样式 Set 为规范模式,或者不使用规范模式。

static VALUE set_canonical(VALUE self, VALUE style)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

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

    return style;
}
end_document(implicit) 点击切换源码

结束一个文档发射,使用一个 implicit 结尾。

参见 Psych::Handler#end_document

static VALUE end_document(VALUE self, VALUE imp)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

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

    emit(emitter, &event);

    return self;
}
end_mapping 点击切换源码

发射一个映射的结束。

参见 Psych::Handler#end_mapping

static VALUE end_mapping(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_mapping_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}
end_sequence 点击切换源码

结束序列发射。

参见 Psych::Handler#end_sequence

static VALUE end_sequence(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_sequence_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}
end_stream 点击切换源码

结束一个流发射

参见 Psych::Handler#end_stream

static VALUE end_stream(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_stream_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}
indentation 点击切换源码

获取缩进级别。

static VALUE indentation(VALUE self)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    return INT2NUM(emitter->best_indent);
}
indentation = level 点击切换源码

将缩进级别 Setlevel。级别必须小于 10 且大于 1。

static VALUE set_indentation(VALUE self, VALUE level)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_emitter_set_indent(emitter, NUM2INT(level));

    return level;
}
line_width 点击切换源码

获取首选的行宽。

static VALUE line_width(VALUE self)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    return INT2NUM(emitter->best_width);
}
line_width = width 点击切换源码

将首选的行宽 Setwidth

static VALUE set_line_width(VALUE self, VALUE width)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_emitter_set_width(emitter, NUM2INT(width));

    return width;
}
scalar(value, anchor, tag, plain, quoted, style) 点击切换源码

使用 value, anchor, tagplainquoted 字符串类型与 style 发射一个标量。

参见 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;
    rb_encoding *encoding;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    Check_Type(value, T_STRING);

    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);
    }

    const char *value_ptr = StringValuePtr(value);
    yaml_scalar_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
            (yaml_char_t*)value_ptr,
            (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) 点击切换源码

开始一个文档发射,使用 YAML versiontags 和一个 implicit 开始。

参见 Psych::Handler#start_document

static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
{
    struct start_document_data data = {
        .self = self,
        .version = version,
        .tags = tags,
        .imp = imp,

        .head = NULL,
    };

    return rb_ensure(start_document_try, (VALUE)&data, start_document_ensure, (VALUE)&data);
}
start_mapping(anchor, tag, implicit, style) 点击切换源码

开始发射一个 YAML 映射,使用 anchor, tag,一个 implicit 开始和结束,以及 style

参见 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;
    rb_encoding *encoding;

    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    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);
    }

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

    emit(emitter, &event);

    return self;
}
start_sequence(anchor, tag, implicit, style) 点击切换源码

开始发射一个序列,使用 anchor,一个 tagimplicit 序列开始和结束,以及 style

参见 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;

    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);
    }

    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

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

    emit(emitter, &event);

    return self;
}
start_stream(encoding) 点击切换源码

开始一个流发射,使用 encoding

参见 Psych::Handler#start_stream

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

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

    emit(emitter, &event);

    return self;
}