mixml 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTc1ZjA5ZjljNGJjMjFiMTk3MjNmMmRiMzEwYzY4NmRlZmJhZmFjNA==
4
+ MTI4M2NkNWIxNjMwMTk5OTMwZmNkN2I2ZThjYjQ4MTFiOWExMmU3YQ==
5
5
  data.tar.gz: !binary |-
6
- MTJkMDc3NDBhNmJhYzVmZDU1ODNmZjgzYWRmMGYyZjQ0NmFjNzU5ZA==
6
+ NGM0ZDJjN2FlYTdiMmY5ZTk3YjZhOGIyMjllYzM3NTZhMzBjZTQ0Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzA0NzY5MDBkMDM1N2NkMTAwNTM4YmM1NThjNDhiNzgzYjVhYTY1NmE0Zjhl
10
- Yzg1YWRhNGI3YjI4Y2NmZjE5MzE0YTM0ODA2MDY3ZWZiNWRlZTgzNzNkOTJl
11
- ZTcwOGEyYzBhNWUyYjVkNjViYWNlYmU2MDcxN2UyMjUxY2FmZjA=
9
+ NzRhNTlhNTkyZTRlYjM4ZTUwNzFjNDRlODY0ZWMyZjM1MGFkZWEzM2IyOWFi
10
+ MTJiYWFmOTY2OWY4NWEyZWJhMjE4MWU0MDc4N2QwZmIyNTNlNzQxNDcwZTc3
11
+ MzExMTNiNjFhMGRkZmFhODJmYjkwZDM0MWI0MDcxZGY5OTFhNWI=
12
12
  data.tar.gz: !binary |-
13
- NzYyOTc4NzQ3NTE4ZDZiOGYwZjMzMWQ3M2U3NmNkMDhjODA2ZWY2YzVkNjU1
14
- MDMxZDUyMTViY2VjZGMyYjc2NjUzOWJlOWZhMTI0MGFiM2Q1MWY4NjZjZjQ4
15
- NmZlYWIxYWJmZTY1ZDY4YzQ0ZDIyMzQ5ODk3OWM3ZDMwZDU4MmQ=
13
+ NDkzYTA4ZGM1ZDQwOTQxYjE5MWVlZjBiY2U4YTY1NjlmNzgzZWQxMjU1YWM4
14
+ MTkxMjJhNDFlZWMyOGRlODA5MjJhZWM1NDY0ZTdiZDI0M2Q3ZjBiMjRkMmQw
15
+ OTY4MGM1YWJjNmZjNjY1OWVkYTczYTM3YjgxODJlZjU0ZDJlOWI=
data/bin/mixml CHANGED
@@ -52,6 +52,19 @@ command :replace do |c|
52
52
  end
53
53
  end
54
54
 
55
+ command :append do |c|
56
+ c.description = 'Append child nodes in the XML documents'
57
+ c.option '-x', '--xpath STRING', String, 'XPath for nodes to append to'
58
+ c.option '-t', '--template STRING', String, 'Template to generate the nodes to append'
59
+ c.action do |args, options|
60
+ $tool.work(args) do
61
+ xpath options.xpath do
62
+ append template options.template
63
+ end
64
+ end
65
+ end
66
+ end
67
+
55
68
  command :replace do |c|
56
69
  c.description = 'Replace nodes from the XML documents'
57
70
  c.option '-x', '--xpath STRING', String, 'XPath for nodes to replace'
@@ -10,7 +10,7 @@ module Mixml
10
10
  attr_reader :xml
11
11
 
12
12
  # @param name [String] Document name
13
- # @param name [Nokigiri::XML::Document] XML document
13
+ # @param xml [Nokigiri::XML::Document] XML document
14
14
  def initialize(name, xml)
15
15
  @name = name
16
16
  @xml = xml
@@ -20,15 +20,27 @@ module Mixml
20
20
  # Replace selected nodes with a template
21
21
  #
