mixml 0.9.1 → 0.9.3

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
- NWIzODcxYzM4YTQzNWQ5MTBkMDZmODAwYmNjYTRjMmFlMjZiNTUwMQ==
4
+ YzAyODY3MmNkMjgyNWM1ZGYzOGNkZTZjNjc3MDAyZjNiYzE0MWUxMA==
5
5
  data.tar.gz: !binary |-
6
- NzJmMTMzYjJhN2ExZTE4MjYxNzQ3OTdjYTI5YjY4ZWFmYzRkN2NiNA==
6
+ MjlhOTU2MWZjMDdlYjlkODI3ZDRiZDkwMWUyYjYyOThhMTM2YTNjNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjY1N2JlY2RiOWNhZTQyMGNhZmRiN2U4M2Y1MDU2ODg3YWQ1ZTg4YjMzNjYy
10
- NzhmNzVkYzk3N2FhY2YwNThmNjFlMTM1MzlkNjUwMmE3OTEzOWY3Y2I0ZGQ1
11
- NWE0MzM3MWU0OTE0YTRkZGI0MGJiMGM0ZGZhN2QxNmI1NTlhMWQ=
9
+ NTBlYThlYmM1MjVhMzFhZmI3OTRkOGVhMmE5ZTY2ZTM2YzYyOTI5YTA5YWUx
10
+ MTI4NDU4MDIyYjlkNDY4MTNkNTI3OTQ5YzIwZDZhYWNiMDNlNDgyMzA0ZjVi
11
+ ZjJmZjI5ODJiMTFlMjlmODcwN2E4YTEyNmEyZGQ1OTE5NDUzMWE=
12
12
  data.tar.gz: !binary |-
