mixml 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTM2NTgxNmFjNmU2MDFjMmZjMDU2YjZjOGMwYWQyMjFkZDc0ODIzOA==
4
+ OTc1ZjA5ZjljNGJjMjFiMTk3MjNmMmRiMzEwYzY4NmRlZmJhZmFjNA==
5
5
  data.tar.gz: !binary |-
6
- ZTBiMzIyZjljMzlhMzk5NzlkMjcxNmRlMjkwOTlmZGZlNzEwNTY3Yw==
6
+ MTJkMDc3NDBhNmJhYzVmZDU1ODNmZjgzYWRmMGYyZjQ0NmFjNzU5ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjJiMzU4YWI2M2YzMmFhYmRjY2JkNmZlMTk5YTk1NzNiZjI0ZWFjOGU5ZmI4
10
- ZWQxNDlmMDQ2YjFhNDIwMTgxODI0YWE5YThlODIzOTgyYWVmN2IzYmEzMjhh
11
- MjFlOGQyZjBjN2MxMjExMDEyZjhkODRjODVlNjRmNmYwMzFlMDg=
9
+ YzA0NzY5MDBkMDM1N2NkMTAwNTM4YmM1NThjNDhiNzgzYjVhYTY1NmE0Zjhl
10
+ Yzg1YWRhNGI3YjI4Y2NmZjE5MzE0YTM0ODA2MDY3ZWZiNWRlZTgzNzNkOTJl
11
+ ZTcwOGEyYzBhNWUyYjVkNjViYWNlYmU2MDcxN2UyMjUxY2FmZjA=
12
12
  data.tar.gz: !binary |-
13
- NTQxNjM5NzQyNmExOGI2ZTlhOTgwMDg2MTdmYTE2ZjM4ZGM2N2ZmNzEyMGY4
14
- NmQ0OGUyN2NiODM4MGUxNDY5Mzg4ZTg2MWVlODhjYjQxZTQ4ZWY5MTdlYWQx
15
- MTMyZTIxYmU0MmEzNTIzYzFiYzc2YjVkYjRkZGRiN2Y3NDQ5MGE=
13
+ NzYyOTc4NzQ3NTE4ZDZiOGYwZjMzMWQ3M2U3NmNkMDhjODA2ZWY2YzVkNjU1
14
+ MDMxZDUyMTViY2VjZGMyYjc2NjUzOWJlOWZhMTI0MGFiM2Q1MWY4NjZjZjQ4
15
+ NmZlYWIxYWJmZTY1ZDY4YzQ0ZDIyMzQ5ODk3OWM3ZDMwZDU4MmQ=
data/bin/mixml CHANGED
@@ -16,6 +16,7 @@ end
16
16
 
17
17
  global_option('-i', '--inplace', 'Replace the processed files with the new files') do |value|
18
18
  $tool.save = value
19
+ $tool.print = !value
19
20
  end
20
21
 
21
22
  command :pretty do |c|
@@ -38,6 +39,19 @@ command :remove do |c|
38
39
  end
39
40
  end
40
41
 
42
+ command :replace do |c|
43
+ c.description = 'Rename nodes in the XML documents'
44
+ c.option '-x', '--xpath STRING', String, 'XPath for nodes to replace'
45
+ c.option '-t', '--template STRING', String, 'Template to generate the replacement value'
46
+ c.action do |args, options|
47
+ $tool.work(args) do
48
+ xpath options.xpath do
49
+ rename template options.template
50
+ end
51
+ end
52
+ end
53
+ end
54
+
41
55
  command :replace do |c|
42
56
  c.description = 'Replace nodes from the XML documents'
43
57
  c.option '-x', '--xpath STRING', String, 'XPath for nodes to replace'
@@ -1,10 +1,16 @@
1
1
 
2
2
 
3
3
  module Mixml
4
+ # Loaded XML document
4
5
  class Document
6
+ # @return [String] Document name
5
7
  attr_reader :name
8
+
9
+ # @return [String] XML document
6
10
  attr_reader :xml
7
11
 
