rexleparser 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rexleparser.rb +68 -80
- metadata +3 -3
data/lib/rexleparser.rb
CHANGED
@@ -6,14 +6,11 @@
|
|
6
6
|
class RexleParser
|
7
7
|
|
8
8
|
def initialize(s)
|
9
|
-
|
10
9
|
super()
|
11
10
|
@a = scan_element(s.gsub(/<\?[^>]+>/,'').split(//))
|
12
11
|
end
|
13
12
|
|
14
|
-
def to_a()
|
15
|
-
@a
|
16
|
-
end
|
13
|
+
def to_a() @a end
|
17
14
|
|
18
15
|
def to_s()
|
19
16
|
name, value, attributes, *remaining = @a
|
@@ -34,116 +31,107 @@ class RexleParser
|
|
34
31
|
|
35
32
|
a.shift until a[0] == '<' and a[1] != '/' or a.length < 1
|
36
33
|
|
37
|
-
|
38
|
-
a.shift
|
39
|
-
|
40
|
-
# CDATA ?
|
41
|
-
if a[0..1].join == '![' then
|
42
|
-
|
43
|
-
name = '!['
|
44
|
-
8.times{ a.shift }
|
45
|
-
value = ''
|
46
|
-
|
47
|
-
value << a.shift until a[0..2].join == ']]>' or a.length <= 1
|
48
|
-
a.slice!(0,3)
|
49
|
-
element = [name, value, {}]
|
50
|
-
elsif a[0..2].join == '!--' then
|
51
|
-
name = '!-'
|
52
|
-
#<![CDATA[
|
53
|
-
#<!--
|
54
|
-
3.times{ a.shift }
|
55
|
-
value = ''
|
56
|
-
|
57
|
-
value << a.shift until a[0..2].join == '-->' or a.length <= 1
|
58
|
-
a.slice!(0,3)
|
59
|
-
element = [name, value, {}]
|
60
|
-
else
|
34
|
+
return unless a.length > 1
|
61
35
|
|
62
|
-
|
63
|
-
name << a.shift
|
64
|
-
name << a.shift while a[0] != ' ' and a[0] != '>' and a[0] != '/'
|
36
|
+
a.shift
|
65
37
|
|
66
|
-
|
38
|
+
# CDATA ?
|
39
|
+
if a[0..1].join == '![' then
|
67
40
|
|
68
|
-
|
69
|
-
|
70
|
-
|
41
|
+
name = '!['
|
42
|
+
8.times{ a.shift }
|
43
|
+
value = ''
|
71
44
|
|
72
|
-
|
73
|
-
|
45
|
+
value << a.shift until a[0..2].join == ']]>' or a.length <= 1
|
46
|
+
a.slice!(0,3)
|
47
|
+
element = [name, value, {}]
|
48
|
+
elsif a[0..2].join == '!--' then
|
49
|
+
name = '!-'
|
50
|
+
#<![CDATA[
|
51
|
+
#<!--
|
52
|
+
3.times{ a.shift }
|
53
|
+
value = ''
|
74
54
|
|
75
|
-
|
55
|
+
value << a.shift until a[0..2].join == '-->' or a.length <= 1
|
56
|
+
a.slice!(0,3)
|
57
|
+
element = [name, value, {}]
|
58
|
+
else
|
76
59
|
|
77
|
-
|
78
|
-
|
60
|
+
name = ''
|
61
|
+
name << a.shift
|
62
|
+
name << a.shift while a[0] != ' ' and a[0] != '>' and a[0] != '/'
|
79
63
|
|
80
|
-
|
81
|
-
|
82
|
-
element = [name, '', attributes]
|
64
|
+
return unless name
|
83
65
|
|
84
|
-
|
85
|
-
|
66
|
+
# find the closing tag
|
67
|
+
i = a.index('>')
|
68
|
+
raw_values = ''
|
86
69
|
|
87
|
-
|
88
|
-
|
89
|
-
value, attributes = get_value_and_attributes(raw_values) if raw_values.length > 0
|
70
|
+
# is it a self closing tag?
|
71
|
+
if a[i-1] == '/' then
|
90
72
|
|
91
|
-
|
92
|
-
|
93
|
-
|
73
|
+
raw_values << a.shift until (a[0] + a[1..-1].join.strip[0]) == '/>'
|
74
|
+
a.shift until a[0] == '<' or a.length < 1
|
75
|
+
raw_values.strip!
|
94
76
|
|
95
|
-
|
77
|
+
attributes = get_attributes(raw_values) if raw_values.length > 0
|
78
|
+
return [name, '', attributes]
|
79
|
+
|
80
|
+
else
|
96
81
|
|
97
|
-
|
98
|
-
|
82
|
+
raw_values << a.shift until a[0] == '<'
|
83
|
+
|
84
|
+
if raw_values.length > 0 then
|
85
|
+
value, attributes = get_value_and_attribs(raw_values)
|
86
|
+
end
|
87
|
+
|
88
|
+
element = [name, value, attributes]
|
89
|
+
tag = a[0, name.length + 3].join
|
99
90
|
|
100
|
-
|
91
|
+
return unless a.length > 0
|
92
|
+
|
93
|
+
children = tag == ("</%s>" % name) ? false : true
|
101
94
|
|
102
|
-
|
95
|
+
if children == true then
|
103
96
|
|
104
|
-
|
105
|
-
|
106
|
-
a.shift until a[0] == '<' or a.length <= 1
|
107
|
-
else
|
97
|
+
xa = scan_elements(a, element) until (a[0, name.length + 3].join \
|
98
|
+
== "</%s>" % [name]) or a.length < 2
|
108
99
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
100
|
+
xa.shift until xa[0] == '>' or xa.length <= 1
|
101
|
+
xa.shift
|
102
|
+
after_text = []
|
103
|
+
after_text << xa.shift until xa[0] == '<' or xa.length <= 1
|
104
|
+
|
105
|
+
return after_text.length >= 1 ? [element, after_text.join] : element
|
113
106
|
|
114
|
-
|
115
|
-
#puts 'element: ' + element.inspect
|
116
|
-
end
|
117
|
-
end
|
107
|
+
else
|
118
108
|
|
119
|
-
|
120
|
-
|
109
|
+
#check for its end tag
|
110
|
+
a.slice!(0, name.length + 3) if a[0, name.length + 3].join \
|
111
|
+
== "</%s>" % name
|
112
|
+
after_text = []
|
113
|
+
after_text << a.shift until a[0] == '<' or a.length <= 1
|
121
114
|
|
122
|
-
|
123
|
-
if remaining.nil? then #or remaining.strip.empty? then
|
124
|
-
return element
|
125
|
-
else
|
126
|
-
return [element, remaining]
|
127
|
-
end
|
115
|
+
return after_text.length >= 1 ? [element, after_text.join] : element
|
128
116
|
|
129
117
|
end
|
130
118
|
end
|
131
|
-
|
132
119
|
end
|
120
|
+
|
133
121
|
end
|
134
122
|
|
135
123
|
def scan_elements(a, element)
|
136
124
|
r = scan_element(a)
|
137
125
|
|
138
126
|
if r and r[0].is_a?(Array) then
|
139
|
-
element = r.inject(element) {|r,x|
|
127
|
+
element = r.inject(element) {|r,x| r << x} if r
|
140
128
|
elsif r
|
141
129
|
element << r
|
142
130
|
end
|
131
|
+
return a
|
143
132
|
end
|
144
133
|
|
145
|
-
def
|
146
|
-
attributes = {}
|
134
|
+
def get_value_and_attribs(raw_values)
|
147
135
|
|
148
136
|
match_found = raw_values.match(/(.*)>([^>]*$)/)
|
149
137
|
if match_found then
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rexleparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Robertson
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-07-14 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description:
|
@@ -49,6 +49,6 @@ rubyforge_project:
|
|
49
49
|
rubygems_version: 1.8.23
|
50
50
|
signing_key:
|
51
51
|
specification_version: 3
|
52
|
-
summary:
|
52
|
+
summary: Rexleparser is an XML parser used by the Rexle gem
|
53
53
|
test_files: []
|
54
54
|
|