psych-shopifork 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +15 -0
  2. data/.autotest +18 -0
  3. data/.gemtest +0 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.rdoc +414 -0
  6. data/Manifest.txt +113 -0
  7. data/README.rdoc +71 -0
  8. data/Rakefile +72 -0
  9. data/ext/psych/depend +3 -0
  10. data/ext/psych/extconf.rb +36 -0
  11. data/ext/psych/psych.c +34 -0
  12. data/ext/psych/psych.h +20 -0
  13. data/ext/psych/psych_emitter.c +538 -0
  14. data/ext/psych/psych_emitter.h +8 -0
  15. data/ext/psych/psych_parser.c +579 -0
  16. data/ext/psych/psych_parser.h +6 -0
  17. data/ext/psych/psych_to_ruby.c +43 -0
  18. data/ext/psych/psych_to_ruby.h +8 -0
  19. data/ext/psych/psych_yaml_tree.c +24 -0
  20. data/ext/psych/psych_yaml_tree.h +8 -0
  21. data/ext/psych/yaml/LICENSE +19 -0
  22. data/ext/psych/yaml/api.c +1392 -0
  23. data/ext/psych/yaml/config.h +11 -0
  24. data/ext/psych/yaml/dumper.c +394 -0
  25. data/ext/psych/yaml/emitter.c +2335 -0
  26. data/ext/psych/yaml/loader.c +432 -0
  27. data/ext/psych/yaml/parser.c +1374 -0
  28. data/ext/psych/yaml/reader.c +465 -0
  29. data/ext/psych/yaml/scanner.c +3570 -0
  30. data/ext/psych/yaml/writer.c +141 -0
  31. data/ext/psych/yaml/yaml.h +1971 -0
  32. data/ext/psych/yaml/yaml_private.h +643 -0
  33. data/lib/psych.rb +497 -0
  34. data/lib/psych/class_loader.rb +101 -0
  35. data/lib/psych/coder.rb +94 -0
  36. data/lib/psych/core_ext.rb +35 -0
  37. data/lib/psych/deprecated.rb +85 -0
  38. data/lib/psych/exception.rb +13 -0
  39. data/lib/psych/handler.rb +249 -0
  40. data/lib/psych/handlers/document_stream.rb +22 -0
  41. data/lib/psych/handlers/recorder.rb +39 -0
  42. data/lib/psych/json/ruby_events.rb +19 -0
  43. data/lib/psych/json/stream.rb +16 -0
  44. data/lib/psych/json/tree_builder.rb +12 -0
  45. data/lib/psych/json/yaml_events.rb +29 -0
  46. data/lib/psych/nodes.rb +77 -0
  47. data/lib/psych/nodes/alias.rb +18 -0
  48. data/lib/psych/nodes/document.rb +60 -0
  49. data/lib/psych/nodes/mapping.rb +56 -0
  50. data/lib/psych/nodes/node.rb +55 -0
  51. data/lib/psych/nodes/scalar.rb +67 -0
  52. data/lib/psych/nodes/sequence.rb +81 -0
  53. data/lib/psych/nodes/stream.rb +37 -0
  54. data/lib/psych/omap.rb +4 -0
  55. data/lib/psych/parser.rb +51 -0
  56. data/lib/psych/scalar_scanner.rb +149 -0
  57. data/lib/psych/set.rb +4 -0
  58. data/lib/psych/stream.rb +37 -0
  59. data/lib/psych/streaming.rb +27 -0
  60. data/lib/psych/syntax_error.rb +21 -0
  61. data/lib/psych/tree_builder.rb +96 -0
  62. data/lib/psych/visitors.rb +6 -0
  63. data/lib/psych/visitors/depth_first.rb +26 -0
  64. data/lib/psych/visitors/emitter.rb +51 -0
  65. data/lib/psych/visitors/json_tree.rb +24 -0
  66. data/lib/psych/visitors/to_ruby.rb +372 -0
  67. data/lib/psych/visitors/visitor.rb +19 -0
  68. data/lib/psych/visitors/yaml_tree.rb +507 -0
  69. data/lib/psych/y.rb +9 -0
  70. data/test/psych/handlers/test_recorder.rb +25 -0
  71. data/test/psych/helper.rb +114 -0
  72. data/test/psych/json/test_stream.rb +109 -0
  73. data/test/psych/nodes/test_enumerable.rb +43 -0
  74. data/test/psych/test_alias_and_anchor.rb +96 -0
  75. data/test/psych/test_array.rb +57 -0
  76. data/test/psych/test_boolean.rb +36 -0
  77. data/test/psych/test_class.rb +36 -0
  78. data/test/psych/test_coder.rb +184 -0
  79. data/test/psych/test_date_time.rb +25 -0
  80. data/test/psych/test_deprecated.rb +214 -0
  81. data/test/psych/test_document.rb +46 -0
  82. data/test/psych/test_emitter.rb +94 -0
  83. data/test/psych/test_encoding.rb +254 -0
  84. data/test/psych/test_engine_manager.rb +47 -0
  85. data/test/psych/test_exception.rb +151 -0
  86. data/test/psych/test_hash.rb +44 -0
  87. data/test/psych/test_json_tree.rb +65 -0
  88. data/test/psych/test_merge_keys.rb +132 -0
  89. data/test/psych/test_nil.rb +18 -0
  90. data/test/psych/test_null.rb +19 -0
  91. data/test/psych/test_numeric.rb +45 -0
  92. data/test/psych/test_object.rb +44 -0
  93. data/test/psych/test_object_references.rb +67 -0
  94. data/test/psych/test_omap.rb +75 -0
  95. data/test/psych/test_parser.rb +339 -0
  96. data/test/psych/test_psych.rb +168 -0
  97. data/test/psych/test_safe_load.rb +97 -0
  98. data/test/psych/test_scalar.rb +11 -0
  99. data/test/psych/test_scalar_scanner.rb +106 -0
  100. data/test/psych/test_serialize_subclasses.rb +38 -0
  101. data/test/psych/test_set.rb +49 -0
  102. data/test/psych/test_stream.rb +93 -0
  103. data/test/psych/test_string.rb +153 -0
  104. data/test/psych/test_struct.rb +49 -0
  105. data/test/psych/test_symbol.rb +17 -0
  106. data/test/psych/test_tainted.rb +130 -0
  107. data/test/psych/test_to_yaml_properties.rb +63 -0
  108. data/test/psych/test_tree_builder.rb +79 -0
  109. data/test/psych/test_yaml.rb +1289 -0
  110. data/test/psych/test_yamldbm.rb +197 -0
  111. data/test/psych/test_yamlstore.rb +87 -0
  112. data/test/psych/visitors/test_depth_first.rb +49 -0
  113. data/test/psych/visitors/test_emitter.rb +144 -0
  114. data/test/psych/visitors/test_to_ruby.rb +326 -0
  115. data/test/psych/visitors/test_yaml_tree.rb +173 -0
  116. metadata +257 -0
data/Manifest.txt ADDED
@@ -0,0 +1,113 @@
1
+ .autotest
2
+ .travis.yml
3
+ CHANGELOG.rdoc
4
+ Manifest.txt
5
+ README.rdoc
6
+ Rakefile
7
+ ext/psych/depend
8
+ ext/psych/extconf.rb
9
+ ext/psych/psych.c
10
+ ext/psych/psych.h
11
+ ext/psych/psych_emitter.c
12
+ ext/psych/psych_emitter.h
13
+ ext/psych/psych_parser.c
14
+ ext/psych/psych_parser.h
15
+ ext/psych/psych_to_ruby.c
16
+ ext/psych/psych_to_ruby.h
17
+ ext/psych/psych_yaml_tree.c
18
+ ext/psych/psych_yaml_tree.h
19
+ ext/psych/yaml/LICENSE
20
+ ext/psych/yaml/api.c
21
+ ext/psych/yaml/config.h
22
+ ext/psych/yaml/dumper.c
23
+ ext/psych/yaml/emitter.c
24
+ ext/psych/yaml/loader.c
25
+ ext/psych/yaml/parser.c
26
+ ext/psych/yaml/reader.c
27
+ ext/psych/yaml/scanner.c
28
+ ext/psych/yaml/writer.c
29
+ ext/psych/yaml/yaml.h
30
+ ext/psych/yaml/yaml_private.h
31
+ lib/psych.rb
32
+ lib/psych/class_loader.rb
33
+ lib/psych/coder.rb
34
+ lib/psych/core_ext.rb
35
+ lib/psych/deprecated.rb
36
+ lib/psych/exception.rb
37
+ lib/psych/handler.rb
38
+ lib/psych/handlers/document_stream.rb
39
+ lib/psych/handlers/recorder.rb
40
+ lib/psych/json/ruby_events.rb
41
+ lib/psych/json/stream.rb
42
+ lib/psych/json/tree_builder.rb
43
+ lib/psych/json/yaml_events.rb
44
+ lib/psych/nodes.rb
45
+ lib/psych/nodes/alias.rb
46
+ lib/psych/nodes/document.rb
47
+ lib/psych/nodes/mapping.rb
48
+ lib/psych/nodes/node.rb
49
+ lib/psych/nodes/scalar.rb
50
+ lib/psych/nodes/sequence.rb
51
+ lib/psych/nodes/stream.rb
52
+ lib/psych/omap.rb
53
+ lib/psych/parser.rb
54
+ lib/psych/scalar_scanner.rb
55
+ lib/psych/set.rb
56
+ lib/psych/stream.rb
57
+ lib/psych/streaming.rb
58
+ lib/psych/syntax_error.rb
59
+ lib/psych/tree_builder.rb
60
+ lib/psych/visitors.rb
61
+ lib/psych/visitors/depth_first.rb
62
+ lib/psych/visitors/emitter.rb
63
+ lib/psych/visitors/json_tree.rb
64
+ lib/psych/visitors/to_ruby.rb
65
+ lib/psych/visitors/visitor.rb
66
+ lib/psych/visitors/yaml_tree.rb
67
+ lib/psych/y.rb
68
+ test/psych/handlers/test_recorder.rb
69
+ test/psych/helper.rb
70
+ test/psych/json/test_stream.rb
71
+ test/psych/nodes/test_enumerable.rb
72
+ test/psych/test_alias_and_anchor.rb
73
+ test/psych/test_array.rb
74
+ test/psych/test_boolean.rb
75
+ test/psych/test_class.rb
76
+ test/psych/test_coder.rb
77
+ test/psych/test_date_time.rb
78
+ test/psych/test_deprecated.rb
79
+ test/psych/test_document.rb
80
+ test/psych/test_emitter.rb
81
+ test/psych/test_encoding.rb
82
+ test/psych/test_engine_manager.rb
83
+ test/psych/test_exception.rb
84
+ test/psych/test_hash.rb
85
+ test/psych/test_json_tree.rb
86
+ test/psych/test_merge_keys.rb
87
+ test/psych/test_nil.rb
88
+ test/psych/test_null.rb
89
+ test/psych/test_numeric.rb
90
+ test/psych/test_object.rb
91
+ test/psych/test_object_references.rb
92
+ test/psych/test_omap.rb
93
+ test/psych/test_parser.rb
94
+ test/psych/test_psych.rb
95
+ test/psych/test_safe_load.rb
96
+ test/psych/test_scalar.rb
97
+ test/psych/test_scalar_scanner.rb
98
+ test/psych/test_serialize_subclasses.rb
99
+ test/psych/test_set.rb
100
+ test/psych/test_stream.rb
101
+ test/psych/test_string.rb
102
+ test/psych/test_struct.rb
103
+ test/psych/test_symbol.rb
104
+ test/psych/test_tainted.rb
105
+ test/psych/test_to_yaml_properties.rb
106
+ test/psych/test_tree_builder.rb
107
+ test/psych/test_yaml.rb
108
+ test/psych/test_yamldbm.rb
109
+ test/psych/test_yamlstore.rb
110
+ test/psych/visitors/test_depth_first.rb
111
+ test/psych/visitors/test_emitter.rb
112
+ test/psych/visitors/test_to_ruby.rb
113
+ test/psych/visitors/test_yaml_tree.rb
data/README.rdoc ADDED
@@ -0,0 +1,71 @@
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://pyyaml.org/wiki/LibYAML]
8
+ for its 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
+ Psych has been included with MRI since 1.9.2,
27
+ and is the default YAML parser in 1.9.3.
28
+
29
+ If you want a newer gem release of Psych, you can use rubygems:
30
+
31
+ gem install psych
32
+
33
+ In order to use the gem release in your app,
34
+ and not the stdlib version, you'll need the following:
35
+
36
+ gem 'psych'
37
+ require 'psych'
38
+
39
+ Or if you use Bundler add this to your +Gemfile+:
40
+
41
+ gem 'psych'
42
+
43
+ JRuby ships with a pure Java implementation of Psych.
44
+
45
+ If you're on Rubinius, Psych is available in 1.9 mode, please refer to the
46
+ Language Modes section of the {Rubinius
47
+ README}[https://github.com/rubinius/rubinius#readme] for more information on
48
+ building and 1.9 mode.
49
+
50
+ == License
51
+
52
+ Copyright 2009 Aaron Patterson, et al.
53
+
54
+ Permission is hereby granted, free of charge, to any person obtaining
55
+ a copy of this software and associated documentation files (the
56
+ 'Software'), to deal in the Software without restriction, including
57
+ without limitation the rights to use, copy, modify, merge, publish,
58
+ distribute, sublicense, and/or sell copies of the Software, and to
59
+ permit persons to whom the Software is furnished to do so, subject to
60
+ the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be
63
+ included in all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
66
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
68
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
69
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
70
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
71
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,72 @@
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 :doofus, :git, :gemspec, :isolate
15
+
16
+ $hoe = Hoe.spec 'psych-shopifork' do
17
+ license 'MIT'
18
+ developer 'Aaron Patterson', 'aaron@tenderlovemaking.com'
19
+
20
+ self.extra_rdoc_files = Dir['*.rdoc']
21
+ self.history_file = 'CHANGELOG.rdoc'
22
+ self.readme_file = 'README.rdoc'
23
+ self.testlib = :minitest
24
+
25
+ extra_dev_deps << ['rake-compiler', '>= 0.4.1']
26
+
27
+ self.spec_extras = {
28
+ :extensions => ["ext/psych/extconf.rb"],
29
+ :required_ruby_version => '>= 1.9.2'
30
+ }
31
+
32
+ Rake::ExtensionTask.new "psych", spec do |ext|
33
+ ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
34
+ end
35
+ end
36
+
37
+ Hoe.add_include_dirs('.:lib/psych')
38
+
39
+ task :test => :compile
40
+
41
+ task :hack_spec do
42
+ $hoe.spec.extra_rdoc_files.clear
43
+ end
44
+ task 'core:spec' => [:hack_spec, 'gem:spec']
45
+
46
+ desc "merge psych in to ruby trunk"
47
+ namespace :merge do
48
+ basedir = File.expand_path File.dirname __FILE__
49
+ rubydir = File.join ENV['HOME'], 'git', 'ruby'
50
+ mergedirs = {
51
+ # From # To
52
+ [basedir, 'ext', 'psych/'] => [rubydir, 'ext', 'psych/'],
53
+ [basedir, 'lib/'] => [rubydir, 'ext', 'psych', 'lib/'],
54
+ [basedir, 'test', 'psych/'] => [rubydir, 'test', 'psych/'],
55
+ }
56
+
57
+ rsync = 'rsync -av --exclude lib --exclude ".*" --exclude "*.o" --exclude Makefile --exclude mkmf.log --delete'
58
+
59
+ task :to_ruby do
60
+ mergedirs.each do |from, to|
61
+ sh "#{rsync} #{File.join(*from)} #{File.join(*to)}"
62
+ end
63
+ end
64
+
65
+ task :from_ruby do
66
+ mergedirs.each do |from, to|
67
+ sh "#{rsync} #{File.join(*to)} #{File.join(*from)}"
68
+ end
69
+ end
70
+ end
71
+
72
+ # vim: syntax=ruby
data/ext/psych/depend ADDED
@@ -0,0 +1,3 @@
1
+ $(OBJS): $(HDRS) $(ruby_headers) \
2
+ $(hdrdir)/ruby/encoding.h \
3
+ $(hdrdir)/ruby/oniguruma.h
@@ -0,0 +1,36 @@
1
+ # -*- coding: us-ascii -*-
2
+ require 'mkmf'
3
+ require 'fileutils'
4
+
5
+ # :stopdoc:
6
+
7
+ dir_config 'libyaml'
8
+
9
+ # Embed libyaml since fixed it.
10
+
11
+ $VPATH << "$(srcdir)/yaml"
12
+ $INCFLAGS << " -I$(srcdir)/yaml"
13
+
14
+ $srcs = Dir.glob("#{$srcdir}/{,yaml/}*.c").map {|n| File.basename(n)}
15
+
16
+ if have_macro("_WIN32")
17
+ $CPPFLAGS << " -DYAML_DECLARE_STATIC -DHAVE_CONFIG_H"
18
+ end
19
+
20
+ have_header 'dlfcn.h'
21
+ have_header 'inttypes.h'
22
+ have_header 'memory.h'
23
+ have_header 'stdint.h'
24
+ have_header 'stdlib.h'
25
+ have_header 'strings.h'
26
+ have_header 'string.h'
27
+ have_header 'sys/stat.h'
28
+ have_header 'sys/types.h'
29
+ have_header 'unistd.h'
30
+
31
+ find_header 'yaml.h'
32
+ have_header 'config.h'
33
+
34
+ create_makefile 'psych'
35
+
36
+ # :startdoc:
data/ext/psych/psych.c ADDED
@@ -0,0 +1,34 @@
1
+ #include <psych.h>
2
+
3
+ /* call-seq: Psych.libyaml_version
4
+ *
5
+ * Returns the version of libyaml being used
6
+ */
7
+ static VALUE libyaml_version(VALUE module)
8
+ {
9
+ int major, minor, patch;
10
+ VALUE list[3];
11
+
12
+ yaml_get_version(&major, &minor, &patch);
13
+
14
+ list[0] = INT2NUM((long)major);
15
+ list[1] = INT2NUM((long)minor);
16
+ list[2] = INT2NUM((long)patch);
17
+
18
+ return rb_ary_new4((long)3, list);
19
+ }
20
+
21
+ VALUE mPsych;
22
+
23
+ void Init_psych()
24
+ {
25
+ mPsych = rb_define_module("Psych");
26
+
27
+ rb_define_singleton_method(mPsych, "libyaml_version", libyaml_version, 0);
28
+
29
+ Init_psych_parser();
30
+ Init_psych_emitter();
31
+ Init_psych_to_ruby();
32
+ Init_psych_yaml_tree();
33
+ }
34
+ /* vim: set noet sws=4 sw=4: */
data/ext/psych/psych.h ADDED
@@ -0,0 +1,20 @@
1
+ #ifndef PSYCH_H
2
+ #define PSYCH_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #ifdef HAVE_RUBY_ENCODING_H
7
+ #include <ruby/encoding.h>
8
+ #endif
9
+
10
+ #include <yaml.h>
11
+
12
+ #include <psych_parser.h>
13
+ #include <psych_emitter.h>
14
+ #include <psych_to_ruby.h>
15
+ #include <psych_yaml_tree.h>
16
+
17
+ extern VALUE mPsych;
18
+
19
+
20
+ #endif
@@ -0,0 +1,538 @@
1
+ #include <psych.h>
2
+
3
+ VALUE cPsychEmitter;
4
+ static ID id_write;
5
+ static ID id_line_width;
6
+ static ID id_indentation;
7
+ static ID id_canonical;
8
+
9
+ static void emit(yaml_emitter_t * emitter, yaml_event_t * event)
10
+ {
11
+ if(!yaml_emitter_emit(emitter, event))
12
+ rb_raise(rb_eRuntimeError, "%s", emitter->problem);
13
+ }
14
+
15
+ static int writer(void *ctx, unsigned char *buffer, size_t size)
16
+ {
17
+ VALUE io = (VALUE)ctx;
18
+ VALUE str = rb_str_new((const char *)buffer, (long)size);
19
+ VALUE wrote = rb_funcall(io, id_write, 1, str);
20
+ return (int)NUM2INT(wrote);
21
+ }
22
+
23
+ static void dealloc(void * ptr)
24
+ {
25
+ yaml_emitter_t * emitter;
26
+
27
+ emitter = (yaml_emitter_t *)ptr;
28
+ yaml_emitter_delete(emitter);
29
+ xfree(emitter);
30
+ }
31
+
32
+ static VALUE allocate(VALUE klass)
33
+ {
34
+ yaml_emitter_t * emitter;
35
+
36
+ emitter = xmalloc(sizeof(yaml_emitter_t));
37
+
38
+ yaml_emitter_initialize(emitter);
39
+ yaml_emitter_set_unicode(emitter, 1);
40
+ yaml_emitter_set_indent(emitter, 2);
41
+
42
+ return Data_Wrap_Struct(klass, 0, dealloc, emitter);
43
+ }
44
+
45
+ /* call-seq: Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS)
46
+ *
47
+ * Create a new Psych::Emitter that writes to +io+.
48
+ */
49
+ static VALUE initialize(int argc, VALUE *argv, VALUE self)
50
+ {
51
+ yaml_emitter_t * emitter;
52
+ VALUE io, options;
53
+ VALUE line_width;
54
+ VALUE indent;
55
+ VALUE canonical;
56
+
57
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
58
+
59
+ if (rb_scan_args(argc, argv, "11", &io, &options) == 2) {
60
+ line_width = rb_funcall(options, id_line_width, 0);
61
+ indent = rb_funcall(options, id_indentation, 0);
62
+ canonical = rb_funcall(options, id_canonical, 0);
63
+
64
+ yaml_emitter_set_width(emitter, NUM2INT(line_width));
65
+ yaml_emitter_set_indent(emitter, NUM2INT(indent));
66
+ yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0);
67
+ }
68
+
69
+ yaml_emitter_set_output(emitter, writer, (void *)io);
70
+
71
+ return self;
72
+ }
73
+
74
+ /* call-seq: emitter.start_stream(encoding)
75
+ *
76
+ * Start a stream emission with +encoding+
77
+ *
78
+ * See Psych::Handler#start_stream
79
+ */
80
+ static VALUE start_stream(VALUE self, VALUE encoding)
81
+ {
82
+ yaml_emitter_t * emitter;
83
+ yaml_event_t event;
84
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
85
+ Check_Type(encoding, T_FIXNUM);
86
+
87
+ yaml_stream_start_event_initialize(&event, (yaml_encoding_t)NUM2INT(encoding));
88
+
89
+ emit(emitter, &event);
90
+
91
+ return self;
92
+ }
93
+
94
+ /* call-seq: emitter.end_stream
95
+ *
96
+ * End a stream emission
97
+ *
98
+ * See Psych::Handler#end_stream
99
+ */
100
+ static VALUE end_stream(VALUE self)
101
+ {
102
+ yaml_emitter_t * emitter;
103
+ yaml_event_t event;
104
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
105
+
106
+ yaml_stream_end_event_initialize(&event);
107
+
108
+ emit(emitter, &event);
109
+
110
+ return self;
111
+ }
112
+
113
+ /* call-seq: emitter.start_document(version, tags, implicit)
114
+ *
115
+ * Start a document emission with YAML +version+, +tags+, and an +implicit+
116
+ * start.
117
+ *
118
+ * See Psych::Handler#start_document
119
+ */
120
+ static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
121
+ {
122
+ yaml_emitter_t * emitter;
123
+ yaml_tag_directive_t * head = NULL;
124
+ yaml_tag_directive_t * tail = NULL;
125
+ yaml_event_t event;
126
+ yaml_version_directive_t version_directive;
127
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
128
+
129
+
130
+ Check_Type(version, T_ARRAY);
131
+
132
+ if(RARRAY_LEN(version) > 0) {
133
+ VALUE major = rb_ary_entry(version, (long)0);
134
+ VALUE minor = rb_ary_entry(version, (long)1);
135
+
136
+ version_directive.major = NUM2INT(major);
137
+ version_directive.minor = NUM2INT(minor);
138
+ }
139
+
140
+ if(RTEST(tags)) {
141
+ int i = 0;
142
+ #ifdef HAVE_RUBY_ENCODING_H
143
+ rb_encoding * encoding = rb_utf8_encoding();
144
+ #endif
145
+
146
+ Check_Type(tags, T_ARRAY);
147
+
148
+ head = xcalloc((size_t)RARRAY_LEN(tags), sizeof(yaml_tag_directive_t));
149
+ tail = head;
150
+
151
+ for(i = 0; i < RARRAY_LEN(tags); i++) {
152
+ VALUE tuple = RARRAY_PTR(tags)[i];
153
+ VALUE name;
154
+ VALUE value;
155
+
156
+ Check_Type(tuple, T_ARRAY);
157
+
158
+ if(RARRAY_LEN(tuple) < 2) {
159
+ xfree(head);
160
+ rb_raise(rb_eRuntimeError, "tag tuple must be of length 2");
161
+ }
162
+ name = RARRAY_PTR(tuple)[0];
163
+ value = RARRAY_PTR(tuple)[1];
164
+ #ifdef HAVE_RUBY_ENCODING_H
165
+ name = rb_str_export_to_enc(name, encoding);
166
+ value = rb_str_export_to_enc(value, encoding);
167
+ #endif
168
+
169
+ tail->handle = (yaml_char_t *)StringValuePtr(name);
170
+ tail->prefix = (yaml_char_t *)StringValuePtr(value);
171
+
172
+ tail++;
173
+ }
174
+ }
175
+
176
+ yaml_document_start_event_initialize(
177
+ &event,
178
+ (RARRAY_LEN(version) > 0) ? &version_directive : NULL,
179
+ head,
180
+ tail,
181
+ imp ? 1 : 0
182
+ );
183
+
184
+ emit(emitter, &event);
185
+
186
+ if(head) xfree(head);
187
+
188
+ return self;
189
+ }
190
+
191
+ /* call-seq: emitter.end_document(implicit)
192
+ *
193
+ * End a document emission with an +implicit+ ending.
194
+ *
195
+ * See Psych::Handler#end_document
196
+ */
197
+ static VALUE end_document(VALUE self, VALUE imp)
198
+ {
199
+ yaml_emitter_t * emitter;
200
+ yaml_event_t event;
201
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
202
+
203
+ yaml_document_end_event_initialize(&event, imp ? 1 : 0);
204
+
205
+ emit(emitter, &event);
206
+
207
+ return self;
208
+ }
209
+
210
+ /* call-seq: emitter.scalar(value, anchor, tag, plain, quoted, style)
211
+ *
212
+ * Emit a scalar with +value+, +anchor+, +tag+, and a +plain+ or +quoted+
213
+ * string type with +style+.
214
+ *
215
+ * See Psych::Handler#scalar
216
+ */
217
+ static VALUE scalar(
218
+ VALUE self,
219
+ VALUE value,
220
+ VALUE anchor,
221
+ VALUE tag,
222
+ VALUE plain,
223
+ VALUE quoted,
224
+ VALUE style
225
+ ) {
226
+ yaml_emitter_t * emitter;
227
+ yaml_event_t event;
228
+ #ifdef HAVE_RUBY_ENCODING_H
229
+ rb_encoding *encoding;
230
+ #endif
231
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
232
+
233
+ Check_Type(value, T_STRING);
234
+
235
+ #ifdef HAVE_RUBY_ENCODING_H
236
+ encoding = rb_utf8_encoding();
237
+
238
+ value = rb_str_export_to_enc(value, encoding);
239
+
240
+ if(!NIL_P(anchor)) {
241
+ Check_Type(anchor, T_STRING);
242
+ anchor = rb_str_export_to_enc(anchor, encoding);
243
+ }
244
+
245
+ if(!NIL_P(tag)) {
246
+ Check_Type(tag, T_STRING);
247
+ tag = rb_str_export_to_enc(tag, encoding);
248
+ }
249
+ #endif
250
+
251
+ yaml_scalar_event_initialize(
252
+ &event,
253
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
254
+ (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
255
+ (yaml_char_t*)StringValuePtr(value),
256
+ (int)RSTRING_LEN(value),
257
+ plain ? 1 : 0,
258
+ quoted ? 1 : 0,
259
+ (yaml_scalar_style_t)NUM2INT(style)
260
+ );
261
+
262
+ emit(emitter, &event);
263
+
264
+ return self;
265
+ }
266
+
267
+ /* call-seq: emitter.start_sequence(anchor, tag, implicit, style)
268
+ *
269
+ * Start emitting a sequence with +anchor+, a +tag+, +implicit+ sequence
270
+ * start and end, along with +style+.
271
+ *
272
+ * See Psych::Handler#start_sequence
273
+ */
274
+ static VALUE start_sequence(
275
+ VALUE self,
276
+ VALUE anchor,
277
+ VALUE tag,
278
+ VALUE implicit,
279
+ VALUE style
280
+ ) {
281
+ yaml_emitter_t * emitter;
282
+ yaml_event_t event;
283
+
284
+ #ifdef HAVE_RUBY_ENCODING_H
285
+ rb_encoding * encoding = rb_utf8_encoding();
286
+
287
+ if(!NIL_P(anchor)) {
288
+ Check_Type(anchor, T_STRING);
289
+ anchor = rb_str_export_to_enc(anchor, encoding);
290
+ }
291
+
292
+ if(!NIL_P(tag)) {
293
+ Check_Type(tag, T_STRING);
294
+ tag = rb_str_export_to_enc(tag, encoding);
295
+ }
296
+ #endif
297
+
298
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
299
+
300
+ yaml_sequence_start_event_initialize(
301
+ &event,
302
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
303
+ (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
304
+ implicit ? 1 : 0,
305
+ (yaml_sequence_style_t)NUM2INT(style)
306
+ );
307
+
308
+ emit(emitter, &event);
309
+
310
+ return self;
311
+ }
312
+
313
+ /* call-seq: emitter.end_sequence
314
+ *
315
+ * End sequence emission.
316
+ *
317
+ * See Psych::Handler#end_sequence
318
+ */
319
+ static VALUE end_sequence(VALUE self)
320
+ {
321
+ yaml_emitter_t * emitter;
322
+ yaml_event_t event;
323
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
324
+
325
+ yaml_sequence_end_event_initialize(&event);
326
+
327
+ emit(emitter, &event);
328
+
329
+ return self;
330
+ }
331
+
332
+ /* call-seq: emitter.start_mapping(anchor, tag, implicit, style)
333
+ *
334
+ * Start emitting a YAML map with +anchor+, +tag+, an +implicit+ start
335
+ * and end, and +style+.
336
+ *
337
+ * See Psych::Handler#start_mapping
338
+ */
339
+ static VALUE start_mapping(
340
+ VALUE self,
341
+ VALUE anchor,
342
+ VALUE tag,
343
+ VALUE implicit,
344
+ VALUE style
345
+ ) {
346
+ yaml_emitter_t * emitter;
347
+ yaml_event_t event;
348
+ #ifdef HAVE_RUBY_ENCODING_H
349
+ rb_encoding *encoding;
350
+ #endif
351
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
352
+
353
+ #ifdef HAVE_RUBY_ENCODING_H
354
+ encoding = rb_utf8_encoding();
355
+
356
+ if(!NIL_P(anchor)) {
357
+ Check_Type(anchor, T_STRING);
358
+ anchor = rb_str_export_to_enc(anchor, encoding);
359
+ }
360
+
361
+ if(!NIL_P(tag)) {
362
+ Check_Type(tag, T_STRING);
363
+ tag = rb_str_export_to_enc(tag, encoding);
364
+ }
365
+ #endif
366
+
367
+ yaml_mapping_start_event_initialize(
368
+ &event,
369
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
370
+ (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
371
+ implicit ? 1 : 0,
372
+ (yaml_mapping_style_t)NUM2INT(style)
373
+ );
374
+
375
+ emit(emitter, &event);
376
+
377
+ return self;
378
+ }
379
+
380
+ /* call-seq: emitter.end_mapping
381
+ *
382
+ * Emit the end of a mapping.
383
+ *
384
+ * See Psych::Handler#end_mapping
385
+ */
386
+ static VALUE end_mapping(VALUE self)
387
+ {
388
+ yaml_emitter_t * emitter;
389
+ yaml_event_t event;
390
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
391
+
392
+ yaml_mapping_end_event_initialize(&event);
393
+
394
+ emit(emitter, &event);
395
+
396
+ return self;
397
+ }
398
+
399
+ /* call-seq: emitter.alias(anchor)
400
+ *
401
+ * Emit an alias with +anchor+.
402
+ *
403
+ * See Psych::Handler#alias
404
+ */
405
+ static VALUE alias(VALUE self, VALUE anchor)
406
+ {
407
+ yaml_emitter_t * emitter;
408
+ yaml_event_t event;
409
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
410
+
411
+ #ifdef HAVE_RUBY_ENCODING_H
412
+ if(!NIL_P(anchor)) {
413
+ Check_Type(anchor, T_STRING);
414
+ anchor = rb_str_export_to_enc(anchor, rb_utf8_encoding());
415
+ }
416
+ #endif
417
+
418
+ yaml_alias_event_initialize(
419
+ &event,
420
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor))
421
+ );
422
+
423
+ emit(emitter, &event);
424
+
425
+ return self;
426
+ }
427
+
428
+ /* call-seq: emitter.canonical = true
429
+ *
430
+ * Set the output style to canonical, or not.
431
+ */
432
+ static VALUE set_canonical(VALUE self, VALUE style)
433
+ {
434
+ yaml_emitter_t * emitter;
435
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
436
+
437
+ yaml_emitter_set_canonical(emitter, Qtrue == style ? 1 : 0);
438
+
439
+ return style;
440
+ }
441
+
442
+ /* call-seq: emitter.canonical
443
+ *
444
+ * Get the output style, canonical or not.
445
+ */
446
+ static VALUE canonical(VALUE self)
447
+ {
448
+ yaml_emitter_t * emitter;
449
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
450
+
451
+ return (emitter->canonical == 0) ? Qfalse : Qtrue;
452
+ }
453
+
454
+ /* call-seq: emitter.indentation = level
455
+ *
456
+ * Set the indentation level to +level+. The level must be less than 10 and
457
+ * greater than 1.
458
+ */
459
+ static VALUE set_indentation(VALUE self, VALUE level)
460
+ {
461
+ yaml_emitter_t * emitter;
462
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
463
+
464
+ yaml_emitter_set_indent(emitter, NUM2INT(level));
465
+
466
+ return level;
467
+ }
468
+
469
+ /* call-seq: emitter.indentation
470
+ *
471
+ * Get the indentation level.
472
+ */
473
+ static VALUE indentation(VALUE self)
474
+ {
475
+ yaml_emitter_t * emitter;
476
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
477
+
478
+ return INT2NUM(emitter->best_indent);
479
+ }
480
+
481
+ /* call-seq: emitter.line_width
482
+ *
483
+ * Get the preferred line width.
484
+ */
485
+ static VALUE line_width(VALUE self)
486
+ {
487
+ yaml_emitter_t * emitter;
488
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
489
+
490
+ return INT2NUM(emitter->best_width);
491
+ }
492
+
493
+ /* call-seq: emitter.line_width = width
494
+ *
495
+ * Set the preferred line with to +width+.
496
+ */
497
+ static VALUE set_line_width(VALUE self, VALUE width)
498
+ {
499
+ yaml_emitter_t * emitter;
500
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
501
+
502
+ yaml_emitter_set_width(emitter, NUM2INT(width));
503
+
504
+ return width;
505
+ }
506
+
507
+ void Init_psych_emitter()
508
+ {
509
+ VALUE psych = rb_define_module("Psych");
510
+ VALUE handler = rb_define_class_under(psych, "Handler", rb_cObject);
511
+ cPsychEmitter = rb_define_class_under(psych, "Emitter", handler);
512
+
513
+ rb_define_alloc_func(cPsychEmitter, allocate);
514
+
515
+ rb_define_method(cPsychEmitter, "initialize", initialize, -1);
516
+ rb_define_method(cPsychEmitter, "start_stream", start_stream, 1);
517
+ rb_define_method(cPsychEmitter, "end_stream", end_stream, 0);
518
+ rb_define_method(cPsychEmitter, "start_document", start_document, 3);
519
+ rb_define_method(cPsychEmitter, "end_document", end_document, 1);
520
+ rb_define_method(cPsychEmitter, "scalar", scalar, 6);
521
+ rb_define_method(cPsychEmitter, "start_sequence", start_sequence, 4);
522
+ rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0);
523
+ rb_define_method(cPsychEmitter, "start_mapping", start_mapping, 4);
524
+ rb_define_method(cPsychEmitter, "end_mapping", end_mapping, 0);
525
+ rb_define_method(cPsychEmitter, "alias", alias, 1);
526
+ rb_define_method(cPsychEmitter, "canonical", canonical, 0);
527
+ rb_define_method(cPsychEmitter, "canonical=", set_canonical, 1);
528
+ rb_define_method(cPsychEmitter, "indentation", indentation, 0);
529
+ rb_define_method(cPsychEmitter, "indentation=", set_indentation, 1);
530
+ rb_define_method(cPsychEmitter, "line_width", line_width, 0);
531
+ rb_define_method(cPsychEmitter, "line_width=", set_line_width, 1);
532
+
533
+ id_write = rb_intern("write");
534
+ id_line_width = rb_intern("line_width");
535
+ id_indentation = rb_intern("indentation");
536
+ id_canonical = rb_intern("canonical");
537
+ }
538
+ /* vim: set noet sws=4 sw=4: */