12
+ # @param name [String] Document name
13
+ # @param name [Nokigiri::XML::Document] XML document
8
14
  def initialize(name, xml)
9
15
  @name = name
10
16
  @xml = xml
@@ -1,21 +1,27 @@
1
- require 'mixml/template/text'
1
+ require 'mixml'
2
+ require 'mixml/template'
2
3
 
3
4
  module Mixml
5
+ # Selection of XML nodes
4
6
  class Selection
7
+ # @return [Nokogiri::XML::NodeSet] Selected nodes
5
8
  attr_reader :nodes
6
9
 
10
+ # @param nodes [Nokogiri::XML::NodeSet] Selected nodes
7
11
  def initialize(nodes)
8
12
  @nodes = nodes
9
13
  end
10
14
 
15
+ # Remove selected nodes from the document
11
16
  def remove
12
17
  @nodes.remove
13
18
  end
14
19
 
20
+ # Replace selected nodes with a template
21
+ #
22
+ # @param template [Template::Base] Template to replace nodes with
15
23
  def replace(template)
16
- if template.is_a?(String) then
17
- template = Template::Text.new(template)
18
- end
24
+ template = template.to_mixml_template
19
25
 
20
26
  @nodes.each do |node|
21
27
  value = template.evaluate(node)
@@ -23,15 +29,28 @@ module Mixml
23
29
  end
24
30
  end
25
31
 
32
+ # Set the value selected nodes with a template
33
+ #
34
+ # @param template [Template::Base] Template to set the value
26
35
  def value(template)
27
- if template.is_a?(String) then
28
- template = Template::Text.new(template)
29
- end
36
+ template = template.to_mixml_template
30
37
 
31
38
  @nodes.each do |node|
32
39
  value = template.evaluate(node)
33
40
  node.value = value
34
41
  end
35
42
  end
43
+
44
+ # Rename selected nodes with a template
45
+ #
46
+ # @param template [Template::Base] Template for new name
47
+ def rename(template)
48
+ template = template.to_mixml_template
49
+
50
+ @nodes.each do |node|
51
+ value = template.evaluate(node)
52
+ node.name = value
53
+ end
54
+ end
36
55
  end
37
56
  end
@@ -0,0 +1,11 @@
1
+ require 'mixml/template/text'
2
+
3
+ # Extend String
4
+ class String
5
+ # Convert a string into a template
6
+ #
7
+ # @return [Mixml::Template::Text] String as template
8
+ def to_mixml_template
9
+ ::Mixml::Template::Text.new(self)
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Mixml
2
+ module Template
3
+ # Base class for templates
4
+ class Base
5
+ # Convert into a template
6
+ #
7
+ # This is done by the complex and intricate process of returning `self`.
8
+ #
9
+ # @return [Mixml::Template::Base] Return self
10
+ def to_mixml_template
11
+ self
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,18 +1,28 @@
1
+ require 'mixml/template/base'
1
2
  require 'erubis'
2
3
 
3
4
  module Mixml
5
+ # Module for template classes
4
6
  module Template
5
- class Template
6
- attr_reader :text
7
+ # Eruby based template
8
+ class Expression < Base
9
+ # @return [Erubis::Eruby] Template expression
10
+ attr_reader :expression
7
11
 
12
+ # Initialize a new template
13
+ #
14
+ # @param [String] text Template text
8
15
  def initialize(text)
9
- @text = text
16
+ @expression = Erubis::Eruby.new(text, :pattern => '{ }')
10
17
  end
11
18
 
19
+ # Evaluate the template
20
+ #
21
+ # @param [Nokogiri::XML::Node] node Current node
22
+ # @return [String] Template result
12
23
  def evaluate(node)
13
- template = Erubis::Eruby.new(@text, :pattern => '{ }')
14
24
  context = {:node => node}
15
- template.result(context)
25
+ @expression.result(context)
16
26
  end
17
27
  end
18
28
  end
@@ -1,14 +1,24 @@
1
+ require 'mixml/template/base'
2
+
1
3
  module Mixml
2
4
  module Template
