rexle 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rexle.rb +10 -62
- metadata +2 -2
data/lib/rexle.rb
CHANGED
@@ -2,10 +2,13 @@
|
|
2
2
|
|
3
3
|
# file: rexle.rb
|
4
4
|
|
5
|
+
require 'rexleparser'
|
6
|
+
|
5
7
|
class Rexle
|
6
8
|
|
7
9
|
def initialize(s)
|
8
|
-
|
10
|
+
a = RexleParser.new(s).to_a
|
11
|
+
@doc = scan_element(*a)
|
9
12
|
end
|
10
13
|
|
11
14
|
def xpath(path)
|
@@ -186,66 +189,10 @@ class Rexle
|
|
186
189
|
|
187
190
|
private
|
188
191
|
|
189
|
-
def scan_element(
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
if a.length > 1 then
|
194
|
-
a.shift
|
195
|
-
|
196
|
-
name = ''
|
197
|
-
name << a.shift
|
198
|
-
name << a.shift while a[0] != ' ' and a[0] != '>'
|
199
|
-
|
200
|
-
if name then
|
201
|
-
|
202
|
-
a.shift until a[0] = '>'
|
203
|
-
raw_values = ''
|
204
|
-
a.shift
|
205
|
-
raw_values << a.shift until a[0] == '<'
|
206
|
-
|
207
|
-
value = raw_values
|
208
|
-
|
209
|
-
if raw_values.length > 0 then
|
210
|
-
match_found = raw_values.match(/(.*)>([^>]*$)/)
|
211
|
-
if match_found then
|
212
|
-
raw_attributes, value = match_found.captures
|
213
|
-
attributes = raw_attributes.scan(/(\w+\='[^']+')|(\w+\="[^"]+")/)\
|
214
|
-
.map(&:compact).flatten.inject({}) do |r, x|
|
215
|
-
|
216
|
-
attr_name, val = x.split(/=/)
|
217
|
-
r.merge(attr_name => val[1..-2])
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
element = Element.new(name, value, attributes)
|
223
|
-
|
224
|
-
tag = a[0, name.length + 3].join
|
225
|
-
|
226
|
-
if a.length > 0 then
|
227
|
-
children = true
|
228
|
-
children = false if tag == "</%s>" % name
|
229
|
-
|
230
|
-
if children == true then
|
231
|
-
r = scan_element(a)
|
232
|
-
element.add_element(r) if r
|
233
|
-
|
234
|
-
# capture siblings
|
235
|
-
#a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
236
|
-
|
237
|
-
(r = scan_element(a); element.add_element r if r) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
|
238
|
-
else
|
239
|
-
#check for its end tag
|
240
|
-
a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
241
|
-
end
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
element
|
246
|
-
|
247
|
-
end
|
248
|
-
end
|
192
|
+
def scan_element(name, value, attributes, *children)
|
193
|
+
element = Element.new(name, value, attributes)
|
194
|
+
children.each{|x| element.add_element scan_element(*x)} if children
|
195
|
+
return element
|
249
196
|
end
|
250
197
|
|
251
198
|
def scan_print(nodes)
|
@@ -255,6 +202,7 @@ class Rexle
|
|
255
202
|
tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
|
256
203
|
|
257
204
|
out = ["<%s>" % tag]
|
205
|
+
out << x.value unless x.value.empty?
|
258
206
|
out << scan_print(x.children)
|
259
207
|
out << "</%s>" % x.name
|
260
208
|
end
|
@@ -263,4 +211,4 @@ class Rexle
|
|
263
211
|
|
264
212
|
def count(path) @doc.xpath(path).flatten.compact.length end
|
265
213
|
|
266
|
-
end
|
214
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rexle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors: []
|
7
7
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-11-
|
12
|
+
date: 2010-11-09 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|