mixml 0.0.3 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,6 @@
1
1
  require 'mixml/version'
2
2
  require 'mixml/tool'
3
+ require 'mixml/selection'
4
+ require 'mixml/template/text'
5
+ require 'mixml/template/expression'
6
+ require 'mixml/template/xml'
@@ -5,26 +5,28 @@ module Mixml
5
5
  # Selection of XML nodes
6
6
  class Selection
7
7
  # @return [Nokogiri::XML::NodeSet] Selected nodes
8
- attr_reader :nodes
8
+ attr_reader :nodesets
9
9
 
10
- # @param nodes [Nokogiri::XML::NodeSet] Selected nodes
11
- def initialize(nodes)
12
- @nodes = nodes
10
+ # @param nodesets [Array<Nokogiri::XML::NodeSet>] Selected nodes
11
+ def initialize(nodesets)
12
+ @nodesets = nodesets
13
13
  end
14
14
 
15
15
  # Remove selected nodes from the document
16
16
  def remove
17
- @nodes.remove
17
+ each_node do |nodeset|
18
+ nodeset.remove
19
+ end
18
20
  end
19
21
 
20
22
  # Replace selected nodes with a template
21
23
  #
22
24
  # @param template [Template::Base] Template to replace nodes with
23
- def replace(template = nil, &block)
25
+ def replace(template)
24
26
  template = template.to_mixml_template
25
27
 
26
- @nodes.each do |node|
27
- value = template.evaluate(node, &block)
28
+ each_node do |node|
29
+ value = template.evaluate(node)
28
30
  node.replace(value)
29
31
  end
30
32
  end
@@ -32,11 +34,11 @@ module Mixml
32
34
  # Append children to node
33
35
  #
34
36
  # @param template [Template::Base] Template to replace nodes with
35
- def append(template = nil, &block)
37
+ def append(template)
36
38
  template = template.to_mixml_template
37
39
 
38
- @nodes.each do |node|
39
- value = template.evaluate(node, &block)
40
+ each_node do |node|
41
+ value = template.evaluate(node)
40
42
  node << value
41
43
  end
42
44
  end
@@ -47,7 +49,7 @@ module Mixml
47
49
  def value(template)
48
50
  template = template.to_mixml_template
49
51
 
50
- @nodes.each do |node|
52
+ each_node do |node|
51
53
  value = template.evaluate(node)
52
54
  node.value = value
53
55
  end
@@ -59,10 +61,24 @@ module Mixml
59
61
  def rename(template)
60
62
  template = template.to_mixml_template
61
63
 
62
- @nodes.each do |node|
64
+ each_node do |node|
63
65
  value = template.evaluate(node)
64
66
  node.name = value
65
67
  end
66
68
  end
69
+
70
+ protected
71
+
72
+ # Execute a block for each node
73
+ #
74
+ # @yield Block to execute for each node
75
+ # @yieldparam node [Nokogiri::XML::Node] Current node
76
+ def each_node
77
+ @nodesets.each do |nodeset|
78
+ nodeset.each do |node|
79
+ yield node
80
+ end
81
+ end
82
+ end
67
83
  end
68
84
  end
@@ -6,13 +6,20 @@ module Mixml
6
6
  module Template
7
7
  # XML builder based template
8
8
  class Xml < Base
9
+ # Initialize new XML template
10
+ #
11
+ # @param proc [lambda] Proc to create xml
12
+ def initialize(proc)
13
+ @proc = proc
14
+ end
15
+
9
16
  # Evaluate the template
10
17
  #
11
18
  # @param [Nokogiri::XML::Node] node Current node
12
19
  # @return [String] Template result
13
20
  def evaluate(node)
14
21
  builder = Nokogiri::XML::Builder.new do |xml|
15
- yield(node, xml)
22
+ @proc.call(node, xml)
16
23
  end
17
24
  builder.to_xml
18
25
  end
@@ -162,36 +162,40 @@ module Mixml
162
162
 
163
163
  # Select nodes using an XPath expression and execute DSL commands for these nodes
164
164
  #
165
- # @param query [String] XPath expression
165
+ # @param paths [Array<String>] XPath expression
166
166
  # @yield Block to execute for each nodeset
167
- # @yieldparam nodes {Nokogiri::XML::NodeSet} XML nodes to process
168
167
  # @return [void]
169
- def xpath(query, &block)
168
+ def xpath(*paths, &block)
169
+ nodesets = []
170
170
  process do |xml|
171
- nodes = xml.xpath(query)
172
- selection = Selection.new(nodes)
171
+ nodesets << xml.xpath(*paths)
172
+ end
173
+ selection = Selection.new(nodesets)
173
174
 
