eim_xml 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/eim_xml.rb CHANGED
@@ -5,206 +5,206 @@
5
5
  #
6
6
 
7
7
  module EimXML
8
- XML_DECLARATION = %[<?xml version="1.0"?>]
9
-
10
- class PCString
11
- attr_reader :encoded_string, :src
12
- alias to_s encoded_string
13
-
14
- def self.encode(s)
15
- s.to_s.gsub(/[&\"\'<>]/) do |m|
16
- case m
17
- when "&"
18
- "&amp;"
19
- when '"'
20
- "&quot;"
21
- when "'"
22
- "&apos;"
23
- when "<"
24
- "&lt;"
25
- when ">"
26
- "&gt;"
27
- end
28
- end
29
- end
30
-
31
- def self.[](obj)
32
- obj.is_a?(PCString) ? obj : PCString.new(obj)
33
- end
34
-
35
- def initialize(s, encoded=false)
36
- @src = s
37
- @encoded_string = encoded ? s : PCString.encode(s)
38
- end
39
-
40
- def ==(other)
41
- other.is_a?(PCString) ? @encoded_string==other.encoded_string : self==PCString.new(other)
42
- end
43
-
44
- def write_to(out="")
45
- out << encoded_string
46
- end
47
- end
48
-
49
- class Comment
50
- def initialize(text)
51
- raise ArgumentError, "Can not include '--'" if text =~ /--/
52
- @text = text
53
- end
54
-
55
- def write_to(out="")
56
- out << "<!-- #{@text} -->"
57
- end
58
- end
59
-
60
- class Element
61
- attr_reader :name, :attributes, :contents
62
-
63
- NEST = " "
64
-
65
- def initialize(name, attributes={})
66
- @name = name.to_sym
67
- @attributes = Hash.new
68
- @contents = Array.new
69
-
70
- attributes.each do |k, v|
71
- @attributes[k.to_sym] = v
72
- end
73
-
74
- yield(self) if block_given?
75
- end
76
-
77
- def name=(new_name)
78
- @name = new_name.to_sym
79
- end
80
- protected :name=
81
-
82
- def add(v)
83
- case v
84
- when nil
85
- when Array
86
- v.each{|i| self.add(i)}
87
- else
88
- @contents << v
89
- end
90
- self
91
- end
92
- alias << add
93
-
94
- def name_and_attributes(out="")
95
- out << "#{@name}"
96
- @attributes.each do |k, v|
97
- next unless v
98
- out << " #{k}='#{PCString===v ? v : PCString.encode(v.to_s)}'"
99
- end
100
- end
101
-
102
- def write_to(out = "")
103
- out << "<"
104
- name_and_attributes(out)
105
-
106
- if @contents.empty?
107
- out << " />"
108
- else
109
- out << ">"
110
- @contents.each do |c|
111
- case c
112
- when Element
113
- c.write_to(out)
114
- when PCString
115
- out << c.to_s
116
- else
117
- out << PCString.encode(c.to_s)
118
- end
119
- end
120
- out << "</#{@name}>"
121
- end
122
- out
123
- end
124
- alias :to_s :write_to
125
- alias :inspect :to_s
126
-
127
- def ==(xml)
128
- return false unless xml.is_a?(Element)
129
- @name==xml.name && @attributes==xml.attributes && @contents==xml.contents
130
- end
131
-
132
- def add_attribute(key, value)
133
- @attributes[key.to_sym] = value
134
- end
135
- alias []= add_attribute
136
-
137
- def [](key)
138
- if key.is_a?(Fixnum)
139
- @contents[key]
140
- else
141
- @attributes[key.to_sym]
142
- end
143
- end
144
-
145
- def del_attribute(key)
146
- @attributes.delete(key.to_sym)
147
- end
148
-
149
- def pcstring_contents
150
- @contents.select{|c| c.is_a?(String)||c.is_a?(PCString)}.map{|c| c.is_a?(String) ? PCString.new(c) : c}
151
- end
152
-
153
- def match(obj, attr=nil)
154
- return match(Element.new(obj, attr)) if attr
155
- return obj=~@name.to_s if obj.is_a?(Regexp)
156
- return @name==obj if obj.is_a?(Symbol)
157
- return is_a?(obj) if obj.is_a?(Module)
158
-
159
- raise ArgumentError unless obj.is_a?(Element)
160
-
161
- return false unless @name==obj.name
162
-
163
- obj.attributes.all? do |k, v|
164
- (v.nil? && !@attributes.include?(k)) ||
165
- (@attributes.include?(k) && (v.is_a?(Regexp) ? v =~ @attributes[k] : PCString[v] == PCString[@attributes[k]]))
166
- end and obj.contents.all? do |i|
167
- case i
168
- when Element
169
- has_element?(i)
170
- when String
171
- pcstring_contents.include?(PCString.new(i))
172
- when PCString
173
- pcstring_contents.include?(i)
174
- when Regexp
175
- @contents.any?{|c| c.is_a?(String) and i=~c}
176
- end
177
- end
178
- end
179
- alias :=~ :match
180
-
181
- def has?(obj, attr=nil)
182
- return has?(Element.new(obj, attr)) if attr
183
-
184
- @contents.any? do |i|
185
- if i.is_a?(Element)
186
- i.match(obj) || i.has?(obj)
187
- else
188
- obj.is_a?(Module) && i.is_a?(obj)
189
- end
190
- end
191
- end
192
- alias has_element? has?
193
- alias include? has?
194
-
195
- def find(obj, dst=Element.new(:found))
196
- return find(Element.new(obj, dst)) if dst.is_a?(Hash)
197
-
198
- dst << self if match(obj)
199
- @contents.each do |i|
200
- case
201
- when i.is_a?(Element)
202
- i.find(obj, dst)
203
- when obj.is_a?(Module) && i.is_a?(obj)
204
- dst << i
205
- end
206
- end
207
- dst
208
- end
209
- end
8
+ XML_DECLARATION = %(<?xml version="1.0"?>)
9
+
10
+ class PCString
11
+ attr_reader :encoded_string, :src
12
+ alias to_s encoded_string
13
+
14
+ ENCODING_MAP = {
15
+ '&' => '&amp;',
16
+ '"' => '&quot;',
17
+ "'" => '&apos;',
18
+ '<' => '&lt;',
19
+ '>' => '&gt;'
20
+ }
21
+
22
+ def self.encode(src)
23
+ src.to_s.gsub(/[&"'<>]/) do |m|
24
+ ENCODING_MAP[m]
25
+ end
26
+ end
27
+
28
+ def self.[](obj)
29
+ obj.is_a?(PCString) ? obj : PCString.new(obj)
30
+ end
31
+
32
+ def initialize(src, encoded = false) # rubocop:disable Style/OptionalBooleanParameter
33
+ @src = src
34
+ @encoded_string = encoded ? src : PCString.encode(src)
35
+ end
36
+
37
+ def ==(other)
38
+ other.is_a?(PCString) ? @encoded_string == other.encoded_string : self == PCString.new(other)
39
+ end
40
+
41
+ def write_to(out = '')
42
+ out << encoded_string
43
+ end
44
+ end
45
+
46
+ class Comment
47
+ def initialize(text)
48
+ raise ArgumentError, "Can not include '--'" if text =~ /--/
49
+
50
+ @text = text
51
+ end
52
+
53
+ def write_to(out = '')
54
+ out << "<!-- #{@text} -->"
55
+ end
56
+ end
57
+
58
+ class Element
59
+ attr_reader :name, :attributes, :contents
60
+
61
+ NEST = ' '
62
+
63
+ def initialize(name, attributes = {})
64
+ @name = name.to_sym
65
+ @attributes = {}
66
+ @contents = []
67
+
68
+ attributes.each do |k, v|
69
+ @attributes[k.to_sym] = v
70
+ end
71
+
72
+ yield(self) if block_given?
73
+ end
74
+
75
+ def name=(new_name)
76
+ @name = new_name.to_sym
77
+ end
78
+ protected :name=
79
+
80
+ def add(content)
81
+ case content
82
+ when nil
83
+ # nothing to do
84
+ when Array
85
+ content.each { |i| add(i) }
86
+ else
87
+ @contents << content
88
+ end
89
+ self
90
+ end
91
+ alias << add
92
+
93
+ def name_and_attributes(out = '')
94
+ out << @name.to_s
95
+ @attributes.each do |k, v|
96
+ next unless v
97
+
98
+ out << " #{k}='#{v.is_a?(PCString) ? v : PCString.encode(v.to_s)}'"
99
+ end
100
+ end
101
+
102
+ def write_to(out = '')
103
+ out << '<'
104
+ name_and_attributes(out)
105
+
106
+ if @contents.empty?
107
+ out << ' />'
108
+ else
109
+ out << '>'
110
+ @contents.each do |c|
111
+ case c
112
+ when Element
113
+ c.write_to(out)
114
+ when PCString
115
+ out << c.to_s
116
+ else
117
+ out << PCString.encode(c.to_s)
118
+ end
119
+ end
120
+ out << "</#{@name}>"
121
+ end
122
+ out
123
+ end
124
+ alias to_s write_to
125
+ alias inspect to_s
126
+
127
+ def ==(other)
128
+ return false unless other.is_a?(Element)
129
+
130
+ @name == other.name && @attributes == other.attributes && @contents == other.contents
131
+ end
132
+
133
+ def add_attribute(key, value)
134
+ @attributes[key.to_sym] = value
135
+ end
136
+ alias []= add_attribute
137
+
138
+ def [](key)
139
+ if key.is_a?(Integer)
140
+ @contents[key]
141
+ else
142
+ @attributes[key.to_sym]
143
+ end
144
+ end
145
+
146
+ def del_attribute(key)
147
+ @attributes.delete(key.to_sym)
148
+ end
149
+
150
+ def pcstring_contents
151
+ @contents.select { |c| c.is_a?(String) || c.is_a?(PCString) }.map { |c| c.is_a?(String) ? PCString.new(c) : c }
152
+ end
153
+
154
+ def match(obj, attr = nil)
155
+ return match(Element.new(obj, attr)) if attr
156
+ return obj =~ @name.to_s if obj.is_a?(Regexp)
157
+ return @name == obj if obj.is_a?(Symbol)
158
+ return is_a?(obj) if obj.is_a?(Module)
159
+
160
+ raise ArgumentError unless obj.is_a?(Element)
161
+
162
+ return false unless @name == obj.name
163
+
164
+ obj.attributes.all? do |k, v|
165
+ (v.nil? && !@attributes.include?(k)) ||
166
+ (@attributes.include?(k) && (v.is_a?(Regexp) ? v =~ @attributes[k] : PCString[v] == PCString[@attributes[k]]))
167
+ end and obj.contents.all? do |i|
168
+ case i
169
+ when Element
170
+ has_element?(i)
171
+ when String
172
+ pcstring_contents.include?(PCString.new(i))
173
+ when PCString
174
+ pcstring_contents.include?(i)
175
+ when Regexp
176
+ @contents.any? { |c| c.is_a?(String) and i =~ c }
177
+ end
178
+ end
179
+ end
180
+ alias =~ match
181
+
182
+ def has?(obj, attr = nil)
183
+ return has?(Element.new(obj, attr)) if attr
184
+
185
+ @contents.any? do |i|
186
+ if i.is_a?(Element)
187
+ i.match(obj) || i.has?(obj)
188
+ else
189
+ obj.is_a?(Module) && i.is_a?(obj)
190
+ end
191
+ end
192
+ end
193
+ alias has_element? has?
194
+ alias include? has?
195
+
196
+ def find(obj, dst = Element.new(:found))
197
+ return find(Element.new(obj, dst)) if dst.is_a?(Hash)
198
+
199
+ dst << self if match(obj)
200
+ @contents.each do |i|
201
+ if i.is_a?(Element)
202
+ i.find(obj, dst)
203
+ elsif obj.is_a?(Module) && i.is_a?(obj)
204
+ dst << i
205
+ end
206
+ end
207
+ dst
208
+ end
209
+ end
210
210
  end
metadata CHANGED
@@ -1,83 +1,53 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: eim_xml
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 4
10
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - KURODA Hiraku
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-07-19 00:00:00 Z
11
+ date: 2024-08-03 00:00:00.000000000 Z
19
12
  dependencies: []
20
-
21
13
  description:
22
- email: hiraku@hinet.mydns.jp
14
+ email: hirakuro@gmail.com
23
15
  executables: []
24
-
25
16
  extensions: []
26
-
27
17
  extra_rdoc_files: []
28
-
29
- files:
30
- - Rakefile.utirake
31
- - Rakefile
32
- - lib/eim_xml/matcher.rb
33
- - lib/eim_xml/xhtml.rb
18
+ files:
19
+ - LICENSE
20
+ - lib/eim_xml.rb
21
+ - lib/eim_xml/assertions.rb
34
22
  - lib/eim_xml/dsl.rb
35
- - lib/eim_xml/parser.rb
36
23
  - lib/eim_xml/formatter.rb
37
- - lib/eim_xml/xhtml/dsl.rb
38
- - lib/eim_xml/assertions.rb
39
24
  - lib/eim_xml/formatter/element_wrapper.rb
40
- - lib/eim_xml.rb
41
- - spec/assertions_test.rb
42
- - spec/formatter_spec.rb
43
- - spec/eim_xml_spec.rb
44
- - spec/dsl_spec.rb
45
- - spec/xhtml_spec.rb
46
- - spec/parser_spec.rb
47
- homepage: http://eimxml.rubyforge.org/
48
- licenses: []
49
-
25
+ - lib/eim_xml/matcher.rb
26
+ - lib/eim_xml/parser.rb
27
+ - lib/eim_xml/xhtml.rb
28
+ - lib/eim_xml/xhtml/dsl.rb
29
+ homepage: https://github.com/hirakuro/eim_xml
30
+ licenses:
31
+ - GPL-2.0-only
32
+ metadata:
33
+ rubygems_mfa_required: 'true'
50
34
  post_install_message:
51
35
  rdoc_options: []
52
-
53
- require_paths:
36
+ require_paths:
54
37
  - lib
55
- required_ruby_version: !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.2.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
58
45
  - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
- version: "0"
64
- required_rubygems_version: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ">"
68
- - !ruby/object:Gem::Version
69
- hash: 25
70
- segments:
71
- - 1
72
- - 3
73
- - 1
74
- version: 1.3.1
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
75
48
  requirements: []
76
-
77
- rubyforge_project: eimxml
78
- rubygems_version: 1.8.5
49
+ rubygems_version: 3.5.11
79
50
  signing_key:
80
- specification_version: 3
51
+ specification_version: 4
81
52
  summary: Easy IMplemented XML
82
53
  test_files: []
83
-
data/Rakefile DELETED
@@ -1,26 +0,0 @@
1
- load "Rakefile.utirake"
2
- VER = "0.0.4"
3
-
4
- UtiRake.setup do
5
- rdoc do |t|
6
- t.title = "Easy IMplemented XML"
7
- t.main = "README"
8
- t.rdoc_files.include(FileList["lib/**/*.rb", "README"])
9
- end
10
-
11
- gemspec do |s|
12
- s.name = "eim_xml"
13
- s.summary = "Easy IMplemented XML"
14
- s.author = "KURODA Hiraku"
15
- s.email = "hiraku@hinet.mydns.jp"
16
- s.homepage = "http://eimxml.rubyforge.org/"
17
- s.rubyforge_project = "eimxml"
18
- s.version = VER
19
- end
20
-
21
- publish("eimxml", "hiraku")
22
-
23
- alias_task
24
- end
25
-
26
- task :default => :spec