rexle 0.2.10 → 0.2.11

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.
Files changed (2) hide show
  1. data/lib/rexle.rb +49 -61
  2. metadata +2 -2
@@ -10,6 +10,7 @@ class Rexle
10
10
 
11
11
  def xpath(path)
12
12
 
13
+ # is it a function
13
14
  fn_match = path.match(/^(\w+)\(([^\)]+)\)$/)
14
15
 
15
16
  if fn_match.nil? then
@@ -39,19 +40,6 @@ class Rexle
39
40
  @child_lookup = []
40
41
  end
41
42
 
42
- def children()
43
- @child_elements
44
- end
45
-
46
- def children=(a)
47
- @child_elements = a
48
- end
49
-
50
- def add_element(item)
51
- @child_lookup << [item.name, item.attributes, item.value]
52
- @child_elements << item
53
- end
54
-
55
43
  def xpath(xpath_value, rlist=[])
56
44
 
57
45
  a = xpath_value.split('/')
@@ -63,63 +51,37 @@ class Rexle
63
51
  return @value
64
52
  else
65
53
  attribute = xpath_value[/^attribute::(.*)/,1]
66
- return @attributes[attribute] if attribute
54
+ return @attributes[attribute] if attribute and @attributes and @attributes.has_key?(attribute)
67
55
 
68
56
  s = a.shift
69
57
  end
70
58
 
71
- element_name, condition = s.split(/\[/)
59
+ element_name, condition = s.match(/(^[^\[]+)(\[[^\]]+\])?/).captures
72
60
 
73
- if condition then
74
- raw_items = condition.scan(/[\w]+=\'[^\']+\'|and|\d+/)
75
-
76
- if raw_items[0][/^\d+$/] then
77
- attr_search = raw_items[0].to_i
78
- else
79
- items = raw_items.map do |x|
80
- name, value = x.split(/=/)
81
- if value then
82
- "h['%s'] == '%s'" % [name,value[1..-2]]
83
- else
84
- name
85
- end
86
- end
87
- attr_search = items.join(' ')
88
- end
89
- condition = "[" + condition
90
-
91
- end
92
-
93
- wildcard = xpath_value[0,2] == '//'
61
+ attr_search = format_attributes(condition) if condition
94
62
 
95
- if wildcard then
96
- return scan_match(self, element_name, attr_search, condition, rlist)
97
- end
63
+ return scan_match(self, element_name, attr_search, condition, rlist) if xpath_value[0,2] == '//'
98
64
 
99
65
  return_elements = @child_lookup.map.with_index.select {|x| x[0][0] == element_name or element_name == '*'}
100
66
 
101
67
  if return_elements.length > 0 then
68
+
102
69
  if a.empty? then
103
70
  rlist = return_elements.map.with_index {|x,i| filter(x, i+1, attr_search)}
104
- rlist
105
71
  else
106
72
 
107
73
  return_elements.map.with_index do |x,i|
108
74
  rtn_element = filter(x, i+1, attr_search){|e| r = e.xpath(a.join('/'), rlist); r || e }
109
- if rtn_element.is_a? String then
75
+ if rtn_element.is_a? Array then
76
+ rlist = rtn_element.flatten
77
+ elsif (rtn_element.is_a? String) || not(rtn_element[0].is_a? String)
110
78
  rlist << rtn_element
111
- else
112
- if rtn_element.is_a? Element then
113
- rlist = rlist + rtn_element
114
- else
115
- if rtn_element.is_a? Array then
116
- rlist << rtn_element unless rtn_element[0].is_a? String
117
- end
118
- end
119
79
  end
120
80
  end
121
81
  end
82
+
122
83
  else
84
+
123
85
  # strip off the 1st element from the XPath
124
86
  new_xpath = xpath_value[/^\/\/\w+\/(.*)/,1]
125
87
 
@@ -131,6 +93,40 @@ class Rexle
131
93
  rlist
132
94
  end
133
95
 
96
+ def add_element(item)
97
+ @child_lookup << [item.name, item.attributes, item.value]
98
+ @child_elements << item
99
+ end
100
+
101
+ def attributes() @attributes end
102
+
103
+ def children() @child_elements end
104
+
105
+ def children=(a) @child_elements = a end
106
+
107
+ def text() @value end
108
+
109
+ private
110
+
111
+ def format_attributes(condition)
112
+ raw_items = condition[1..-1].scan(/[\w]+=\'[^\']+\'|and|\d+/)
113
+
114
+ if raw_items[0][/^\d+$/] then
115
+ return raw_items[0].to_i
116
+ else
117
+ items = raw_items.map do |x|
118
+ name, value = x.split(/=/)
119
+ if value then
120
+ "h['%s'] == '%s'" % [name,value[1..-2]]
121
+ else
122
+ name
123
+ end
124
+ end
125
+ return items.join(' ')
126
+ end
127
+ end
128
+
129
+
134
130
  def scan_match(nodes, element, attr_search, condition, rlist)
135
131
  nodes.children.each.with_index do |x, i|
136
132
  if x.name == element
@@ -147,14 +143,6 @@ class Rexle
147
143
  rlist
148
144
  end
149
145
 
150
- def attributes()
151
- @attributes
152
- end
153
-
154
- def text()
155
- @value
156
- end
157
-
158
146
  def filter(raw_element, i, attr_search, &blk)
159
147
 
160
148
  x = raw_element
@@ -178,6 +166,8 @@ class Rexle
178
166
 
179
167
  end # -- end of element --
180
168
 
169
+ private
170
+
181
171
  def scan_element(a)
182
172
 
183
173
  a.shift until a[0] == '<' and a[1] != '/' or a.length < 1
@@ -238,8 +228,6 @@ class Rexle
238
228
  end
239
229
  end
240
230
 
241
- def count(path)
242
- @doc.xpath(path).flatten.compact.length
243
- end
231
+ def count(path) @doc.xpath(path).flatten.compact.length end
244
232
 
245
- end
233
+ 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.2.10
4
+ version: 0.2.11
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-03 00:00:00 +00:00
12
+ date: 2010-11-05 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15