rexleparser 0.1.2 → 0.1.3
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/rexleparser.rb +51 -33
- metadata +1 -1
data/lib/rexleparser.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
class RexleParser
|
7
7
|
|
8
|
-
def initialize(s)
|
8
|
+
def initialize(s)
|
9
9
|
@a = scan_element(s.sub(/<\?[^>]+>/,'').split(//))
|
10
10
|
end
|
11
11
|
|
@@ -17,57 +17,55 @@ class RexleParser
|
|
17
17
|
|
18
18
|
def scan_element(a)
|
19
19
|
|
20
|
-
a.shift until a[0] == '<' and a[1] != '/' or a.length < 1
|
20
|
+
a.shift until a[0] == '<' and a[1] != '/' or a.length < 1
|
21
21
|
|
22
22
|
if a.length > 1 then
|
23
23
|
a.shift
|
24
24
|
|
25
25
|
name = ''
|
26
26
|
name << a.shift
|
27
|
-
name << a.shift while a[0] != ' ' and a[0] != '>'
|
27
|
+
name << a.shift while a[0] != ' ' and a[0] != '>' and a[0] != '/'
|
28
28
|
|
29
29
|
if name then
|
30
30
|
|
31
|
-
|
31
|
+
# find the closing tag
|
32
|
+
i = a.index(a.detect{|x| x == '>'})
|
32
33
|
raw_values = ''
|
33
|
-
a.shift
|
34
|
-
raw_values << a.shift until a[0] == '<'
|
35
34
|
|
36
|
-
|
35
|
+
# is it a self closing tag?
|
36
|
+
if a[i-1] == '/' then
|
37
|
+
raw_values << a.shift until a[0] == '/'
|
38
|
+
2.times{a.shift}
|
39
|
+
raw_values.strip!
|
37
40
|
|
38
|
-
|
41
|
+
attributes = get_attributes(raw_values) if raw_values.length > 0
|
42
|
+
element = [name, '', attributes]
|
43
|
+
else
|
39
44
|
|
40
|
-
|
41
|
-
if
|
42
|
-
raw_attributes, value = match_found.captures
|
45
|
+
raw_values << a.shift until a[0] == '<'
|
46
|
+
value, attributes = get_attributes(raw_values) if raw_values.length > 0
|
43
47
|
|
44
|
-
|
45
|
-
attr_name, val = x.split(/=/)
|
46
|
-
r.merge(attr_name => val[1..-2])
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
element = [name, value, attributes]
|
48
|
+
element = [name, value, attributes]
|
52
49
|
|
53
|
-
|
50
|
+
tag = a[0, name.length + 3].join
|
54
51
|
|
55
|
-
|
52
|
+
if a.length > 0 then
|
56
53
|
|
57
|
-
|
58
|
-
|
54
|
+
children = true
|
55
|
+
children = false if tag == "</%s>" % name
|
59
56
|
|
60
|
-
|
61
|
-
|
62
|
-
|
57
|
+
if children == true then
|
58
|
+
r = scan_element(a)
|
59
|
+
element << r if r
|
63
60
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
61
|
+
(r = scan_element(a); element << r if r) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
|
62
|
+
else
|
63
|
+
#check for its end tag
|
64
|
+
a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
|
65
|
+
a.shift until a[0] == '<' or a.length <= 1
|
66
|
+
end
|
69
67
|
end
|
70
|
-
|
68
|
+
|
71
69
|
end
|
72
70
|
|
73
71
|
element
|
@@ -75,4 +73,24 @@ class RexleParser
|
|
75
73
|
end
|
76
74
|
end
|
77
75
|
end
|
78
|
-
|
76
|
+
|
77
|
+
def get_value_and_attributes(raw_values)
|
78
|
+
attributes = {}
|
79
|
+
|
80
|
+
match_found = raw_values.match(/(.*)>([^>]*$)/)
|
81
|
+
if match_found then
|
82
|
+
raw_attributes, value = match_found.captures
|
83
|
+
attributes = get_attributes(raw_attributes)
|
84
|
+
end
|
85
|
+
|
86
|
+
[value, attributes]
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_attributes(raw_attributes)
|
90
|
+
raw_attributes.scan(/(\w+\='[^']+')|(\w+\="[^"]+")/).map(&:compact).flatten.inject({}) do |r, x|
|
91
|
+
attr_name, val = x.split(/=/)
|
92
|
+
r.merge(attr_name => val[1..-2])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|