psych 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.autotest +18 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.rdoc +3 -0
  4. data/Manifest.txt +87 -0
  5. data/README.rdoc +50 -0
  6. data/Rakefile +66 -0
  7. data/ext/psych/emitter.c +517 -0
  8. data/ext/psych/emitter.h +8 -0
  9. data/ext/psych/extconf.rb +22 -0
  10. data/ext/psych/parser.c +384 -0
  11. data/ext/psych/parser.h +6 -0
  12. data/ext/psych/psych.c +34 -0
  13. data/ext/psych/psych.h +20 -0
  14. data/ext/psych/to_ruby.c +41 -0
  15. data/ext/psych/to_ruby.h +8 -0
  16. data/ext/psych/yaml_tree.c +24 -0
  17. data/ext/psych/yaml_tree.h +8 -0
  18. data/lib/psych.rb +263 -0
  19. data/lib/psych/coder.rb +94 -0
  20. data/lib/psych/core_ext.rb +39 -0
  21. data/lib/psych/deprecated.rb +82 -0
  22. data/lib/psych/handler.rb +221 -0
  23. data/lib/psych/json.rb +6 -0
  24. data/lib/psych/json/ruby_events.rb +19 -0
  25. data/lib/psych/json/stream.rb +15 -0
  26. data/lib/psych/json/tree_builder.rb +12 -0
  27. data/lib/psych/json/yaml_events.rb +29 -0
  28. data/lib/psych/nodes.rb +77 -0
  29. data/lib/psych/nodes/alias.rb +18 -0
  30. data/lib/psych/nodes/document.rb +60 -0
  31. data/lib/psych/nodes/mapping.rb +56 -0
  32. data/lib/psych/nodes/node.rb +52 -0
  33. data/lib/psych/nodes/scalar.rb +67 -0
  34. data/lib/psych/nodes/sequence.rb +81 -0
  35. data/lib/psych/nodes/stream.rb +37 -0
  36. data/lib/psych/omap.rb +4 -0
  37. data/lib/psych/parser.rb +47 -0
  38. data/lib/psych/scalar_scanner.rb +105 -0
  39. data/lib/psych/set.rb +4 -0
  40. data/lib/psych/stream.rb +36 -0
  41. data/lib/psych/streaming.rb +22 -0
  42. data/lib/psych/tree_builder.rb +94 -0
  43. data/lib/psych/visitors.rb +6 -0
  44. data/lib/psych/visitors/depth_first.rb +26 -0
  45. data/lib/psych/visitors/emitter.rb +44 -0
  46. data/lib/psych/visitors/json_tree.rb +21 -0
  47. data/lib/psych/visitors/to_ruby.rb +267 -0
  48. data/lib/psych/visitors/visitor.rb +19 -0
  49. data/lib/psych/visitors/yaml_tree.rb +373 -0
  50. data/test/psych/helper.rb +63 -0
  51. data/test/psych/json/test_stream.rb +109 -0
  52. data/test/psych/nodes/test_enumerable.rb +43 -0
  53. data/test/psych/test_alias_and_anchor.rb +26 -0
  54. data/test/psych/test_array.rb +19 -0
  55. data/test/psych/test_boolean.rb +36 -0
  56. data/test/psych/test_class.rb +17 -0
  57. data/test/psych/test_coder.rb +184 -0
  58. data/test/psych/test_date_time.rb +17 -0
  59. data/test/psych/test_deprecated.rb +210 -0
  60. data/test/psych/test_document.rb +46 -0
  61. data/test/psych/test_emitter.rb +94 -0
  62. data/test/psych/test_encoding.rb +179 -0
  63. data/test/psych/test_engine_manager.rb +57 -0
  64. data/test/psych/test_exception.rb +39 -0
  65. data/test/psych/test_hash.rb +30 -0
  66. data/test/psych/test_json_tree.rb +65 -0
  67. data/test/psych/test_merge_keys.rb +72 -0
  68. data/test/psych/test_nil.rb +18 -0
  69. data/test/psych/test_null.rb +19 -0
  70. data/test/psych/test_object.rb +27 -0
  71. data/test/psych/test_omap.rb +68 -0
  72. data/test/psych/test_parser.rb +297 -0
  73. data/test/psych/test_psych.rb +168 -0
  74. data/test/psych/test_scalar.rb +11 -0
  75. data/test/psych/test_scalar_scanner.rb +69 -0
  76. data/test/psych/test_serialize_subclasses.rb +38 -0
  77. data/test/psych/test_set.rb +49 -0
  78. data/test/psych/test_stream.rb +49 -0
  79. data/test/psych/test_string.rb +49 -0
  80. data/test/psych/test_struct.rb +51 -0
  81. data/test/psych/test_symbol.rb +17 -0
  82. data/test/psych/test_to_yaml_properties.rb +63 -0
  83. data/test/psych/test_tree_builder.rb +79 -0
  84. data/test/psych/test_yaml.rb +1256 -0
  85. data/test/psych/visitors/test_depth_first.rb +49 -0
  86. data/test/psych/visitors/test_emitter.rb +144 -0
  87. data/test/psych/visitors/test_to_ruby.rb +325 -0
  88. data/test/psych/visitors/test_yaml_tree.rb +155 -0
  89. metadata +232 -0
