mixml 0.9.3 → 0.9.4
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 +8 -8
- data/README.md +5 -0
- data/demo/application_checks.md +2 -2
- data/demo/application_commands.md +38 -0
- data/lib/mixml/application.rb +50 -11
- data/lib/mixml/selection.rb +20 -2
- data/lib/mixml/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
N2ZmMjE2Y2Q1OWE4NjA3YmM1ZjcwYWU2YTRmYzg1MDNlNjI5ZTk3MA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Y2M0NTNhOWJlYWRlN2JjNTRiNTc2MjcyMDQzMDlkM2M1YTFkZTBlNA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGU2MzhhMzNlZTA3NDMwMmU3MzJkYWExNDI3MzFjMWZkYjM0Njg4MmI0YmVm
|
10
|
+
ZGYyM2EwNThhMGY1MWRiODk2NGI4MmVhNjNkN2Q1ZWQxNTVmYzdjN2QwNTE0
|
11
|
+
NWFlMjZjMjY2MWZmYWZlZWI4YTg1Mzc4ZDZiNDAxMWEwOTcxZWQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjBiMjQzMDYyMDdhOTEyYzEyMDg0MjBjOTBlMmQwM2JmYjZkMmU2N2JjZTdi
|
14
|
+
NGM4ZjhkMDc1MjhiMWViYzc3NDYzZWYzY2IzN2ZhN2M2ODY5NDBkOGFlNTlh
|
15
|
+
MTRlN2Y1YTZlOGU0ZWExZjE5MTdmMTJlOTE0MTJhYjQ5M2NmODA=
|
data/README.md
CHANGED
@@ -13,6 +13,7 @@ to spare me from having to use XSLT ever again.
|
|
13
13
|
You can use mixml to change XML files in the following ways:
|
14
14
|
|
15
15
|
* Pretty print
|
16
|
+
* Print selected nodes and attributes to the console
|
16
17
|
* Remove nodes
|
17
18
|
* Add nodes
|
18
19
|
* Replace nodes
|
@@ -28,6 +29,10 @@ The following command will pretty print all XML files:
|
|
28
29
|
|
29
30
|
mixml pretty --inplace *.xml
|
30
31
|
|
32
|
+
The following command will print all attribute names and values:
|
33
|
+
|
34
|
+
mixml write --xpath '//@*' --template '{=node.name}={=node.value}' test.xml
|
35
|
+
|
31
36
|
You can find a description of all supported commands and usage examples [here](demo/application_commands.md).
|
32
37
|
|
33
38
|
## Selection
|
data/demo/application_checks.md
CHANGED
@@ -20,8 +20,8 @@ Executing mixml to modify documents with multiple value types fails:
|
|
20
20
|
|
21
21
|
# mixml rename --xpath '//philosopher' --string 'tiger' --template 'scholar'
|
22
22
|
|
23
|
-
## Command without
|
23
|
+
## Command without value fails
|
24
24
|
|
25
25
|
Executing mixml to modify documents without a selector fails:
|
26
26
|
|
27
|
-
# mixml rename --
|
27
|
+
# mixml rename --xpath '//philosopher'
|
@@ -22,6 +22,44 @@ This produces the following text output:
|
|
22
22
|
<philosopher name="Rawls"/>
|
23
23
|
</list>
|
24
24
|
|
25
|
+
## Write nodes
|
26
|
+
|
27
|
+
You can use mixml to print selected nodes from your XML documents.
|
28
|
+
|
29
|
+
Let's use the following XML in file `test.xml`:
|
30
|
+
|
31
|
+
<list>
|
32
|
+
<philosopher name="Hobbes"/>
|
33
|
+
<philosopher name="Rawls"/>
|
34
|
+
</list>
|
35
|
+
|
36
|
+
Now execute the following command to select some nodes and remove them:
|
37
|
+
|
38
|
+
# mixml write --quiet --xpath '//philosopher/@name' test.xml
|
39
|
+
|
40
|
+
This produces the following text output:
|
41
|
+
|
42
|
+
name="Hobbes"
|
43
|
+
name="Rawls"
|
44
|
+
|
45
|
+
You can also supply an expression that used to create the output:
|
46
|
+
|
47
|
+
Let's use the following XML in file `test.xml`:
|
48
|
+
|
49
|
+
<list>
|
50
|
+
<philosopher name="Hobbes"/>
|
51
|
+
<philosopher name="Rawls"/>
|
52
|
+
</list>
|
53
|
+
|
54
|
+
Now execute the following command to select some nodes and remove them:
|
55
|
+
|
56
|
+
# mixml write --quiet --xpath '//philosopher/@name' --template '{=node.name}={=node.value}' test.xml
|
57
|
+
|
58
|
+
This produces the following text output:
|
59
|
+
|
60
|
+
name=Hobbes
|
61
|
+
name=Rawls
|
62
|
+
|
25
63
|
## Remove nodes
|
26
64
|
|
27
65
|
You can use mixml to remove nodes from your XML documents.
|
data/lib/mixml/application.rb
CHANGED
@@ -9,6 +9,9 @@ module Mixml
|
|
9
9
|
|
10
10
|
# Command that selects nodes
|
11
11
|
class SelectCommand < Commander::Command
|
12
|
+
# @return [Boolean] Suppress automatic output of result
|
13
|
+
attr_accessor :suppress_output
|
14
|
+
|
12
15
|
# Initialize a new command
|
13
16
|
#
|
14
17
|
# @param method [Symbol] Command method
|
@@ -31,25 +34,49 @@ module Mixml
|
|
31
34
|
}
|
32
35
|
end
|
33
36
|
|
34
|
-
when_called self, :
|
37
|
+
when_called self, :execute
|
35
38
|
end
|
36
39
|
|
37
40
|
# Run the command
|
38
41
|
#
|
39
42
|
# @param args [Array<String>] Arguments from the command line
|
40
43
|
# @param options [Commander::Command::Options] Options from the command line
|
41
|
-
def
|
44
|
+
def execute(args, options)
|
45
|
+
before args, options
|
46
|
+
|
47
|
+
if @suppress_output then
|
48
|
+
$tool.print = false
|
49
|
+
$tool.save = false
|
50
|
+
end
|
51
|
+
|
42
52
|
$tool.work(args) do
|
43
53
|
@selectors.each do |selector|
|
44
54
|
selection = instance_eval(&selector)
|
45
|
-
selection.send name
|
55
|
+
selection.send name, *parameters
|
46
56
|
end
|
47
57
|
end
|
48
58
|
end
|
59
|
+
|
60
|
+
# Parameters for command execution
|
61
|
+
#
|
62
|
+
# @return [Array] Parameters
|
63
|
+
def parameters
|
64
|
+
[]
|
65
|
+
end
|
66
|
+
|
67
|
+
# Invoked before the command is executed
|
68
|
+
#
|
69
|
+
# @param args [Array<String>] Arguments from the command line
|
70
|
+
# @param options [Commander::Command::Options] Options from the command line
|
71
|
+
def before(args, options)
|
72
|
+
end
|
49
73
|
end
|
50
74
|
|
51
75
|
# Command that selects and modifies nodes
|
52
76
|
class ModifyCommand < SelectCommand
|
77
|
+
# @return [Boolean] Supplying an expression is optional
|
78
|
+
attr_accessor :optional_expression
|
79
|
+
|
53
80
|
# Initialize a new command
|
54
81
|
#
|
55
82
|
# @param method [Symbol] Command method
|
@@ -58,6 +85,7 @@ module Mixml
|
|
58
85
|
super(method, args)
|
59
86
|
|
60
87
|
@template = nil
|
88
|
+
@optional_expression = false
|
61
89
|
|
62
90
|
option '-s', '--string STRING', String, 'String value' do |value|
|
63
91
|
raise SystemExit, 'Value already specified. Please use --string or --template only once.' unless @template.nil?
|
@@ -70,16 +98,17 @@ module Mixml
|
|
70
98
|
end
|
71
99
|
end
|
72
100
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
@selectors.each do |selector|
|
78
|
-
selection = instance_eval(&selector)
|
79
|
-
selection.send name, @template
|
80
|
-
end
|
101
|
+
# Check if an expression is set
|
102
|
+
def before(args, options)
|
103
|
+
if not @optional_expression and @template.nil? then
|
104
|
+
raise SystemExit, 'Please specify a value with --string or --template.'
|
81
105
|
end
|
82
106
|
end
|
107
|
+
|
108
|
+
# Return the template as parameter
|
109
|
+
def parameters
|
110
|
+
[@template]
|
111
|
+
end
|
83
112
|
end
|
84
113
|
|
85
114
|
# Create a new selection command
|
@@ -115,6 +144,10 @@ module Mixml
|
|
115
144
|
$tool.print = !value
|
116
145
|
end
|
117
146
|
|
147
|
+
global_option('-q', '--quiet', 'Do not print nodes') do |value|
|
148
|
+
$tool.print = !value
|
149
|
+
end
|
150
|
+
|
118
151
|
command :pretty do |c|
|
119
152
|
c.description = 'Pretty print XML files'
|
120
153
|
c.action do |args, options|
|
@@ -123,6 +156,12 @@ module Mixml
|
|
123
156
|
end
|
124
157
|
end
|
125
158
|
|
159
|
+
modify_command :write do |c|
|
160
|
+
c.description = 'Write selected nodes to the console'
|
161
|
+
c.suppress_output = true
|
162
|
+
c.optional_expression = true
|
163
|
+
end
|
164
|
+
|
126
165
|
select_command :remove do |c|
|
127
166
|
c.description = 'Remove nodes from the XML documents'
|
128
167
|
end
|
data/lib/mixml/selection.rb
CHANGED
@@ -16,8 +16,26 @@ module Mixml
|
|
16
16
|
|
17
17
|
# Remove selected nodes from the document
|
18
18
|
def remove
|
19
|
-
each_node do |
|
20
|
-
|
19
|
+
each_node do |node|
|
20
|
+
node.remove
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Print selected nodes to stdout
|
25
|
+
#
|
26
|
+
# @param template [Template::Base] Template to evaluate and print
|
27
|
+
def write(template = nil)
|
28
|
+
if not template.nil? then
|
29
|
+
template = template.to_mixml_template
|
30
|
+
end
|
31
|
+
|
32
|
+
each_node do |node|
|
33
|
+
if template.nil? then
|
34
|
+
node.write_xml_to($stdout)
|
35
|
+
puts
|
36
|
+
else
|
37
|
+
puts template.evaluate(node)
|
38
|
+
end
|
21
39
|
end
|
22
40
|
end
|
23
41
|
|
data/lib/mixml/version.rb
CHANGED
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.9.
|
4
|
+
version: 0.9.4
|
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-
|
11
|
+
date: 2014-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: commander
|