rexleparser 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rexleparser.rb +36 -9
- metadata +1 -1
data/lib/rexleparser.rb
CHANGED
@@ -5,8 +5,10 @@
|
|
5
5
|
|
6
6
|
class RexleParser
|
7
7
|
|
8
|
-
def initialize(s)
|
9
|
-
|
8
|
+
def initialize(s)
|
9
|
+
|
10
|
+
super()
|
11
|
+
@a = scan_element(s.sub(/<\?[^>]+>/,'').split(//))
|
10
12
|
end
|
11
13
|
|
12
14
|
def to_a()
|
@@ -46,7 +48,9 @@ class RexleParser
|
|
46
48
|
|
47
49
|
# is it a self closing tag?
|
48
50
|
if a[i-1] == '/' then
|
49
|
-
|
51
|
+
|
52
|
+
raw_values << a.shift until (a[0] + a[1..-1].join.strip[0]) == '/>'
|
53
|
+
puts 'raw_values: ' + raw_values
|
50
54
|
a.shift until a[0] == '<' or a.length < 1
|
51
55
|
raw_values.strip!
|
52
56
|
|
@@ -55,6 +59,7 @@ class RexleParser
|
|
55
59
|
else
|
56
60
|
|
57
61
|
raw_values << a.shift until a[0] == '<'
|
62
|
+
#puts 'raw_values: ' + raw_values.inspect
|
58
63
|
value, attributes = get_value_and_attributes(raw_values) if raw_values.length > 0
|
59
64
|
|
60
65
|
element = [name, value, attributes]
|
@@ -68,19 +73,31 @@ class RexleParser
|
|
68
73
|
|
69
74
|
if children == true then
|
70
75
|
|
71
|
-
(
|
76
|
+
scan_elements(a, element) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
|
77
|
+
|
78
|
+
#(r = scan_element(a); element << r if r) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
|
72
79
|
a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
73
80
|
a.shift until a[0] == '<' or a.length <= 1
|
74
81
|
else
|
82
|
+
|
75
83
|
#check for its end tag
|
76
84
|
a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
77
|
-
|
85
|
+
text_remaining = []
|
86
|
+
text_remaining << a.shift until a[0] == '<' or a.length <= 1
|
87
|
+
|
88
|
+
remaining = text_remaining.join unless text_remaining.empty?
|
89
|
+
#puts 'element: ' + element.inspect
|
78
90
|
end
|
79
91
|
end
|
80
92
|
|
81
93
|
end
|
82
94
|
|
83
|
-
return element
|
95
|
+
#return remaining ? [element, remaining] : element
|
96
|
+
if remaining.nil? then
|
97
|
+
return element
|
98
|
+
else
|
99
|
+
return [element, remaining]
|
100
|
+
end
|
84
101
|
|
85
102
|
end
|
86
103
|
end
|
@@ -88,6 +105,16 @@ class RexleParser
|
|
88
105
|
end
|
89
106
|
end
|
90
107
|
|
108
|
+
def scan_elements(a, element)
|
109
|
+
r = scan_element(a)
|
110
|
+
|
111
|
+
if r and r[0].is_a?(Array) then
|
112
|
+
element = r.inject(element) {|r,x| r << x} if r
|
113
|
+
elsif r
|
114
|
+
element << r
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
91
118
|
def get_value_and_attributes(raw_values)
|
92
119
|
attributes = {}
|
93
120
|
|
@@ -101,10 +128,10 @@ class RexleParser
|
|
101
128
|
end
|
102
129
|
|
103
130
|
def get_attributes(raw_attributes)
|
104
|
-
raw_attributes.scan(/(\w+\='[^']+')|(\w+\="[^"]+")/).map(&:compact).flatten.inject({}) do |r, x|
|
131
|
+
raw_attributes.scan(/([\w:]+\='[^']+')|([\w:]+\="[^"]+")/).map(&:compact).flatten.inject({}) do |r, x|
|
132
|
+
|
105
133
|
attr_name, val = x.split(/=/)
|
106
|
-
r.merge(attr_name
|
134
|
+
r.merge(attr_name => val[1..-2])
|
107
135
|
end
|
108
136
|
end
|
109
|
-
|
110
137
|
end
|