22
22
  # @param template [Template::Base] Template to replace nodes with
23
- def replace(template)
23
+ def replace(template = nil, &block)
24
24
  template = template.to_mixml_template
25
25
 
26
26
  @nodes.each do |node|
27
- value = template.evaluate(node)
27
+ value = template.evaluate(node, &block)
28
28
  node.replace(value)
29
29
  end
30
30
  end
31
31
 
32
+ # Append children to node
33
+ #
34
+ # @param template [Template::Base] Template to replace nodes with
35
+ def append(template = nil, &block)
36
+ template = template.to_mixml_template
37
+
38
+ @nodes.each do |node|
39
+ value = template.evaluate(node, &block)
40
+ node << value
41
+ end
42
+ end
43
+
32
44
  # Set the value selected nodes with a template
33
45
  #
34
46
  # @param template [Template::Base] Template to set the value
File without changes
@@ -0,0 +1,21 @@
1
+ require 'mixml/template/base'
2
+ require 'erubis'
3
+
4
+ module Mixml
5
+ # Module for template classes
6
+ module Template
7
+ # XML builder based template
8
+ class Xml < Base
9
+ # Evaluate the template
10
+ #
11
+ # @param [Nokogiri::XML::Node] node Current node
12
+ # @return [String] Template result
13
+ def evaluate(node)
14
+ builder = Nokogiri::XML::Builder.new do |xml|
15
+ yield(node, xml)
16
+ end
17
+ builder.to_xml
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/mixml/tool.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'mixml/selection'
2
2
  require 'mixml/document'
3
- require 'mixml/template/template'
3
+ require 'mixml/template/expression'
4
+ require 'mixml/template/xml'
4
5
  require 'docile'
5
6
  require 'nokogiri'
6
7
 
@@ -176,6 +177,23 @@ module Mixml
176
177
  end
177
178
  end
178
179
 
180
+ # Select nodes using CSS selectors and execute DSL commands for these nodes
181
+ #
182
+ # @param selectors [String] CSS selectors
183
+ # @yield Block to execute for each nodeset
184
+ # @yieldparam nodes {Nokogiri::XML::NodeSet} XML nodes to process
185
+ # @return [void]
186
+ def css(*selectors, &block)
187
+ process do |xml|
188
+ nodes = xml.css(*selectors)
189
+ selection = Selection.new(nodes)
190
+
191
+ if block_given? then
192
+ Docile.dsl_eval(selection, &block)
193
+ end
194
+ end
195
+ end
196
+
179
197
  # Create a DSL replacement template
180
198
  #
181
199
  # @param text [String] Template text
@@ -184,6 +202,13 @@ module Mixml
184
202
  Template::Expression.new(text)
185
203
  end
186
204
 
205
+ # Create a XML replacement template
206
+ #
207
+ # @return [Template] Replacement template
208
+ def xml
209
+ Template::Xml.new
210
+ end
211
+
187
212
  # Execute a script or a block
188
213
  #
189
214
  # @param program [String] DSL script to execute
data/lib/mixml/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Mixml
2
2
  # Current version
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jochen Seeber
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ~>
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.8'
153
+ - !ruby/object:Gem::Dependency
154
+ name: yard-qed
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ~>
158
+ - !ruby/object:Gem::Version
159
+ version: '0.1'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: '0.1'
153
167
  description: Easily manipulate multiple XML files at once
154
168
  email:
155
169
  - jochen@seeber.me
@@ -164,8 +178,9 @@ files:
164
178
  - lib/mixml/selection.rb
165
179
  - lib/mixml/template.rb
166
180
  - lib/mixml/template/base.rb
167
- - lib/mixml/template/template.rb
181
+ - lib/mixml/template/expression.rb
168
182
  - lib/mixml/template/text.rb
183
+ - lib/mixml/template/xml.rb
169
184
  - lib/mixml/tool.rb
170
185
  - lib/mixml/version.rb
171
186
  homepage: https://github.com/jochenseeber/mixml