13
- YTc3ZjA3YzM4YjViNTRkYTg1MzgwMzljZmMwZWU2ODEzOWIyNjZhNTVhM2Mx
14
- YWYwNjBkNmYxM2M3MGIzMGQ3NDRjZjE4YWI5OTNhNjZlYjA2NTM0N2NiZmZi
15
- NzBhODAwNGJmZTBjYWM3Y2ZiMWQ4YmMzODQ3ZTBmZWJiMGY3ODE=
13
+ NDczODMwYzhjNmVjYTYxMTdlZjA5NzhkYzA4YzhmYjg3YjVmZGRmMGZlNWJh
14
+ N2M5OTI0ZmFhZDQ1MTUzYTUyOGQwMDQ1ZWQ3ZjNmODdjZDFiOTNlZmI5NTFl
15
+ ZGUxYTc3MTIzNTIxOTExNWNlZmM1MGUxOGFjYmNlN2FhM2QyYWY=
data/README.md CHANGED
@@ -3,9 +3,14 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/mixml.png)](http://badge.fury.io/rb/mixml)
4
4
  [![Build Status](https://travis-ci.org/jochenseeber/mixml.png?branch=master)](https://travis-ci.org/jochenseeber/mixml)
5
5
  [![Coverage Status](https://coveralls.io/repos/jochenseeber/mixml/badge.png?branch=master)](https://coveralls.io/r/jochenseeber/mixml?branch=master)
6
+ [![Inline docs](http://inch-ci.org/github/jochenseeber/mixml.png?branch=master)](http://inch-ci.org/github/jochenseeber/mixml)
6
7
 
7
- Mixml is a small tool to greatly simplify the tedious task of changing multiple multiple XML files at once. Its main
8
- purpose is to spare me from having to use XSLT ever again. You can use mixml to change XML files in the following ways:
8
+ Mixml is a small tool to greatly simplify the tedious task of changing multiple XML files at once. Its main purpose is
9
+ to spare me from having to use XSLT ever again.
10
+
11
+ ## Usage
12
+
13
+ You can use mixml to change XML files in the following ways:
9
14
 
10
15
  * Pretty print
11
16
  * Remove nodes
@@ -14,151 +19,62 @@ purpose is to spare me from having to use XSLT ever again. You can use mixml to
14
19
  * Rename nodes
15
20
  * Change node values
16
21
 
17
- For example, the following command will remove all attributes named `id` from the supplied XML files:
22
+ For example, the following command will remove all attributes named `id` from the all XML files in the current
23
+ directory.
18
24
 
19
25
  mixml remove --inplace --xpath '//@id' *.xml
20
26
 
21
- Mixml also supports a simple DSL to perform scripted changes. To perform the same as above using a script, save the
22
- following in `test.mixml`:
23
-
24
- xpath '//@id' do
25
- remove
26
- end
27
-
28
- and then call:
29
-
30
- mixml execute --script test.mixml *.xml
31
-
32
- You can also use mixml directly in your Ruby code:
33
-
34
- require 'mixml'
35
-
36
- tool = Mixml::Tool.new do |t|
37
- t.save = true
38
- end
39
-
40
- tool.work('one.xml', 'two.xml') do
41
- xpath '//@id' do
42
- remove
43
- end
44
- end
45
-
46
- Mixml supports building replacement values using
47
-
48
- * [Ruby string interpolation](http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation)
49
- * [Erubis templates](http://www.kuwata-lab.com/erubis/)
50
- * [Nokogiri's XML DSL](http://nokogiri.org/Nokogiri/XML/Builder.html)
51
- * Plain Ruby
52
-
53
- You can find more usage examples [here](demo/tool.md).
54
-
55
- ## Installation
56
-
57
- Install mixml:
58
-
59
- $ gem install mixml
60
-
61
- ## Usage
62
-
63
- Use the following command to get help:
27
+ The following command will pretty print all XML files:
64
28
 
65
- mixml --help
29
+ mixml pretty --inplace *.xml
66
30
 
67
- ### Pretty print XML
31
+ You can find a description of all supported commands and usage examples [here](demo/application_commands.md).
68
32
 
69
- mixml pretty *.xml
33
+ ## Selection
70
34
 
71
- ### Remove nodes
35
+ In addition to using XPath expressions, you can also use CSS rules to select the nodes you want to process. For
36
+ example, the following command will remove the first child of each element:
72
37
 
73
- mixml remove --xpath '//addresses' *.xml
38
+ mixml remove --inplace --css '*:first-child' *.xml
74
39
 
75
- ### Rename nodes
40
+ You can find a detailed description and usage examples [here](demo/application_selection.md).
76
41
 
77
- mixml rename --xpath '//addresses' --template 'addressbook' *.xml
42
+ ## Scripts
78
43
 
79
- ### Replace nodes
44
+ In addition to the command line, mixml also supports a simple simple DSL to perform scripted changes. For example, to
45
+ perform the same as above using a script, save the following in `test.mixml`:
80
46
 
81
- mixml replace --xpath '//addresses' --template '<addressbook/>' *.xml
82
-
83
- ### Append nodes
84
-
85
- mixml append --xpath '/list' --template '<addressbook/>' *.xml
86
-
87
- ### Set node value
88
-
89
- mixml value --xpath '//addresses/@name' --template 'default' *.xml
90
-
91
- ### Execute a script
92
-
93
- Example script in `test.mixml`
94
-
95
- xpath '//addresses[name="default"]' do
47
+ xpath '//@id' do
96
48
  remove
97
49
  end
98
- xpath '//addresses' do
99
- replace template '<addressbook/>'
100
- end
101
50
 
102
- Use the following command to run the script
51
+ and then call:
103
52
 
104
53
  mixml execute --script test.mixml *.xml
105
54
 
106
- Script commands:
107
-
108
- xpath 'xpath-expression' do
109
- remove # Remove node
110
- replace 'xml' # Replace node
111
- append 'xml'
112
- value 'text' # Set node value
113
- rename 'text' # Rename node
114
- end
115
-
116
- Instead of using strings for parameters, you can also use a template expression:
117
-
118
- xpath 'xpath-expression' do
119
- replace template 'special-{=node.name}' # Prefix nodes with 'special-''
120
- end
121
-
122
- This works for all commands that take a string parameter. We use [Erubis](http://www.kuwata-lab.com/erubis) as
123
- templating engine, and `{` and `}` as delimiters.
55
+ You can also use mixml directly in your Ruby code. See [here](demo/application_invocation.md) for usage examples.
124
56
 
125
- In addition, you can also use [Nokogiri](http://http://nokogiri.org/)'s
126
- [builder component](http://nokogiri.org/Nokogiri/XML/Builder.html) to create the XML that replaces an element:
57
+ ## Values
127
58
 
128
- xpath '//addresses' do
129
- replace xml ->(node, xml) {
130
- xml.addressbook(:name => node['name'])
131
- }
132
- end
133
-
134
- ### Use CSS rules to select nodes
135
-
136
- You can also use CSS rules instead of XPath expressions to select the nodes to process:
137
-
138
- css 'addresses:first-child', 'addresses:last-child' do
139
- remove
140
- end
141
-
142
- ### Evaluate an expression
143
-
144
- You can also pass the script to execute directly to mixml:
59
+ Mixml supports specifying XML fragments that are to be inserted using
145
60
 
146
- mixml execute --expression 'xpath("//addresses") { remove }' *.xml
147
-
148
- ### Write results
61
+ * [Ruby string interpolation](http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation)
62
+ * [Erubis templates](http://www.kuwata-lab.com/erubis/)
63
+ * [Nokogiri's XML DSL](http://nokogiri.org/Nokogiri/XML/Builder.html)
64
+ * Plain Ruby
149
65
 
150
- The standard setting is to leave the input files unchanged and print the resulting files. You can replace the input
151
- files with the changed XML by using the `inplace` option:
66
+ See [here](demo/application_values.md) for a description of all methods and detailed usage examples.
152
67
 
153
- mixml remove --inplace --xpath '//addresses' test.xml
68
+ ## Output
154
69
 
155
- This will remove all elements named `group` from `test.xml`.
70
+ XML output can be printed to the console or written back to the file it was read from. Mixml also supports pretty
71
+ printing all output. See [here](demo/application_output.md) for a detailed description and usage examples.
156
72
 
157
- ### Pretty print results
73
+ ## Installation
158
74
 
159
- To pretty print the output, use the `pretty` option:
75
+ Install mixml:
160
76
 
161
- mixml remove --inplace --xpath '//addresses' --pretty test.xml
77
+ $ gem install mixml
162
78
 
163
79
  ## Contributing
164
80
 
data/bin/mixml CHANGED
@@ -1,105 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'rubygems'
3
3
  require 'bundler/setup'
4
- require 'commander/import'
5
- require 'mixml'
4
+ require 'mixml/application'
6
5
 
7
- program :name, 'mixml'
8
- program :version, Mixml::VERSION
9
- program :description, 'XML helper tool'
10
-
11
- $tool = Mixml::Tool.new
12
-
13
- global_option('-p', '--pretty', 'Pretty print output') do |value|
14
- $tool.pretty = value
15
- end
16
-
17
- global_option('-i', '--inplace', 'Replace the processed files with the new files') do |value|
18
- $tool.save = value
19
- $tool.print = !value
20
- end
21
-
22
- command :pretty do |c|
23
- c.description = 'Pretty print XML files'
24
- c.action do |args, options|
25
- $tool.pretty = true
26
- $tool.work(args)
27
- end
28
- end
29
-
30
- command :remove do |c|
31
- c.description = 'Remove nodes from the XML documents'
32
- c.option '-x', '--xpath STRING', String, 'XPath for nodes to delete'
33
- c.action do |args, options|
34
- $tool.work(args) do
35
- xpath options.xpath do
36
- remove
37
- end
38
- end
39
- end
40
- end
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
-
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
-
68
- command :replace do |c|
69
- c.description = 'Replace nodes from the XML documents'
70
- c.option '-x', '--xpath STRING', String, 'XPath for nodes to replace'
71
- c.option '-t', '--template STRING', String, 'Template to generate the replacement value'
72
- c.action do |args, options|
73
- $tool.work(args) do
74
- xpath options.xpath do
75
- replace template options.template
76
- end
77
- end
78
- end
79
- end
80
-
81
- command :value do |c|
82
- c.description = 'Set node values'
83
- c.option '-x', '--xpath STRING', String, 'XPath for nodes to change'
84
- c.option '-t', '--template STRING', String, 'Template to generate the replacement value'
85
- c.action do |args, options|
86
- $tool.work(args) do
87
- xpath options.xpath do
88
- value template options.template
89
- end
90
- end
91
- end
92
- end
93
-
94
- command :execute do |c|
95
- c.description = 'Execute script on the XML documents'
96
- c.option '-s', '--script STRING', String, 'Script file to execute'
97
- c.option '-e', '--expression STRING', String, 'Command to execute'
98
- c.action do |args, options|
99
- script = options.expression || File.read(options.script)
100
-
101
- $tool.work(args) do
102
- execute(script)
103
- end
104
- end
105
- end
6
+ # Run application if we're the main file
7
+ Mixml::Application.new.run if $0 == __FILE__
@@ -0,0 +1,27 @@
1
+ # Error checking
2
+
3
+ The following demonstrations test mixml's error checking. All examples on this page are designed to fail.
4
+
5
+ ## Command without selector fails
6
+
7
+ Executing mixml to modify documents without a selector fails:
8
+
9
+ # mixml rename --string 'tiger'
10
+
11
+ ## Command with multiple values fails
12
+
13
+ Executing mixml to modify documents with multiple values fails:
14
+
15
+ # mixml rename --xpath '//philosopher' --string 'tiger' --string 'scholar'
16
+
17
+ ## Command with multiple value types fails
18
+
19
+ Executing mixml to modify documents with multiple value types fails:
20
+
21
+ # mixml rename --xpath '//philosopher' --string 'tiger' --template 'scholar'
22
+
23
+ ## Command without selector fails
24
+
25
+ Executing mixml to modify documents without a selector fails:
26
+
27
+ # mixml rename --string 'tiger'
@@ -0,0 +1,133 @@
1
+ # mixml Commands
2
+
3
+ This document demonstrates the different mixml commands to modify XML documents.
4
+
5
+ ## Pretty print
6
+
7
+ You can use mixml to pretty print your XML documents.
8
+
9
+ Let's use the following XML in file `test.xml`:
10
+
11
+ <list><philosopher name="Hobbes"/><philosopher name="Rawls"/></list>
12
+
13
+ Now execute the following command to pretty print this file:
14
+
15
+ # mixml pretty test.xml
16
+
17
+ This produces the following text output:
18
+
19
+ <?xml version="1.0"?>
20
+ <list>
21
+ <philosopher name="Hobbes"/>
22
+ <philosopher name="Rawls"/>
23
+ </list>
24
+
25
+ ## Remove nodes
26
+
27
+ You can use mixml to remove 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 remove --xpath '//philosopher[@name="Hobbes"]' test.xml
39
+
40
+ This produces the following XML output:
41
+
42
+ <list>
43
+ <philosopher name="Rawls"/>
44
+ </list>
45
+
46
+ ## Replace nodes
47
+
48
+ You can use mixml to replace nodes in your XML documents.
49
+
50
+ Let's use the following XML in file `test.xml`:
51
+
52
+ <list>
53
+ <philosopher name="Hobbes"/>
54
+ <philosopher name="Rawls"/>
55
+ </list>
56
+
57
+ Now execute the following command to select some nodes and replace them:
58
+
59
+ # mixml replace --xpath '//philosopher[@name="Hobbes"]' --string '<tiger name="Hobbes"/>' test.xml
60
+
61
+ This produces the following XML output:
62
+
63
+ <list>
64
+ <tiger name="Hobbes"/>
65
+ <philosopher name="Rawls"/>
66
+ </list>
67
+
68
+ ## Append nodes
69
+
70
+ You can use mixml to append nodes to your XML documents.
71
+
72
+ Let's use the following XML in file `test.xml`:
73
+
74
+ <list>
75
+ <philosopher name="Hobbes"/>
76
+ <philosopher name="Rawls"/>
77
+ </list>
78
+
79
+ Now execute the following command to select some nodes and append a child node to them:
80
+
81
+ # mixml append --xpath '/list' --string '<tiger name="Hobbes"/>' test.xml
82
+
83
+ This produces the following XML output:
84
+
85
+ <list>
86
+ <philosopher name="Hobbes"/>
87
+ <philosopher name="Rawls"/>
88
+ <tiger name="Hobbes"/>
89
+ </list>
90
+
91
+ ## Replace attribute values
92
+
93
+ You can use mixml to replace attribute values in your XML documents.
94
+
95
+ Let's use the following XML in file `test.xml`:
96
+
97
+ <list>
98
+ <philosopher name="Hobbes"/>
99
+ <philosopher name="Rawls"/>
100
+ </list>
101
+
102
+ Now execute the following command to select some attributes and change their value:
103
+
104
+ # mixml value --xpath '//philosopher[1]/@name' --string 'Platon' test.xml
105
+
106
+ This produces the following XML output:
107
+
108
+ <list>
109
+ <philosopher name="Platon"/>
110
+ <philosopher name="Rawls"/>
111
+ </list>
112
+
113
+ ## Rename nodes
114
+
115
+ You can use mixml to rename nodes values in your XML documents.
116
+
117
+ Let's use the following XML in file `test.xml`:
118
+
119
+ <list>
120
+ <philosopher name="Hobbes"/>
121
+ <philosopher name="Rawls"/>
122
+ </list>
123
+
124
+ Now execute the following command to select some attributes and rename them:
125
+
126
+ # mixml rename --xpath '//philosopher[@name="Hobbes"]' --template 'tiger-and-{=node.name}' test.xml
127
+
128
+ This produces the following XML output:
129
+
130
+ <list>
131
+ <tiger-and-philosopher name="Hobbes"/>
132
+ <philosopher name="Rawls"/>
133
+ </list>