rexleparser 0.1.0
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 +78 -0
- metadata +55 -0
data/lib/rexleparser.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# file: rexleparser.rb
|
4
|
+
# description: used by rexle.rb
|
5
|
+
|
6
|
+
class RexleParser
|
7
|
+
|
8
|
+
def initialize(s)
|
9
|
+
@a = scan_element(s.split(//))
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_a()
|
13
|
+
@a
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def scan_element(a)
|
19
|
+
|
20
|
+
a.shift until a[0] == '<' and a[1] != '/' or a.length < 1
|
21
|
+
|
22
|
+
if a.length > 1 then
|
23
|
+
a.shift
|
24
|
+
|
25
|
+
name = ''
|
26
|
+
name << a.shift
|
27
|
+
name << a.shift while a[0] != ' ' and a[0] != '>'
|
28
|
+
|
29
|
+
if name then
|
30
|
+
|
31
|
+
a.shift until a[0] = '>'
|
32
|
+
raw_values = ''
|
33
|
+
a.shift
|
34
|
+
raw_values << a.shift until a[0] == '<'
|
35
|
+
|
36
|
+
value = raw_values
|
37
|
+
|
38
|
+
if raw_values.length > 0 then
|
39
|
+
|
40
|
+
match_found = raw_values.match(/(.*)>([^>]*$)/)
|
41
|
+
if match_found then
|
42
|
+
raw_attributes, value = match_found.captures
|
43
|
+
|
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]
|
52
|
+
|
53
|
+
tag = a[0, name.length + 3].join
|
54
|
+
|
55
|
+
if a.length > 0 then
|
56
|
+
|
57
|
+
children = true
|
58
|
+
children = false if tag == "</%s>" % name
|
59
|
+
|
60
|
+
if children == true then
|
61
|
+
r = scan_element(a)
|
62
|
+
element << r if r
|
63
|
+
|
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
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
element
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rexleparser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-11-09 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/rexleparser.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: rexleparser
|
54
|
+
test_files: []
|
55
|
+
|