rexleparser 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rexleparser.rb +51 -33
  2. 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
- a.shift until a[0] = '>'
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
- value = raw_values
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
- if raw_values.length > 0 then
41
+ attributes = get_attributes(raw_values) if raw_values.length > 0
42
+ element = [name, '', attributes]
43
+ else
39
44
 
40
- match_found = raw_values.match(/(.*)>([^>]*$)/)
41
- if match_found then
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
- attributes = raw_attributes.scan(/(\w+\='[^']+')|(\w+\="[^"]+")/).map(&:compact).flatten.inject({}) do |r, x|
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
- tag = a[0, name.length + 3].join
50
+ tag = a[0, name.length + 3].join
54
51
 
55
- if a.length > 0 then
52
+ if a.length > 0 then
56
53
 
57
- children = true
58
- children = false if tag == "</%s>" % name
54
+ children = true
55
+ children = false if tag == "</%s>" % name
59
56
 
60
- if children == true then
61
- r = scan_element(a)
62
- element << r if r
57
+ if children == true then
58
+ r = scan_element(a)
59
+ element << r if r
63
60
 
64
- (r = scan_element(a); element << r if r) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
65
- else
66
- #check for its end tag
67
- a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
68
- a.shift until a[0] == '<' or a.length <= 1
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
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rexleparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors: []
7
7