3
- class Text
5
+ # Ruby string template
6
+ class Text < Base
7
+ # @return [String] Template text
4
8
  attr_reader :text
5
9
 
10
+ # Initialize a new template
11
+ #
12
+ # @param text [String] Template text
6
13
  def initialize(text)
7
- @text = text
14
+ @text = '"' << text.gsub('"', '\"') << '"'
8
15
  end
9
16
 
17
+ # Evaulate the template using Ruby string interpolation
18
+ #
19
+ # @param node [Nokogiri::XML::Node] Current node
10
20
  def evaluate(node)
11
- @text
21
+ eval(@text, binding)
12
22
  end
13
23
  end
14
24
  end
@@ -4,20 +4,44 @@ require 'mixml/template/template'
4
4
  require 'docile'
5
5
  require 'nokogiri'
6
6
 
7
+ # Mixml main module
7
8
  module Mixml
9
+ # Mixml tool
10
+ #
11
+ # This is the main class for using mixml.
8
12
  class Tool
13
+ # @return [Array<Document>] loaded XML Documents
9
14
  attr_reader :documents
15
+
16
+ # @return [Boolean] pretty print XML during output
10
17
  attr_accessor :pretty
18
+
19
+ # @return [Boolean] save processed XML documents, defaults to false
11
20
  attr_accessor :save
21
+
22
+ # @return [Boolean] print processed XML documents, defaults to true
23
+ attr_accessor :print
24
+
25
+ # @return [Integer] indent width, defaults to 4
12
26
  attr_accessor :indent
13
27
 
14
- def initialize
28
+ # Intialize a new mixml tool
29
+ def initialize(&block)
15
30
  @indent = 4
16
31
  @pretty = false
17
32
  @save = false
33
+ @print = true
18
34
  @documents = []
35
+
36
+ if block_given? then
37
+ yield self
38
+ end
19
39
  end
20
40
 
41
+ # Load XML files
42
+ #
43
+ # @param file_names [Array] Names of the XML files to load
44
+ # @return [void]
21
45
  def load(*file_names)
22
46
  file_names.flatten.each do |file_name|
23
47
  xml = File.open(file_name, 'r') do |file|
@@ -31,6 +55,11 @@ module Mixml
31
55
  end
32
56
  end
33
57
 
58
+ # Save all loaded XML files
59
+ #
60
+ # Pretty prints the XML if {#pretty} is enabled.
61
+ #
62
+ # @return [void]
34
63
  def save_all
35
64
  options = {}
36
65
 
@@ -45,6 +74,12 @@ module Mixml
45
74
  end
46
75
  end
47
76
 
77
+ # Print all loaded XML files
78
+ #
79
+ # Pretty prints the XML if {#pretty} is enabled. If more than one file is loaded, a header with the file's name
80
+ # is printed before each file.
81
+ #
82
+ # @return [void]
48
83
  def print_all
49
84
  options = {}
50
85
 
@@ -62,20 +97,43 @@ module Mixml
62
97
  end
63
98
  end
64
99
 
100
+ # Remove all loaded XML files
101
+ #
102
+ # Files are not saved before removing them.
103
+ #
104
+ # @return [void]
65
105
  def remove_all
66
106
  @documents = []
67
107
  end
68
108
 
109
+ # Print/save all loaded XML files and then remove them
110
+ #
111
+ # Files are saved if {#save} is enabled, and they are printed to the console if {#print} is enabled.
112
+ #
113
+ # @return [void]
69
114
  def flush
115
+ if @print then
116
+ print_all
117
+ end
118
+
70
119
  if @save then
71
120
  save_all
72
- else
73
- print_all
74
121
  end
75
122
 
76
123
  remove_all
77
124
  end
78
125
 
126
+ # Perform work on a list of XML files
127
+ #
128
+ # Perform the following steps:
129
+ # #. Remove all loaded XML files without saving them
130
+ # #. Load the supplied XML files
131
+ # #. Execute the supplied block
132
+ # #. Flush all XML files
133
+ #
134
+ # @param file_names [Array] Names of the XML files to load
135
+ # @yield Block to execute with loaded XML files
136
+ # @return [void]
79
137
  def work(*file_names, &block)
