Class: Psych::Emitter

Inherits:
Handler show all
Defined in:
psych_emitter.c

Direct Known Subclasses

Stream::Emitter

Constant Summary

Constants inherited from Handler

Handler::EVENTS, Handler::OPTIONS

Instance Method Summary collapse

Methods inherited from Handler

#empty, #streaming?

Constructor Details

#Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'psych_emitter.c', line 67

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

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

    return self;
}

Instance Method Details

#alias(anchor) ⇒ Object

Emit an alias with anchor.

See Psych::Handler#alias



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'psych_emitter.c', line 423

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

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

#canonicalObject

Get the output style, canonical or not.



464
465
466
467
468
469
470
# File 'psych_emitter.c', line 464

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) ⇒ Object

Set the output style to canonical, or not.



450
451
452
453
454
455
456
457
458
# File 'psych_emitter.c', line 450

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) ⇒ Object

End a document emission with an implicit ending.

See Psych::Handler#end_document



215
216
217
218
219
220
221
222
223
224
225
226
# File 'psych_emitter.c', line 215

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_mappingObject

Emit the end of a mapping.

See Psych::Handler#end_mapping



404
405
406
407
408
409
410
411
412
413
414
415
# File 'psych_emitter.c', line 404

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_sequenceObject

End sequence emission.

See Psych::Handler#end_sequence



337
338
339
340
341
342
343
344
345
346
347
348
# File 'psych_emitter.c', line 337

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_streamObject

End a stream emission

See Psych::Handler#end_stream



118
119
120
121
122
123
124
125
126
127
128
129
# File 'psych_emitter.c', line 118

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

#indentationObject

Get the indentation level.



491
492
493
494
495
496
497
# File 'psych_emitter.c', line 491

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) ⇒ Object

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



477
478
479
480
481
482
483
484
485
# File 'psych_emitter.c', line 477

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_widthObject

Get the preferred line width.



503
504
505
506
507
508
509
# File 'psych_emitter.c', line 503

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) ⇒ Object

Set the preferred line with to width.



515
516
517
518
519
520
521
522
523
# File 'psych_emitter.c', line 515

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) ⇒ Object

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

See Psych::Handler#scalar



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'psych_emitter.c', line 235

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

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

See Psych::Handler#start_document



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'psych_emitter.c', line 138

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)) {
	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) ⇒ Object

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

See Psych::Handler#start_mapping



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'psych_emitter.c', line 357

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

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

See Psych::Handler#start_sequence



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'psych_emitter.c', line 292

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

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

Start a stream emission with encoding

See Psych::Handler#start_stream



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'psych_emitter.c', line 98

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