km-psych 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +129 -0
- data/ext/psych/emitter.c +488 -0
- data/ext/psych/emitter.h +8 -0
- data/ext/psych/extconf.rb +22 -0
- data/ext/psych/parser.c +349 -0
- data/ext/psych/parser.h +6 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +20 -0
- data/ext/psych/to_ruby.c +41 -0
- data/ext/psych/to_ruby.h +8 -0
- data/ext/psych/yaml_tree.c +24 -0
- data/ext/psych/yaml_tree.h +8 -0
- data/lib/km-psych.rb +244 -0
- data/lib/psych/coder.rb +86 -0
- data/lib/psych/core_ext.rb +38 -0
- data/lib/psych/deprecated.rb +82 -0
- data/lib/psych/handler.rb +221 -0
- data/lib/psych/json.rb +6 -0
- data/lib/psych/json/stream.rb +32 -0
- data/lib/psych/json/tree_builder.rb +32 -0
- data/lib/psych/nodes.rb +77 -0
- data/lib/psych/nodes/alias.rb +18 -0
- data/lib/psych/nodes/document.rb +60 -0
- data/lib/psych/nodes/mapping.rb +56 -0
- data/lib/psych/nodes/node.rb +42 -0
- data/lib/psych/nodes/scalar.rb +67 -0
- data/lib/psych/nodes/sequence.rb +81 -0
- data/lib/psych/nodes/stream.rb +37 -0
- data/lib/psych/omap.rb +4 -0
- data/lib/psych/parser.rb +44 -0
- data/lib/psych/scalar_scanner.rb +105 -0
- data/lib/psych/set.rb +4 -0
- data/lib/psych/stream.rb +53 -0
- data/lib/psych/tree_builder.rb +94 -0
- data/lib/psych/visitors.rb +5 -0
- data/lib/psych/visitors/emitter.rb +41 -0
- data/lib/psych/visitors/json_tree.rb +14 -0
- data/lib/psych/visitors/to_ruby.rb +263 -0
- data/lib/psych/visitors/visitor.rb +27 -0
- data/lib/psych/visitors/yaml_tree.rb +342 -0
- data/test/psych/helper.rb +63 -0
- data/test/psych/json/test_stream.rb +75 -0
- data/test/psych/test_alias_and_anchor.rb +26 -0
- data/test/psych/test_array.rb +19 -0
- data/test/psych/test_boolean.rb +36 -0
- data/test/psych/test_class.rb +17 -0
- data/test/psych/test_coder.rb +169 -0
- data/test/psych/test_date_time.rb +17 -0
- data/test/psych/test_deprecated.rb +210 -0
- data/test/psych/test_document.rb +46 -0
- data/test/psych/test_emitter.rb +88 -0
- data/test/psych/test_encoding.rb +179 -0
- data/test/psych/test_engine_manager.rb +57 -0
- data/test/psych/test_exception.rb +39 -0
- data/test/psych/test_hash.rb +30 -0
- data/test/psych/test_json_tree.rb +43 -0
- data/test/psych/test_null.rb +19 -0
- data/test/psych/test_object.rb +27 -0
- data/test/psych/test_omap.rb +68 -0
- data/test/psych/test_parser.rb +216 -0
- data/test/psych/test_psych.rb +133 -0
- data/test/psych/test_scalar.rb +11 -0
- data/test/psych/test_scalar_scanner.rb +70 -0
- data/test/psych/test_serialize_subclasses.rb +38 -0
- data/test/psych/test_set.rb +49 -0
- data/test/psych/test_stream.rb +49 -0
- data/test/psych/test_string.rb +49 -0
- data/test/psych/test_struct.rb +51 -0
- data/test/psych/test_symbol.rb +17 -0
- data/test/psych/test_to_yaml_properties.rb +63 -0
- data/test/psych/test_tree_builder.rb +79 -0
- data/test/psych/test_yaml.rb +1251 -0
- data/test/psych/visitors/test_emitter.rb +124 -0
- data/test/psych/visitors/test_to_ruby.rb +325 -0
- data/test/psych/visitors/test_yaml_tree.rb +149 -0
- metadata +187 -0
data/README.rdoc
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
= Psych
|
2
|
+
|
3
|
+
* http://github.com/tenderlove/psych
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
Psych is a YAML parser and emitter. Psych leverages libyaml[http://libyaml.org]
|
8
|
+
for it's YAML parsing and emitting capabilities. In addition to wrapping
|
9
|
+
libyaml, Psych also knows how to serialize and de-serialize most Ruby objects
|
10
|
+
to and from the YAML format.
|
11
|
+
|
12
|
+
== Install
|
13
|
+
|
14
|
+
You need Ruby 1.9.2-p0 or higher. Also install libyaml fx in one of the following ways...
|
15
|
+
|
16
|
+
$ brew install libyaml
|
17
|
+
$ port install libyaml +universal
|
18
|
+
$ yum install libyaml-devel
|
19
|
+
|
20
|
+
== Examples
|
21
|
+
|
22
|
+
# Load YAML in to a Ruby object
|
23
|
+
Psych.load('--- foo') # => 'foo'
|
24
|
+
|
25
|
+
# Emit YAML from a Ruby object
|
26
|
+
Psych.dump("foo") # => "--- foo\n...\n"
|
27
|
+
|
28
|
+
== Documentation
|
29
|
+
|
30
|
+
http://tenderlovemaking.com/2010/04/17/event-based-json-and-yaml-parsing/
|
31
|
+
|
32
|
+
Our event listener is only going to listen for scalar events, meaning that when Psych parses a string, it will send that string to our listener. There are many different events that can happen, so Psych ships with a handler from which you can inherit. If you check out the source for the base class handler, you can see what types of events your handler can intercept.
|
33
|
+
|
34
|
+
require 'psych'
|
35
|
+
|
36
|
+
class Listener < Psych::Handler
|
37
|
+
def scalar(value, anchor, tag, plain, quoted, style)
|
38
|
+
puts value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
listener = Listener.new
|
43
|
+
parser = Psych::Parser.new listener
|
44
|
+
parser.parse DATA
|
45
|
+
|
46
|
+
__END__
|
47
|
+
{"foo":"bar"}
|
48
|
+
|
49
|
+
In this example, our handler simply prints out every scalar value encountered. We created a new instance of the listener, pass that listener to a new instance of the parser, and tell the parser to parse DATA. We can hand the parser an IO object or a String object. This is important because we’d like to hand the parser our socket connection, that way the parser can deal with reading from the socket for us.
|
50
|
+
|
51
|
+
require 'socket'
|
52
|
+
require 'psych'
|
53
|
+
|
54
|
+
class StreamClient
|
55
|
+
def initialize user, pass
|
56
|
+
@ba = ["#{user}:#{pass}"].pack('m').chomp
|
57
|
+
end
|
58
|
+
|
59
|
+
def listen listener
|
60
|
+
socket = TCPSocket.new 'stream.twitter.com', 80
|
61
|
+
# .. (authentication) ...
|
62
|
+
|
63
|
+
# Read the headers
|
64
|
+
while((line = socket.readline) != "\r\n"); puts line if $DEBUG; end
|
65
|
+
|
66
|
+
reader, writer = IO.pipe
|
67
|
+
producer = Thread.new(socket, writer) do |s, io|
|
68
|
+
loop do
|
69
|
+
io.write "---\n"
|
70
|
+
io.write s.read s.readline.strip.to_i 16
|
71
|
+
io.write "...\n"
|
72
|
+
s.read 2 # strip the blank line
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
parser = Psych::Parser.new listener
|
77
|
+
parser.parse reader
|
78
|
+
|
79
|
+
producer.join
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Listener < Psych::Handler
|
84
|
+
def initialize
|
85
|
+
@was_text = false
|
86
|
+
end
|
87
|
+
|
88
|
+
def scalar value, anchor, tag, plain, quoted, style
|
89
|
+
puts value if @was_text
|
90
|
+
@was_text = value == 'text'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
StreamClient.new(ARGV[0], ARGV[1]).listen Listener.new
|
95
|
+
|
96
|
+
We configure the “listen” method in our client to munge the feed to a pipe, and hand that off to our YAML processor.
|
97
|
+
I only care about the text of people’s tweets, so let’s modify our listener too.
|
98
|
+
|
99
|
+
== Dependencies
|
100
|
+
|
101
|
+
* libyaml
|
102
|
+
|
103
|
+
== Installation
|
104
|
+
|
105
|
+
* sudo port install libyaml +universal
|
106
|
+
* sudo gem install psych
|
107
|
+
|
108
|
+
== License
|
109
|
+
|
110
|
+
Copyright 2009 Aaron Patterson, et al.
|
111
|
+
|
112
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
113
|
+
a copy of this software and associated documentation files (the
|
114
|
+
'Software'), to deal in the Software without restriction, including
|
115
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
116
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
117
|
+
permit persons to whom the Software is furnished to do so, subject to
|
118
|
+
the following conditions:
|
119
|
+
|
120
|
+
The above copyright notice and this permission notice shall be
|
121
|
+
included in all copies or substantial portions of the Software.
|
122
|
+
|
123
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
124
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
125
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
126
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
127
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
128
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
129
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/ext/psych/emitter.c
ADDED
@@ -0,0 +1,488 @@
|
|
1
|
+
#include <psych.h>
|
2
|
+
|
3
|
+
VALUE cPsychEmitter;
|
4
|
+
static ID id_write;
|
5
|
+
|
6
|
+
static void emit(yaml_emitter_t * emitter, yaml_event_t * event)
|
7
|
+
{
|
8
|
+
if(!yaml_emitter_emit(emitter, event))
|
9
|
+
rb_raise(rb_eRuntimeError, "%s", emitter->problem);
|
10
|
+
}
|
11
|
+
|
12
|
+
static int writer(void *ctx, unsigned char *buffer, size_t size)
|
13
|
+
{
|
14
|
+
VALUE io = (VALUE)ctx;
|
15
|
+
VALUE str = rb_str_new((const char *)buffer, (long)size);
|
16
|
+
VALUE wrote = rb_funcall(io, id_write, 1, str);
|
17
|
+
return (int)NUM2INT(wrote);
|
18
|
+
}
|
19
|
+
|
20
|
+
static void dealloc(void * ptr)
|
21
|
+
{
|
22
|
+
yaml_emitter_t * emitter;
|
23
|
+
|
24
|
+
emitter = (yaml_emitter_t *)ptr;
|
25
|
+
yaml_emitter_delete(emitter);
|
26
|
+
xfree(emitter);
|
27
|
+
}
|
28
|
+
|
29
|
+
static VALUE allocate(VALUE klass)
|
30
|
+
{
|
31
|
+
yaml_emitter_t * emitter;
|
32
|
+
|
33
|
+
emitter = xmalloc(sizeof(yaml_emitter_t));
|
34
|
+
|
35
|
+
yaml_emitter_initialize(emitter);
|
36
|
+
yaml_emitter_set_unicode(emitter, 1);
|
37
|
+
yaml_emitter_set_indent(emitter, 2);
|
38
|
+
|
39
|
+
return Data_Wrap_Struct(klass, 0, dealloc, emitter);
|
40
|
+
}
|
41
|
+
|
42
|
+
/* call-seq: Psych::Emitter.new(io)
|
43
|
+
*
|
44
|
+
* Create a new Psych::Emitter that writes to +io+.
|
45
|
+
*/
|
46
|
+
static VALUE initialize(VALUE self, VALUE io)
|
47
|
+
{
|
48
|
+
yaml_emitter_t * emitter;
|
49
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
50
|
+
|
51
|
+
yaml_emitter_set_output(emitter, writer, (void *)io);
|
52
|
+
|
53
|
+
return self;
|
54
|
+
}
|
55
|
+
|
56
|
+
/* call-seq: emitter.start_stream(encoding)
|
57
|
+
*
|
58
|
+
* Start a stream emission with +encoding+
|
59
|
+
*
|
60
|
+
* See Psych::Handler#start_stream
|
61
|
+
*/
|
62
|
+
static VALUE start_stream(VALUE self, VALUE encoding)
|
63
|
+
{
|
64
|
+
yaml_emitter_t * emitter;
|
65
|
+
yaml_event_t event;
|
66
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
67
|
+
Check_Type(encoding, T_FIXNUM);
|
68
|
+
|
69
|
+
yaml_stream_start_event_initialize(&event, (yaml_encoding_t)NUM2INT(encoding));
|
70
|
+
|
71
|
+
emit(emitter, &event);
|
72
|
+
|
73
|
+
return self;
|
74
|
+
}
|
75
|
+
|
76
|
+
/* call-seq: emitter.end_stream
|
77
|
+
*
|
78
|
+
* End a stream emission
|
79
|
+
*
|
80
|
+
* See Psych::Handler#end_stream
|
81
|
+
*/
|
82
|
+
static VALUE end_stream(VALUE self)
|
83
|
+
{
|
84
|
+
yaml_emitter_t * emitter;
|
85
|
+
yaml_event_t event;
|
86
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
87
|
+
|
88
|
+
yaml_stream_end_event_initialize(&event);
|
89
|
+
|
90
|
+
emit(emitter, &event);
|
91
|
+
|
92
|
+
return self;
|
93
|
+
}
|
94
|
+
|
95
|
+
/* call-seq: emitter.start_document(version, tags, implicit)
|
96
|
+
*
|
97
|
+
* Start a document emission with YAML +version+, +tags+, and an +implicit+
|
98
|
+
* start.
|
99
|
+
*
|
100
|
+
* See Psych::Handler#start_document
|
101
|
+
*/
|
102
|
+
static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
|
103
|
+
{
|
104
|
+
yaml_emitter_t * emitter;
|
105
|
+
yaml_tag_directive_t * head = NULL;
|
106
|
+
yaml_tag_directive_t * tail = NULL;
|
107
|
+
yaml_event_t event;
|
108
|
+
yaml_version_directive_t version_directive;
|
109
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
110
|
+
|
111
|
+
|
112
|
+
Check_Type(version, T_ARRAY);
|
113
|
+
|
114
|
+
if(RARRAY_LEN(version) > 0) {
|
115
|
+
VALUE major = rb_ary_entry(version, (long)0);
|
116
|
+
VALUE minor = rb_ary_entry(version, (long)1);
|
117
|
+
|
118
|
+
version_directive.major = NUM2INT(major);
|
119
|
+
version_directive.minor = NUM2INT(minor);
|
120
|
+
}
|
121
|
+
|
122
|
+
if(RTEST(tags)) {
|
123
|
+
int i = 0;
|
124
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
125
|
+
rb_encoding * encoding = rb_utf8_encoding();
|
126
|
+
#endif
|
127
|
+
|
128
|
+
Check_Type(tags, T_ARRAY);
|
129
|
+
|
130
|
+
head = xcalloc((size_t)RARRAY_LEN(tags), sizeof(yaml_tag_directive_t));
|
131
|
+
tail = head;
|
132
|
+
|
133
|
+
for(i = 0; i < RARRAY_LEN(tags); i++) {
|
134
|
+
VALUE tuple = RARRAY_PTR(tags)[i];
|
135
|
+
VALUE name;
|
136
|
+
VALUE value;
|
137
|
+
|
138
|
+
Check_Type(tuple, T_ARRAY);
|
139
|
+
|
140
|
+
if(RARRAY_LEN(tuple) < 2) {
|
141
|
+
xfree(head);
|
142
|
+
rb_raise(rb_eRuntimeError, "tag tuple must be of length 2");
|
143
|
+
}
|
144
|
+
name = RARRAY_PTR(tuple)[0];
|
145
|
+
value = RARRAY_PTR(tuple)[1];
|
146
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
147
|
+
name = rb_str_export_to_enc(name, encoding);
|
148
|
+
value = rb_str_export_to_enc(value, encoding);
|
149
|
+
#endif
|
150
|
+
|
151
|
+
tail->handle = (yaml_char_t *)StringValuePtr(name);
|
152
|
+
tail->prefix = (yaml_char_t *)StringValuePtr(value);
|
153
|
+
|
154
|
+
tail++;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
yaml_document_start_event_initialize(
|
159
|
+
&event,
|
160
|
+
(RARRAY_LEN(version) > 0) ? &version_directive : NULL,
|
161
|
+
head,
|
162
|
+
tail,
|
163
|
+
imp ? 1 : 0
|
164
|
+
);
|
165
|
+
|
166
|
+
emit(emitter, &event);
|
167
|
+
|
168
|
+
if(head) xfree(head);
|
169
|
+
|
170
|
+
return self;
|
171
|
+
}
|
172
|
+
|
173
|
+
/* call-seq: emitter.end_document(implicit)
|
174
|
+
*
|
175
|
+
* End a document emission with an +implicit+ ending.
|
176
|
+
*
|
177
|
+
* See Psych::Handler#end_document
|
178
|
+
*/
|
179
|
+
static VALUE end_document(VALUE self, VALUE imp)
|
180
|
+
{
|
181
|
+
yaml_emitter_t * emitter;
|
182
|
+
yaml_event_t event;
|
183
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
184
|
+
|
185
|
+
yaml_document_end_event_initialize(&event, imp ? 1 : 0);
|
186
|
+
|
187
|
+
emit(emitter, &event);
|
188
|
+
|
189
|
+
return self;
|
190
|
+
}
|
191
|
+
|
192
|
+
/* call-seq: emitter.scalar(value, anchor, tag, plain, quoted, style)
|
193
|
+
*
|
194
|
+
* Emit a scalar with +value+, +anchor+, +tag+, and a +plain+ or +quoted+
|
195
|
+
* string type with +style+.
|
196
|
+
*
|
197
|
+
* See Psych::Handler#scalar
|
198
|
+
*/
|
199
|
+
static VALUE scalar(
|
200
|
+
VALUE self,
|
201
|
+
VALUE value,
|
202
|
+
VALUE anchor,
|
203
|
+
VALUE tag,
|
204
|
+
VALUE plain,
|
205
|
+
VALUE quoted,
|
206
|
+
VALUE style
|
207
|
+
) {
|
208
|
+
yaml_emitter_t * emitter;
|
209
|
+
yaml_event_t event;
|
210
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
211
|
+
rb_encoding *encoding;
|
212
|
+
#endif
|
213
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
214
|
+
|
215
|
+
Check_Type(value, T_STRING);
|
216
|
+
|
217
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
218
|
+
encoding = rb_utf8_encoding();
|
219
|
+
|
220
|
+
value = rb_str_export_to_enc(value, encoding);
|
221
|
+
|
222
|
+
if(!NIL_P(anchor)) {
|
223
|
+
Check_Type(anchor, T_STRING);
|
224
|
+
anchor = rb_str_export_to_enc(anchor, encoding);
|
225
|
+
}
|
226
|
+
|
227
|
+
if(!NIL_P(tag)) {
|
228
|
+
Check_Type(tag, T_STRING);
|
229
|
+
tag = rb_str_export_to_enc(tag, encoding);
|
230
|
+
}
|
231
|
+
#endif
|
232
|
+
|
233
|
+
yaml_scalar_event_initialize(
|
234
|
+
&event,
|
235
|
+
(yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
|
236
|
+
(yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
|
237
|
+
(yaml_char_t*)StringValuePtr(value),
|
238
|
+
(int)RSTRING_LEN(value),
|
239
|
+
plain ? 1 : 0,
|
240
|
+
quoted ? 1 : 0,
|
241
|
+
(yaml_scalar_style_t)NUM2INT(style)
|
242
|
+
);
|
243
|
+
|
244
|
+
emit(emitter, &event);
|
245
|
+
|
246
|
+
return self;
|
247
|
+
}
|
248
|
+
|
249
|
+
/* call-seq: emitter.start_sequence(anchor, tag, implicit, style)
|
250
|
+
*
|
251
|
+
* Start emitting a sequence with +anchor+, a +tag+, +implicit+ sequence
|
252
|
+
* start and end, along with +style+.
|
253
|
+
*
|
254
|
+
* See Psych::Handler#start_sequence
|
255
|
+
*/
|
256
|
+
static VALUE start_sequence(
|
257
|
+
VALUE self,
|
258
|
+
VALUE anchor,
|
259
|
+
VALUE tag,
|
260
|
+
VALUE implicit,
|
261
|
+
VALUE style
|
262
|
+
) {
|
263
|
+
yaml_emitter_t * emitter;
|
264
|
+
yaml_event_t event;
|
265
|
+
|
266
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
267
|
+
rb_encoding * encoding = rb_utf8_encoding();
|
268
|
+
|
269
|
+
if(!NIL_P(anchor)) {
|
270
|
+
Check_Type(anchor, T_STRING);
|
271
|
+
anchor = rb_str_export_to_enc(anchor, encoding);
|
272
|
+
}
|
273
|
+
|
274
|
+
if(!NIL_P(tag)) {
|
275
|
+
Check_Type(tag, T_STRING);
|
276
|
+
tag = rb_str_export_to_enc(tag, encoding);
|
277
|
+
}
|
278
|
+
#endif
|
279
|
+
|
280
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
281
|
+
|
282
|
+
yaml_sequence_start_event_initialize(
|
283
|
+
&event,
|
284
|
+
(yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
|
285
|
+
(yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
|
286
|
+
implicit ? 1 : 0,
|
287
|
+
(yaml_sequence_style_t)NUM2INT(style)
|
288
|
+
);
|
289
|
+
|
290
|
+
emit(emitter, &event);
|
291
|
+
|
292
|
+
return self;
|
293
|
+
}
|
294
|
+
|
295
|
+
/* call-seq: emitter.end_sequence
|
296
|
+
*
|
297
|
+
* End sequence emission.
|
298
|
+
*
|
299
|
+
* See Psych::Handler#end_sequence
|
300
|
+
*/
|
301
|
+
static VALUE end_sequence(VALUE self)
|
302
|
+
{
|
303
|
+
yaml_emitter_t * emitter;
|
304
|
+
yaml_event_t event;
|
305
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
306
|
+
|
307
|
+
yaml_sequence_end_event_initialize(&event);
|
308
|
+
|
309
|
+
emit(emitter, &event);
|
310
|
+
|
311
|
+
return self;
|
312
|
+
}
|
313
|
+
|
314
|
+
/* call-seq: emitter.start_mapping(anchor, tag, implicit, style)
|
315
|
+
*
|
316
|
+
* Start emitting a YAML map with +anchor+, +tag+, an +implicit+ start
|
317
|
+
* and end, and +style+.
|
318
|
+
*
|
319
|
+
* See Psych::Handler#start_mapping
|
320
|
+
*/
|
321
|
+
static VALUE start_mapping(
|
322
|
+
VALUE self,
|
323
|
+
VALUE anchor,
|
324
|
+
VALUE tag,
|
325
|
+
VALUE implicit,
|
326
|
+
VALUE style
|
327
|
+
) {
|
328
|
+
yaml_emitter_t * emitter;
|
329
|
+
yaml_event_t event;
|
330
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
331
|
+
rb_encoding *encoding;
|
332
|
+
#endif
|
333
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
334
|
+
|
335
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
336
|
+
encoding = rb_utf8_encoding();
|
337
|
+
|
338
|
+
if(!NIL_P(anchor)) {
|
339
|
+
Check_Type(anchor, T_STRING);
|
340
|
+
anchor = rb_str_export_to_enc(anchor, encoding);
|
341
|
+
}
|
342
|
+
|
343
|
+
if(!NIL_P(tag)) {
|
344
|
+
Check_Type(tag, T_STRING);
|
345
|
+
tag = rb_str_export_to_enc(tag, encoding);
|
346
|
+
}
|
347
|
+
#endif
|
348
|
+
|
349
|
+
yaml_mapping_start_event_initialize(
|
350
|
+
&event,
|
351
|
+
(yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
|
352
|
+
(yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
|
353
|
+
implicit ? 1 : 0,
|
354
|
+
(yaml_sequence_style_t)NUM2INT(style)
|
355
|
+
);
|
356
|
+
|
357
|
+
emit(emitter, &event);
|
358
|
+
|
359
|
+
return self;
|
360
|
+
}
|
361
|
+
|
362
|
+
/* call-seq: emitter.end_mapping
|
363
|
+
*
|
364
|
+
* Emit the end of a mapping.
|
365
|
+
*
|
366
|
+
* See Psych::Handler#end_mapping
|
367
|
+
*/
|
368
|
+
static VALUE end_mapping(VALUE self)
|
369
|
+
{
|
370
|
+
yaml_emitter_t * emitter;
|
371
|
+
yaml_event_t event;
|
372
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
373
|
+
|
374
|
+
yaml_mapping_end_event_initialize(&event);
|
375
|
+
|
376
|
+
emit(emitter, &event);
|
377
|
+
|
378
|
+
return self;
|
379
|
+
}
|
380
|
+
|
381
|
+
/* call-seq: emitter.alias(anchor)
|
382
|
+
*
|
383
|
+
* Emit an alias with +anchor+.
|
384
|
+
*
|
385
|
+
* See Psych::Handler#alias
|
386
|
+
*/
|
387
|
+
static VALUE alias(VALUE self, VALUE anchor)
|
388
|
+
{
|
389
|
+
yaml_emitter_t * emitter;
|
390
|
+
yaml_event_t event;
|
391
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
392
|
+
|
393
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
394
|
+
if(!NIL_P(anchor)) {
|
395
|
+
Check_Type(anchor, T_STRING);
|
396
|
+
anchor = rb_str_export_to_enc(anchor, rb_utf8_encoding());
|
397
|
+
}
|
398
|
+
#endif
|
399
|
+
|
400
|
+
yaml_alias_event_initialize(
|
401
|
+
&event,
|
402
|
+
(yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor))
|
403
|
+
);
|
404
|
+
|
405
|
+
emit(emitter, &event);
|
406
|
+
|
407
|
+
return self;
|
408
|
+
}
|
409
|
+
|
410
|
+
/* call-seq: emitter.canonical = true
|
411
|
+
*
|
412
|
+
* Set the output style to canonical, or not.
|
413
|
+
*/
|
414
|
+
static VALUE set_canonical(VALUE self, VALUE style)
|
415
|
+
{
|
416
|
+
yaml_emitter_t * emitter;
|
417
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
418
|
+
|
419
|
+
yaml_emitter_set_canonical(emitter, Qtrue == style ? 1 : 0);
|
420
|
+
|
421
|
+
return style;
|
422
|
+
}
|
423
|
+
|
424
|
+
/* call-seq: emitter.canonical
|
425
|
+
*
|
426
|
+
* Get the output style, canonical or not.
|
427
|
+
*/
|
428
|
+
static VALUE canonical(VALUE self)
|
429
|
+
{
|
430
|
+
yaml_emitter_t * emitter;
|
431
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
432
|
+
|
433
|
+
return (emitter->canonical == 0) ? Qfalse : Qtrue;
|
434
|
+
}
|
435
|
+
|
436
|
+
/* call-seq: emitter.indentation = level
|
437
|
+
*
|
438
|
+
* Set the indentation level to +level+.
|
439
|
+
*/
|
440
|
+
static VALUE set_indentation(VALUE self, VALUE level)
|
441
|
+
{
|
442
|
+
yaml_emitter_t * emitter;
|
443
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
444
|
+
|
445
|
+
yaml_emitter_set_indent(emitter, NUM2INT(level));
|
446
|
+
|
447
|
+
return level;
|
448
|
+
}
|
449
|
+
|
450
|
+
/* call-seq: emitter.indentation
|
451
|
+
*
|
452
|
+
* Get the indentation level.
|
453
|
+
*/
|
454
|
+
static VALUE indentation(VALUE self)
|
455
|
+
{
|
456
|
+
yaml_emitter_t * emitter;
|
457
|
+
Data_Get_Struct(self, yaml_emitter_t, emitter);
|
458
|
+
|
459
|
+
return INT2NUM(emitter->best_indent);
|
460
|
+
}
|
461
|
+
|
462
|
+
void Init_psych_emitter()
|
463
|
+
{
|
464
|
+
VALUE psych = rb_define_module("Psych");
|
465
|
+
VALUE handler = rb_define_class_under(psych, "Handler", rb_cObject);
|
466
|
+
cPsychEmitter = rb_define_class_under(psych, "Emitter", handler);
|
467
|
+
|
468
|
+
rb_define_alloc_func(cPsychEmitter, allocate);
|
469
|
+
|
470
|
+
rb_define_method(cPsychEmitter, "initialize", initialize, 1);
|
471
|
+
rb_define_method(cPsychEmitter, "start_stream", start_stream, 1);
|
472
|
+
rb_define_method(cPsychEmitter, "end_stream", end_stream, 0);
|
473
|
+
rb_define_method(cPsychEmitter, "start_document", start_document, 3);
|
474
|
+
rb_define_method(cPsychEmitter, "end_document", end_document, 1);
|
475
|
+
rb_define_method(cPsychEmitter, "scalar", scalar, 6);
|
476
|
+
rb_define_method(cPsychEmitter, "start_sequence", start_sequence, 4);
|
477
|
+
rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0);
|
478
|
+
rb_define_method(cPsychEmitter, "start_mapping", start_mapping, 4);
|
479
|
+
rb_define_method(cPsychEmitter, "end_mapping", end_mapping, 0);
|
480
|
+
rb_define_method(cPsychEmitter, "alias", alias, 1);
|
481
|
+
rb_define_method(cPsychEmitter, "canonical", canonical, 0);
|
482
|
+
rb_define_method(cPsychEmitter, "canonical=", set_canonical, 1);
|
483
|
+
rb_define_method(cPsychEmitter, "indentation", indentation, 0);
|
484
|
+
rb_define_method(cPsychEmitter, "indentation=", set_indentation, 1);
|
485
|
+
|
486
|
+
id_write = rb_intern("write");
|
487
|
+
}
|
488
|
+
/* vim: set noet sws=4 sw=4: */
|