174
- if block_given? then
175
- Docile.dsl_eval(selection, &block)
176
- end
175
+ if block_given? then
176
+ Docile.dsl_eval(selection, &block)
177
177
  end
178
+
179
+ selection
178
180
  end
179
181
 
180
182
  # Select nodes using CSS selectors and execute DSL commands for these nodes
181
183
  #
182
- # @param selectors [String] CSS selectors
184
+ # @param selectors [Array<String>] CSS selectors
183
185
  # @yield Block to execute for each nodeset
184
- # @yieldparam nodes {Nokogiri::XML::NodeSet} XML nodes to process
185
186
  # @return [void]
186
187
  def css(*selectors, &block)
188
+ nodesets = []
187
189
  process do |xml|
188
- nodes = xml.css(*selectors)
189
- selection = Selection.new(nodes)
190
+ nodesets << xml.css(*selectors)
191
+ end
192
+ selection = Selection.new(nodesets)
190
193
 
191
- if block_given? then
192
- Docile.dsl_eval(selection, &block)
193
- end
194
+ if block_given? then
195
+ Docile.dsl_eval(selection, &block)
194
196
  end
197
+
198
+ selection
195
199
  end
196
200
 
197
201
  # Create a DSL replacement template
@@ -205,8 +209,9 @@ module Mixml
205
209
  # Create a XML replacement template
206
210
  #
207
211
  # @return [Template] Replacement template
208
- def xml
209
- Template::Xml.new
212
+ # @param proc [Proc] Lambda to create XML
213
+ def xml(proc)
214
+ Template::Xml.new(proc)
210
215
  end
211
216
 
212
217
  # Execute a script or a block
@@ -223,5 +228,30 @@ module Mixml
223
228
  Docile.dsl_eval(self, &block)
224
229
  end
225
230
  end
231
+
232
+ # Execute block for each node
233
+ #
234
+ # @param selection [Selection] Selected nodes
235
+ # @yield Block to execute for each node
236
+ # @yieldparam node [Nokogiri::XML::Node] Current node
237
+ def node(selection)
238
+ selection.nodesets.each do |nodeset|
239
+ nodeset.each do |node|
240
+ yield node
241
+ end
242
+ end
243
+ end
244
+
245
+ # Execute block for each node set
246
+ #
247
+ # @param selection [Selection] Selected nodes
248
+ # @yield Block to execute for each node set
249
+ # @yieldparam node [Nokogiri::XML::NodeSet] Current node set
250
+ def nodes(selection, &block)
251
+ selection.nodesets.each do |nodeset|
252
+ yield nodeset
253
+ end
254
+ end
255
+
226
256
  end
227
257
  end
@@ -1,4 +1,4 @@
1
1
  module Mixml
2
2
  # Current version
3
- VERSION = '0.0.3'
3
+ VERSION = '0.9'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: '0.9'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jochen Seeber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-15 00:00:00.000000000 Z
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander
@@ -84,16 +84,16 @@ dependencies:
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ~>
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '10.3'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ~>
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '10.3'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: qed
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ~>
137
137
  - !ruby/object:Gem::Version
138
138
  version: '1.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: equivalent-xml
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ~>
144
+ - !ruby/object:Gem::Version
145
+ version: '0.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ~>
151
+ - !ruby/object:Gem::Version
152
+ version: '0.5'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: yard
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -151,20 +165,20 @@ dependencies:
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0.8'
153
167
  - !ruby/object:Gem::Dependency
154
- name: yard-qed
168
+ name: coveralls
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
171
  - - ~>
158
172
  - !ruby/object:Gem::Version
159
- version: '0.1'
173
+ version: '0.7'
160
174
  type: :development
161
175
  prerelease: false
162
176
  version_requirements: !ruby/object:Gem::Requirement
163
177
  requirements:
164
178
  - - ~>
165
179
  - !ruby/object:Gem::Version
166
- version: '0.1'
167
- description: Easily manipulate multiple XML files at once
180
+ version: '0.7'
181
+ description: Never use XSLT again...
168
182
  email:
169
183
  - jochen@seeber.me
170
184
  executables:
@@ -172,7 +186,12 @@ executables:
172
186
  extensions: []
173
187
  extra_rdoc_files: []
174
188
  files:
189
+ - .yardopts
190
+ - LICENSE.txt
191
+ - README.md
175
192
  - bin/mixml
193
+ - demo/applique/test.rb
194
+ - demo/tool.md
176
195
  - lib/mixml.rb
177
196
  - lib/mixml/document.rb
178
197
  - lib/mixml/selection.rb