data/.autotest ADDED
@@ -0,0 +1,18 @@
1
+ require "autotest/restart"
2
+ require 'rbconfig'
3
+
4
+ Autotest.add_hook :initialize do |at|
5
+ at.find_directories = ARGV unless ARGV.empty?
6
+ at.testlib = "minitest/autorun"
7
+ end
8
+
9
+ Autotest.add_hook :run_command do |at|
10
+ at.unit_diff = 'cat'
11
+ system "ruby -S rake compile"
12
+ end
13
+
14
+ Autotest.add_hook :ran_command do |at|
15
+ File.open('/tmp/autotest.txt', 'wb') { |f|
16
+ f.write(at.results.join)
17
+ }
18
+ end
data/.gemtest ADDED
File without changes
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ === 1.0.0 / 2009-09-26
2
+
3
+ * Birthday!
data/Manifest.txt ADDED
@@ -0,0 +1,87 @@
1
+ .autotest
2
+ CHANGELOG.rdoc
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ ext/psych/emitter.c
7
+ ext/psych/emitter.h
8
+ ext/psych/extconf.rb
9
+ ext/psych/parser.c
10
+ ext/psych/parser.h
11
+ ext/psych/psych.c
12
+ ext/psych/psych.h
13
+ ext/psych/to_ruby.c
14
+ ext/psych/to_ruby.h
15
+ ext/psych/yaml_tree.c
16
+ ext/psych/yaml_tree.h
17
+ lib/psych.rb
18
+ lib/psych/coder.rb
19
+ lib/psych/core_ext.rb
20
+ lib/psych/deprecated.rb
21
+ lib/psych/handler.rb
22
+ lib/psych/json.rb
23
+ lib/psych/json/ruby_events.rb
24
+ lib/psych/json/stream.rb
25
+ lib/psych/json/tree_builder.rb
26
+ lib/psych/json/yaml_events.rb
27
+ lib/psych/nodes.rb
28
+ lib/psych/nodes/alias.rb
29
+ lib/psych/nodes/document.rb
30
+ lib/psych/nodes/mapping.rb
31
+ lib/psych/nodes/node.rb
32
+ lib/psych/nodes/scalar.rb
33
+ lib/psych/nodes/sequence.rb
34
+ lib/psych/nodes/stream.rb
35
+ lib/psych/omap.rb
36
+ lib/psych/parser.rb
37
+ lib/psych/scalar_scanner.rb
38
+ lib/psych/set.rb
39
+ lib/psych/stream.rb
40
+ lib/psych/streaming.rb
41
+ lib/psych/tree_builder.rb
42
+ lib/psych/visitors.rb
43
+ lib/psych/visitors/depth_first.rb
44
+ lib/psych/visitors/emitter.rb
45
+ lib/psych/visitors/json_tree.rb
46
+ lib/psych/visitors/to_ruby.rb
47
+ lib/psych/visitors/visitor.rb
48
+ lib/psych/visitors/yaml_tree.rb
49
+ test/psych/helper.rb
50
+ test/psych/json/test_stream.rb
51
+ test/psych/nodes/test_enumerable.rb
52
+ test/psych/test_alias_and_anchor.rb
53
+ test/psych/test_array.rb
54
+ test/psych/test_boolean.rb
55
+ test/psych/test_class.rb
56
+ test/psych/test_coder.rb
57
+ test/psych/test_date_time.rb
58
+ test/psych/test_deprecated.rb
59
+ test/psych/test_document.rb
60
+ test/psych/test_emitter.rb
61
+ test/psych/test_encoding.rb
62
+ test/psych/test_engine_manager.rb
63
+ test/psych/test_exception.rb
64
+ test/psych/test_hash.rb
65
+ test/psych/test_json_tree.rb
66
+ test/psych/test_merge_keys.rb
67
+ test/psych/test_nil.rb
68
+ test/psych/test_null.rb
69
+ test/psych/test_object.rb
70
+ test/psych/test_omap.rb
71
+ test/psych/test_parser.rb
72
+ test/psych/test_psych.rb
73
+ test/psych/test_scalar.rb
74
+ test/psych/test_scalar_scanner.rb
75
+ test/psych/test_serialize_subclasses.rb
76
+ test/psych/test_set.rb
77
+ test/psych/test_stream.rb
78
+ test/psych/test_string.rb
79
+ test/psych/test_struct.rb
80
+ test/psych/test_symbol.rb
81
+ test/psych/test_to_yaml_properties.rb
82
+ test/psych/test_tree_builder.rb
83
+ test/psych/test_yaml.rb
84
+ test/psych/visitors/test_depth_first.rb
85
+ test/psych/visitors/test_emitter.rb
86
+ test/psych/visitors/test_to_ruby.rb
87
+ test/psych/visitors/test_yaml_tree.rb
data/README.rdoc ADDED
@@ -0,0 +1,50 @@
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
+ == Examples
13
+
14
+ # Load YAML in to a Ruby object
15
+ Psych.load('--- foo') # => 'foo'
16
+
17
+ # Emit YAML from a Ruby object
18
+ Psych.dump("foo") # => "--- foo\n...\n"
19
+
20
+ == Dependencies
21
+
22
+ * libyaml
23
+
24
+ == Installation
25
+
26
+ * sudo port install libyaml +universal
27
+ * sudo install_ruby.sh
28
+
29
+ == License
30
+
31
+ Copyright 2009 Aaron Patterson, et al.
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,66 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ class Hoe
7
+ remove_const :RUBY_FLAGS
8
+ RUBY_FLAGS = "-I#{%w(lib ext bin test).join(File::PATH_SEPARATOR)}"
9
+ end
10
+
11
+ gem 'rake-compiler', '>= 0.4.1'
12
+ require "rake/extensiontask"
13
+
14
+ Hoe.plugin :debugging, :doofus, :git
15
+
16
+ Hoe.spec 'psych' do
17
+ developer 'Aaron Patterson', 'aaronp@rubyforge.org'
18
+
19
+ self.extra_rdoc_files = Dir['*.rdoc']
20
+ self.history_file = 'CHANGELOG.rdoc'
21
+ self.readme_file = 'README.rdoc'
22
+ self.testlib = :minitest
23
+
24
+ extra_dev_deps << ['rake-compiler', '>= 0.4.1']
25
+
26
+ self.spec_extras = {
27
+ :extensions => ["ext/psych/extconf.rb"],
28
+ :required_ruby_version => '>= 1.9.2'
29
+ }
30
+
31
+ Rake::ExtensionTask.new "psych", spec do |ext|
32
+ ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
33
+ end
34
+ end
35
+
36
+ Hoe.add_include_dirs('.:lib/psych')
37
+
38
+ task :test => :compile
39
+
40
+ desc "merge psych in to ruby trunk"
41
+ namespace :merge do
42
+ basedir = File.expand_path File.dirname __FILE__
43
+ rubydir = File.join ENV['HOME'], 'git', 'ruby'
44
+ mergedirs = {
45
+ # From # To
46
+ [basedir, 'ext', 'psych/'] => [rubydir, 'ext', 'psych/'],
47
+ [basedir, 'lib/'] => [rubydir, 'ext', 'psych', 'lib/'],
48
+ [basedir, 'test', 'psych/'] => [rubydir, 'test', 'psych/'],
49
+ }
50
+
51
+ rsync = 'rsync -av --exclude extconf.rb --exclude lib --exclude ".*" --exclude "*.o" --exclude Makefile --exclude mkmf.log --delete'
52
+
53
+ task :to_ruby do
54
+ mergedirs.each do |from, to|
55
+ sh "#{rsync} #{File.join(*from)} #{File.join(*to)}"
56
+ end
57
+ end
58
+
59
+ task :from_ruby do
60
+ mergedirs.each do |from, to|
61
+ sh "#{rsync} #{File.join(*to)} #{File.join(*from)}"
62
+ end
63
+ end
64
+ end
65
+
66
+ # vim: syntax=ruby
@@ -0,0 +1,517 @@
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+. The level must be less than 10 and
439
+ * greater than 1.
440
+ */
441
+ static VALUE set_indentation(VALUE self, VALUE level)
442
+ {
443
+ yaml_emitter_t * emitter;
444
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
445
+
446
+ yaml_emitter_set_indent(emitter, NUM2INT(level));
447
+
448
+ return level;
449
+ }
450
+
451
+ /* call-seq: emitter.indentation
452
+ *
453
+ * Get the indentation level.
454
+ */
455
+ static VALUE indentation(VALUE self)
456
+ {
457
+ yaml_emitter_t * emitter;
458
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
459
+
460
+ return INT2NUM(emitter->best_indent);
461
+ }
462
+
463
+ /* call-seq: emitter.line_width
464
+ *
465
+ * Get the preferred line width.
466
+ */
467
+ static VALUE line_width(VALUE self)
468
+ {
469
+ yaml_emitter_t * emitter;
470
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
471
+
472
+ return INT2NUM(emitter->best_width);
473
+ }
474
+
475
+ /* call-seq: emitter.line_width = width
476
+ *
477
+ * Set the preferred line with to +width+.
478
+ */
479
+ static VALUE set_line_width(VALUE self, VALUE width)
480
+ {
481
+ yaml_emitter_t * emitter;
482
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
483
+
484
+ yaml_emitter_set_width(emitter, NUM2INT(width));
485
+
486
+ return width;
487
+ }
488
+
489
+ void Init_psych_emitter()
490
+ {
491
+ VALUE psych = rb_define_module("Psych");
492
+ VALUE handler = rb_define_class_under(psych, "Handler", rb_cObject);
493
+ cPsychEmitter = rb_define_class_under(psych, "Emitter", handler);
494
+
495
+ rb_define_alloc_func(cPsychEmitter, allocate);
496
+
497
+ rb_define_method(cPsychEmitter, "initialize", initialize, 1);
498
+ rb_define_method(cPsychEmitter, "start_stream", start_stream, 1);
499
+ rb_define_method(cPsychEmitter, "end_stream", end_stream, 0);
500
+ rb_define_method(cPsychEmitter, "start_document", start_document, 3);
501
+ rb_define_method(cPsychEmitter, "end_document", end_document, 1);
502
+ rb_define_method(cPsychEmitter, "scalar", scalar, 6);
503
+ rb_define_method(cPsychEmitter, "start_sequence", start_sequence, 4);
504
+ rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0);
505
+ rb_define_method(cPsychEmitter, "start_mapping", start_mapping, 4);
506
+ rb_define_method(cPsychEmitter, "end_mapping", end_mapping, 0);
507
+ rb_define_method(cPsychEmitter, "alias", alias, 1);
508
+ rb_define_method(cPsychEmitter, "canonical", canonical, 0);
509
+ rb_define_method(cPsychEmitter, "canonical=", set_canonical, 1);
510
+ rb_define_method(cPsychEmitter, "indentation", indentation, 0);
511
+ rb_define_method(cPsychEmitter, "indentation=", set_indentation, 1);
512
+ rb_define_method(cPsychEmitter, "line_width", line_width, 0);
513
+ rb_define_method(cPsychEmitter, "line_width=", set_line_width, 1);
514
+
515
+ id_write = rb_intern("write");
516
+ }
517
+ /* vim: set noet sws=4 sw=4: */