80
138
  remove_all
81
139
 
@@ -90,12 +148,23 @@ module Mixml
90
148
  flush
91
149
  end
92
150
 
151
+ # Execute a block for each loaded XML document
152
+ #
153
+ # @yield Block to execute for each XML document
154
+ # @yieldparam xml {Nokogiri::XML::Document} XML document
155
+ # @return [void]
93
156
  def process
94
157
  @documents.each do |document|
95
158
  yield document.xml
96
159
  end
97
160
  end
98
161
 
162
+ # Select nodes using an XPath expression and execute DSL commands for these nodes
163
+ #
164
+ # @param query [String] XPath expression
165
+ # @yield Block to execute for each nodeset
166
+ # @yieldparam nodes {Nokogiri::XML::NodeSet} XML nodes to process
167
+ # @return [void]
99
168
  def xpath(query, &block)
100
169
  process do |xml|
101
170
  nodes = xml.xpath(query)
@@ -107,10 +176,19 @@ module Mixml
107
176
  end
108
177
  end
109
178
 
179
+ # Create a DSL replacement template
180
+ #
181
+ # @param text [String] Template text
182
+ # @return [Template] Replacement template
110
183
  def template(text)
111
- Template::Template.new(text)
184
+ Template::Expression.new(text)
112
185
  end
113
186
 
187
+ # Execute a script or a block
188
+ #
189
+ # @param program [String] DSL script to execute
190
+ # @yield Block to execute
191
+ # @return [void]
114
192
  def execute(program = nil, &block)
115
193
  if not program.nil? then
116
194
  instance_eval(program)
@@ -1,3 +1,4 @@
1
1
  module Mixml
2
- VERSION = '0.0.1'
2
+ # Current version
3
+ VERSION = '0.0.2'
3
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.1
4
+ version: 0.0.2
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-14 00:00:00.000000000 Z
11
+ date: 2014-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander
@@ -108,7 +108,49 @@ dependencies:
108
108
  - - ~>
109
109
  - !ruby/object:Gem::Version
110
110
  version: '2.9'
111
- description: XML tool to easily manipulate multiple XML files at once
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-expectations
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-collection_matchers
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: '1.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: '1.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ~>
144
+ - !ruby/object:Gem::Version
145
+ version: '0.8'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ~>
151
+ - !ruby/object:Gem::Version
152
+ version: '0.8'
153
+ description: Easily manipulate multiple XML files at once
112
154
  email:
113
155
  - jochen@seeber.me
114
156
  executables:
@@ -120,14 +162,20 @@ files:
120
162
  - lib/mixml.rb
121
163
  - lib/mixml/document.rb
122
164
  - lib/mixml/selection.rb
165
+ - lib/mixml/template.rb
166
+ - lib/mixml/template/base.rb
123
167
  - lib/mixml/template/template.rb
124
168
  - lib/mixml/template/text.rb
125
169
  - lib/mixml/tool.rb
126
170
  - lib/mixml/version.rb
127
- homepage: http://www.noussommesdesoles.net
171
+ homepage: https://github.com/jochenseeber/mixml
128
172
  licenses:
129
173
  - AGPL-3.0
130
- metadata: {}
174
+ metadata:
175
+ issue_tracker: https://github.com/jochenseeber/mixml/issues
176
+ source_code: https://github.com/jochenseeber/mixml
177
+ documentation: http://rubydoc.info/gems/mixml/frames
178
+ wiki: https://github.com/jochenseeber/mixml/wiki
131
179
  post_install_message:
132
180
  rdoc_options: []
133
181
  require_paths:
@@ -147,6 +195,6 @@ rubyforge_project:
147
195
  rubygems_version: 2.2.2
148
196
  signing_key:
149
197
  specification_version: 4
150
- summary: XML tool to easily manipulate multiple XML files at once
198
+ summary: Easily manipulate multiple XML files at once
151
199
  test_files: []
152
200
  has_rdoc: