rubysl-syck 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a06b453065914717bddd099560db4f11a06d9e8c
4
- data.tar.gz: 0384d1ba6faac83b644499880bc8a72a345b800e
3
+ metadata.gz: 1a69e7732569afa3618d8b200e5de2d90704081c
4
+ data.tar.gz: 8ebd1fb1a11062f0c35619fe287f5fd392e432a6
5
5
  SHA512:
6
- metadata.gz: f04a6742cb199a336743f7c84f2375c4b163cfb00cfb79f1014c86e0cb8b46429bf773e4296830396a980a38764d2cf7700d0773f13c1069b2b76b854bbbc923
7
- data.tar.gz: 7e6716858a3d4e18f6a9d761ef30988410d7768f2475d3a303730b98df1cfde263e29eff977b00cce3759ba76089a66540b48b9586a85fa722fe9b64f93930c3
6
+ metadata.gz: e70a0872e86d2884372f69904c9c379fe0eceff4d3063f2645b078d8feebb250e36ded9cbd4bb120d6c5e22845dd6369af7b80706c5bd72282f4d774dbbabde1
7
+ data.tar.gz: cd363bdc9654ebd71cc37d8c9d866806cdf5f151245bd18bfff3e211e10c9c58ecd07208e6c2be4afd8da99535f6a7082d62dc07328ce9fe61bb3d12186d2bcf
data/lib/rubysl/syck.rb CHANGED
@@ -1,2 +1,2 @@
1
- require "syck/syck"
1
+ require "rubysl/syck/syck"
2
2
  require "rubysl/syck/version"
@@ -0,0 +1,53 @@
1
+ # $Id$
2
+ #
3
+ # = yaml/syck.rb:
4
+ #
5
+
6
+ require 'stringio'
7
+ require 'syck/syck'
8
+ require 'syck/error'
9
+ require 'syck/syck'
10
+ require 'syck/tag'
11
+ require 'syck/stream'
12
+ require 'syck/constants'
13
+ require 'syck/rubytypes'
14
+ require 'syck/types'
15
+
16
+ module Syck
17
+ #--
18
+ # For Rubinius, replaces the rb_iterate call to syck_set_ivars.
19
+ #++
20
+ def self.set_ivars(hsh, obj)
21
+ hsh.each do |key, value|
22
+ obj.instance_variable_set :"@#{key}", value
23
+ end
24
+ end
25
+
26
+ #--
27
+ # For Rubinius, replaces the rb_iterate call to syck_merge_i.
28
+ #++
29
+ def self.merge_i(ary, hsh)
30
+ ary.each do |entry|
31
+ begin
32
+ entry = Rubinius::Type.coerce_to entry, Hash, :to_hash
33
+ hsh.update entry
34
+ rescue
35
+ # ignore coercion errors
36
+ end
37
+ end
38
+
39
+ nil
40
+ end
41
+
42
+ #--
43
+ # For Rubinius, replaces rb_syck_mktime.
44
+ #++
45
+ def self.mktime(str)
46
+ require "date"
47
+ begin
48
+ DateTime.parse(str).to_time
49
+ rescue ArgumentError
50
+ # nothing
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Syck
3
- VERSION = "2.0.1"
3
+ VERSION = "2.0.2"
4
4
  end
5
5
  end
@@ -0,0 +1,242 @@
1
+ #
2
+ # BaseEmitter
3
+ #
4
+
5
+ require 'syck/constants'
6
+ require 'syck/encoding'
7
+ require 'syck/error'
8
+
9
+ module Syck
10
+ module BaseEmitter
11
+ def options( opt = nil )
12
+ if opt
13
+ @options[opt] || DEFAULTS[opt]
14
+ else
15
+ @options
16
+ end
17
+ end
18
+
19
+ def options=( opt )
20
+ @options = opt
21
+ end
22
+
23
+ #
24
+ # Emit binary data
25
+ #
26
+ def binary_base64( value )
27
+ self << "!binary "
28
+ self.node_text( [value].pack("m"), '|' )
29
+ end
30
+
31
+ #
32
+ # Emit plain, normal flowing text
33
+ #
34
+ def node_text( value, block = nil )
35
+ @seq_map = false
36
+ valx = value.dup
37
+ unless block
38
+ block =
39
+ if options(:UseBlock)
40
+ '|'
41
+ elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{ESCAPE_CHAR}/
42
+ '|'
43
+ else
44
+ '>'
45
+ end
46
+ indt = $&.to_i if block =~ /\d+/
47
+ if valx =~ /(\A\n*[ \t#]|^---\s+)/
48
+ indt = options(:Indent) unless indt.to_i > 0
49
+ block += indt.to_s
50
+ end
51
+
52
+ block +=
53
+ if valx =~ /\n\Z\n/
54
+ "+"
55
+ elsif valx =~ /\Z\n/
56
+ ""
57
+ else
58
+ "-"
59
+ end
60
+ end
61
+ block += "\n"
62
+ if block[0] == ?"
63
+ esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
64
+ valx = fold( Syck.escape( valx, esc_skip ) + "\"" ).chomp
65
+ self << '"' + indent_text( valx, indt, false )
66
+ else
67
+ if block[0] == ?>
68
+ valx = fold( valx )
69
+ end
70
+ #p [block, indt]
71
+ self << block + indent_text( valx, indt )
72
+ end
73
+ end
74
+
75
+ #
76
+ # Emit a simple, unqouted string
77
+ #
78
+ def simple( value )
79
+ @seq_map = false
80
+ self << value.to_s
81
+ end
82
+
83
+ #
84
+ # Emit double-quoted string
85
+ #
86
+ def double( value )
87
+ "\"#{Syck.escape( value )}\""
88
+ end
89
+
90
+ #
91
+ # Emit single-quoted string
92
+ #
93
+ def single( value )
94
+ "'#{value}'"
95
+ end
96
+
97
+ #
98
+ # Write a text block with the current indent
99
+ #
100
+ def indent_text( text, mod, first_line = true )
101
+ return "" if text.to_s.empty?
102
+ spacing = indent( mod )
103
+ text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
104
+ return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
105
+ end
106
+
107
+ #
108
+ # Write a current indent
109
+ #
110
+ def indent( mod = nil )
111
+ #p [ self.id, level, mod, :INDENT ]
112
+ if level <= 0
113
+ mod ||= 0
114
+ else
115
+ mod ||= options(:Indent)
116
+ mod += ( level - 1 ) * options(:Indent)
117
+ end
118
+ return " " * mod
119
+ end
120
+
121
+ #
122
+ # Add indent to the buffer
123
+ #
124
+ def indent!
125
+ self << indent
126
+ end
127
+
128
+ #
129
+ # Folding paragraphs within a column
130
+ #
131
+ def fold( value )
132
+ value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do
133
+ $1 || $2 + ( $3 || "\n" )
134
+ end
135
+ end
136
+
137
+ #
138
+ # Quick mapping
139
+ #
140
+ def map( type, &e )
141
+ val = Mapping.new
142
+ e.call( val )
143
+ self << "#{type} " if type.length.nonzero?
144
+
145
+ #
146
+ # Empty hashes
147
+ #
148
+ if val.length.zero?
149
+ self << "{}"
150
+ @seq_map = false
151
+ else
152
+ # FIXME
153
+ # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
154
+ # @headless = 1
155
+ # end
156
+
157
+ defkey = @options.delete( :DefaultKey )
158
+ if defkey
159
+ seq_map_shortcut
160
+ self << "= : "
161
+ defkey.to_yaml( :Emitter => self )
162
+ end
163
+
164
+ #
165
+ # Emit the key and value
166
+ #
167
+ val.each { |v|
168
+ seq_map_shortcut
169
+ if v[0].is_complex_yaml?
170
+ self << "? "
171
+ end
172
+ v[0].to_yaml( :Emitter => self )
173
+ if v[0].is_complex_yaml?
174
+ self << "\n"
175
+ indent!
176
+ end
177
+ self << ": "
178
+ v[1].to_yaml( :Emitter => self )
179
+ }
180
+ end
181
+ end
182
+
183
+ def seq_map_shortcut
184
+ # FIXME: seq_map needs to work with the new anchoring system
185
+ # if @seq_map
186
+ # @anchor_extras[@buffer.length - 1] = "\n" + indent
187
+ # @seq_map = false
188
+ # else
189
+ self << "\n"
190
+ indent!
191
+ # end
192
+ end
193
+
194
+ #
195
+ # Quick sequence
196
+ #
197
+ def seq( type, &e )
198
+ @seq_map = false
199
+ val = Sequence.new
200
+ e.call( val )
201
+ self << "#{type} " if type.length.nonzero?
202
+
203
+ #
204
+ # Empty arrays
205
+ #
206
+ if val.length.zero?
207
+ self << "[]"
208
+ else
209
+ # FIXME
210
+ # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
211
+ # @headless = 1
212
+ # end
213
+
214
+ #
215
+ # Emit the key and value
216
+ #
217
+ val.each { |v|
218
+ self << "\n"
219
+ indent!
220
+ self << "- "
221
+ @seq_map = true if v.class == Hash
222
+ v.to_yaml( :Emitter => self )
223
+ }
224
+ end
225
+ end
226
+ end
227
+
228
+ #
229
+ # Emitter helper classes
230
+ #
231
+ class Mapping < Array
232
+ def add( k, v )
233
+ push [k, v]
234
+ end
235
+ end
236
+
237
+ class Sequence < Array
238
+ def add( v )
239
+ push v
240
+ end
241
+ end
242
+ end
@@ -0,0 +1,222 @@
1
+ #
2
+ # YAML::BaseNode class
3
+ #
4
+
5
+ module Syck
6
+
7
+ #
8
+ # YAML Generic Model container
9
+ #
10
+ module BaseNode
11
+
12
+ #
13
+ # Search for YPath entry and return
14
+ # qualified nodes.
15
+ #
16
+ def select( ypath_str )
17
+ warn "#{caller[0]}: select is deprecated" if $VERBOSE
18
+ matches = match_path( ypath_str )
19
+
20
+ #
21
+ # Create a new generic view of the elements selected
22
+ #
23
+ if matches
24
+ result = []
25
+ matches.each { |m|
26
+ result.push m.last
27
+ }
28
+ Syck.transfer( 'seq', result )
29
+ end
30
+ end
31
+
32
+ #
33
+ # Search for YPath entry and return
34
+ # transformed nodes.
35
+ #
36
+ def select!( ypath_str )
37
+ warn "#{caller[0]}: select!() is deprecated" if $VERBOSE
38
+ matches = match_path( ypath_str )
39
+
40
+ #
41
+ # Create a new generic view of the elements selected
42
+ #
43
+ if matches
44
+ result = []
45
+ matches.each { |m|
46
+ result.push m.last.transform
47
+ }
48
+ result
49
+ end
50
+ end
51
+
52
+ #
53
+ # Search for YPath entry and return a list of
54
+ # qualified paths.
55
+ #
56
+ def search( ypath_str )
57
+ warn "#{caller[0]}: search() is deprecated" if $VERBOSE
58
+ matches = match_path( ypath_str )
59
+
60
+ if matches
61
+ matches.collect { |m|
62
+ path = []
63
+ m.each_index { |i|
64
+ path.push m[i] if ( i % 2 ).zero?
65
+ }
66
+ "/" + path.compact.join( "/" )
67
+ }
68
+ end
69
+ end
70
+
71
+ def at( seg )
72
+ warn "#{caller[0]}: at() is deprecated" if $VERBOSE
73
+ if Hash === @value
74
+ self[seg]
75
+ elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i]
76
+ @value[seg.to_i]
77
+ end
78
+ end
79
+
80
+ #
81
+ # YPath search returning a complete depth array
82
+ #
83
+ def match_path( ypath_str )
84
+ warn "#{caller[0]}: match_path is deprecated" if $VERBOSE
85
+ require 'syck/ypath'
86
+ matches = []
87
+ YPath.each_path( ypath_str ) do |ypath|
88
+ seg = match_segment( ypath, 0 )
89
+ matches += seg if seg
90
+ end
91
+ matches.uniq
92
+ end
93
+
94
+ #
95
+ # Search a node for a single YPath segment
96
+ #
97
+ def match_segment( ypath, depth )
98
+ warn "#{caller[0]}: match_segment is deprecated" if $VERBOSE
99
+ deep_nodes = []
100
+ seg = ypath.segments[ depth ]
101
+ if seg == "/"
102
+ unless String === @value
103
+ idx = -1
104
+ @value.collect { |v|
105
+ idx += 1
106
+ if Hash === @value
107
+ match_init = [v[0].transform, v[1]]
108
+ match_deep = v[1].match_segment( ypath, depth )
109
+ else
110
+ match_init = [idx, v]
111
+ match_deep = v.match_segment( ypath, depth )
112
+ end
113
+ if match_deep
114
+ match_deep.each { |m|
115
+ deep_nodes.push( match_init + m )
116
+ }
117
+ end
118
+ }
119
+ end
120
+ depth += 1
121
+ seg = ypath.segments[ depth ]
122
+ end
123
+ match_nodes =
124
+ case seg
125
+ when "."
126
+ [[nil, self]]
127
+ when ".."
128
+ [["..", nil]]
129
+ when "*"
130
+ if @value.is_a? Enumerable
131
+ idx = -1
132
+ @value.collect { |h|
133
+ idx += 1
134
+ if Hash === @value
135
+ [h[0].transform, h[1]]
136
+ else
137
+ [idx, h]
138
+ end
139
+ }
140
+ end
141
+ else
142
+ if seg =~ /^"(.*)"$/
143
+ seg = $1
144
+ elsif seg =~ /^'(.*)'$/
145
+ seg = $1
146
+ end
147
+ if ( v = at( seg ) )
148
+ [[ seg, v ]]
149
+ end
150
+ end
151
+ return deep_nodes unless match_nodes
152
+ pred = ypath.predicates[ depth ]
153
+ if pred
154
+ case pred
155
+ when /^\.=/
156
+ pred = $' # '
157
+ match_nodes.reject! { |n|
158
+ n.last.value != pred
159
+ }
160
+ else
161
+ match_nodes.reject! { |n|
162
+ n.last.at( pred ).nil?
163
+ }
164
+ end
165
+ end
166
+ return match_nodes + deep_nodes unless ypath.segments.length > depth + 1
167
+
168
+ #puts "DEPTH: #{depth + 1}"
169
+ deep_nodes = []
170
+ match_nodes.each { |n|
171
+ if n[1].is_a? BaseNode
172
+ match_deep = n[1].match_segment( ypath, depth + 1 )
173
+ if match_deep
174
+ match_deep.each { |m|
175
+ deep_nodes.push( n + m )
176
+ }
177
+ end
178
+ else
179
+ deep_nodes = []
180
+ end
181
+ }
182
+ deep_nodes = nil if deep_nodes.length == 0
183
+ deep_nodes
184
+ end
185
+
186
+ #
187
+ # We want the node to act like as Hash
188
+ # if it is.
189
+ #
190
+ def []( *key )
191
+ if Hash === @value
192
+ v = @value.detect { |k,| k.transform == key.first }
193
+ v[1] if v
194
+ elsif Array === @value
195
+ @value.[]( *key )
196
+ end
197
+ end
198
+
199
+ def children
200
+ if Hash === @value
201
+ @value.values.collect { |c| c[1] }
202
+ elsif Array === @value
203
+ @value
204
+ end
205
+ end
206
+
207
+ def children_with_index
208
+ warn "#{caller[0]}: children_with_index is deprecated, use children" if $VERBOSE
209
+ if Hash === @value
210
+ @value.keys.collect { |i| [self[i], i] }
211
+ elsif Array === @value
212
+ i = -1; @value.collect { |v| i += 1; [v, i] }
213
+ end
214
+ end
215
+
216
+ def emit
217
+ transform.to_yaml
218
+ end
219
+ end
220
+
221
+ end
222
+