rexle 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/rexle.rb +119 -0
- metadata +55 -0
data/lib/rexle.rb
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/ruby
|
|
2
|
+
|
|
3
|
+
# file: rexle.rb
|
|
4
|
+
|
|
5
|
+
class Rexle
|
|
6
|
+
|
|
7
|
+
def initialize(s)
|
|
8
|
+
@doc = scan_element(s.split(//))
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def xpath(path)
|
|
12
|
+
@doc.xpath path
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Element
|
|
16
|
+
attr_accessor :name, :value
|
|
17
|
+
|
|
18
|
+
def initialize(name=nil, value='', attributes={})
|
|
19
|
+
@name, @value, @attributes = name, value, attributes
|
|
20
|
+
raise "Element name must not be blank" unless name
|
|
21
|
+
@child_elements = []
|
|
22
|
+
@child_lookup = {}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def children()
|
|
26
|
+
@child_elements
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def children=(a)
|
|
30
|
+
@child_elements = a
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def add_element(item)
|
|
34
|
+
@child_lookup[item.name] = item.value
|
|
35
|
+
@child_elements << item
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def xpath(xpath_value)
|
|
39
|
+
|
|
40
|
+
a = xpath_value.split('/')
|
|
41
|
+
element_name = a.shift
|
|
42
|
+
|
|
43
|
+
index = @child_lookup.keys.index(element_name)
|
|
44
|
+
if index then
|
|
45
|
+
if a.empty? then
|
|
46
|
+
@child_elements[index]
|
|
47
|
+
else
|
|
48
|
+
@child_elements[index].xpath a.join
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def attributes()
|
|
54
|
+
@attributes
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def scan_element(a)
|
|
60
|
+
|
|
61
|
+
a.shift until a[0] == '<' and a[1] != '/' or a.length < 1
|
|
62
|
+
|
|
63
|
+
if a.length > 1 then
|
|
64
|
+
a.shift
|
|
65
|
+
|
|
66
|
+
name = ''
|
|
67
|
+
name << a.shift
|
|
68
|
+
name << a.shift while a[0] != ' ' and a[0] != '>'
|
|
69
|
+
|
|
70
|
+
if name then
|
|
71
|
+
|
|
72
|
+
a.shift until a[0] = '>'
|
|
73
|
+
raw_values = ''
|
|
74
|
+
a.shift
|
|
75
|
+
raw_values << a.shift until a[0] == '<'
|
|
76
|
+
|
|
77
|
+
value = raw_values
|
|
78
|
+
|
|
79
|
+
if raw_values.length > 0 then
|
|
80
|
+
match_found = raw_values.match(/(.*)>([^>]+$)/)
|
|
81
|
+
if match_found then
|
|
82
|
+
raw_attributes, value = match_found.captures
|
|
83
|
+
attributes = raw_attributes.split(/\s/).inject({}) do |r, x|
|
|
84
|
+
attr_name, val = x.split(/=/)
|
|
85
|
+
r.merge(attr_name => val[1..-2])
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
element = Element.new(name, value, attributes)
|
|
91
|
+
|
|
92
|
+
tag = a[0, name.length + 3].join
|
|
93
|
+
|
|
94
|
+
if a.length > 0 then
|
|
95
|
+
children = true
|
|
96
|
+
children = false if tag == "</%s>" % name
|
|
97
|
+
|
|
98
|
+
if children == true then
|
|
99
|
+
r = scan_element(a)
|
|
100
|
+
element.add_element(r) if r
|
|
101
|
+
|
|
102
|
+
# capture siblings
|
|
103
|
+
a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
|
104
|
+
|
|
105
|
+
(r = scan_element(a); element.add_element r if r) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
|
|
106
|
+
else
|
|
107
|
+
#check for its end tag
|
|
108
|
+
a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
element
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rexle
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors: []
|
|
7
|
+
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2010-10-08 00:00:00 +01:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email:
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
|
|
24
|
+
files:
|
|
25
|
+
- lib/rexle.rb
|
|
26
|
+
has_rdoc: true
|
|
27
|
+
homepage:
|
|
28
|
+
licenses: []
|
|
29
|
+
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
|
|
33
|
+
require_paths:
|
|
34
|
+
- lib
|
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: "0"
|
|
40
|
+
version:
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
version:
|
|
47
|
+
requirements: []
|
|
48
|
+
|
|
49
|
+
rubyforge_project:
|
|
50
|
+
rubygems_version: 1.3.5
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 3
|
|
53
|
+
summary: rexle
|
|
54
|
+
test_files: []
|
|
55
|
+
|