類別 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 按一下以切換原始碼

設定 縮排層級為 層級。層級必須小於 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;
}
行寬 按一下以切換來源

取得偏好的行寬。

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);
}
行寬 = 寬度 按一下以切換來源

設定 偏好的行寬為 寬度

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;
}
標量 (值、錨點、標籤、純文字、引號、樣式) 按一下以切換來源

發出具有 錨點標籤,以及 純文字引號 字串類型和 樣式 的標量。

請參閱 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);
    }

    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*)StringValuePtr(value),
            (int)RSTRING_LEN(value),
            plain ? 1 : 0,
            quoted ? 1 : 0,
            (yaml_scalar_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}
開始文件 (版本、標籤、隱含) 按一下以切換來源

使用 YAML 版本標籤隱含 開始發出文件。

請參閱 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;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, 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)) {
        long i = 0;
        long len;
        rb_encoding * encoding = rb_utf8_encoding();

        Check_Type(tags, T_ARRAY);

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

        for(i = 0; i < len && i < RARRAY_LEN(tags); i++) {
            VALUE tuple = RARRAY_AREF(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_AREF(tuple, 0);
            value = RARRAY_AREF(tuple, 1);
            StringValue(name);
            StringValue(value);
            name = rb_str_export_to_enc(name, encoding);
            value = rb_str_export_to_enc(value, encoding);

            tail->handle = (yaml_char_t *)StringValueCStr(name);
            tail->prefix = (yaml_char_t *)StringValueCStr(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;
}
開始對應 (錨點、標籤、隱含、樣式) 按一下以切換來源

開始發出具有 錨點標籤隱含 開始和結束,以及 樣式YAML 對應。

請參閱 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;
}
開始序列 (錨點、標籤、隱含、樣式) 按一下以切換來源

開始發出具有 錨點標籤隱含 序列開始和結束,以及 樣式 的序列。

請參閱 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;
}
開始串流 (編碼) 按一下以切換來源

使用 編碼 開始發出串流

請參閱 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;
}