mixml 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTM2NTgxNmFjNmU2MDFjMmZjMDU2YjZjOGMwYWQyMjFkZDc0ODIzOA==
5
+ data.tar.gz: !binary |-
6
+ ZTBiMzIyZjljMzlhMzk5NzlkMjcxNmRlMjkwOTlmZGZlNzEwNTY3Yw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjJiMzU4YWI2M2YzMmFhYmRjY2JkNmZlMTk5YTk1NzNiZjI0ZWFjOGU5ZmI4
10
+ ZWQxNDlmMDQ2YjFhNDIwMTgxODI0YWE5YThlODIzOTgyYWVmN2IzYmEzMjhh
11
+ MjFlOGQyZjBjN2MxMjExMDEyZjhkODRjODVlNjRmNmYwMzFlMDg=
12
+ data.tar.gz: !binary |-
13
+ NTQxNjM5NzQyNmExOGI2ZTlhOTgwMDg2MTdmYTE2ZjM4ZGM2N2ZmNzEyMGY4
14
+ NmQ0OGUyN2NiODM4MGUxNDY5Mzg4ZTg2MWVlODhjYjQxZTQ4ZWY5MTdlYWQx
15
+ MTMyZTIxYmU0MmEzNTIzYzFiYzc2YjVkYjRkZGRiN2Y3NDQ5MGE=
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'commander/import'
5
+ require 'mixml'
6
+
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
+ end
20
+
21
+ command :pretty do |c|
22
+ c.description = 'Pretty print XML files'
23
+ c.action do |args, options|
24
+ $tool.pretty = true
25
+ $tool.work(args)
26
+ end
27
+ end
28
+
29
+ command :remove do |c|
30
+ c.description = 'Remove nodes from the XML documents'
31
+ c.option '-x', '--xpath STRING', String, 'XPath for nodes to delete'
32
+ c.action do |args, options|
33
+ $tool.work(args) do
34
+ xpath options.xpath do
35
+ remove
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ command :replace do |c|
42
+ c.description = 'Replace nodes from the XML documents'
43
+ c.option '-x', '--xpath STRING', String, 'XPath for nodes to replace'
44
+ c.option '-t', '--template STRING', String, 'Template to generate the replacement value'
45
+ c.action do |args, options|
46
+ $tool.work(args) do
47
+ xpath options.xpath do
48
+ replace template options.template
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ command :value do |c|
55
+ c.description = 'Set node values'
56
+ c.option '-x', '--xpath STRING', String, 'XPath for nodes to change'
57
+ c.option '-t', '--template STRING', String, 'Template to generate the replacement value'
58
+ c.action do |args, options|
59
+ $tool.work(args) do
60
+ xpath options.xpath do
61
+ value template options.template
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ command :execute do |c|
68
+ c.description = 'Execute script on the XML documents'
69
+ c.option '-s', '--script STRING', String, 'Script file to execute'
70
+ c.option '-e', '--expression STRING', String, 'Command to execute'
71
+ c.action do |args, options|
72
+ script = options.expression || File.read(options.script)
73
+
74
+ $tool.work(args) do
75
+ execute(script)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,2 @@
1
+ require 'mixml/version'
2
+ require 'mixml/tool'
@@ -0,0 +1,13 @@
1
+
2
+
3
+ module Mixml
4
+ class Document
5
+ attr_reader :name
6
+ attr_reader :xml
7
+
8
+ def initialize(name, xml)
9
+ @name = name
10
+ @xml = xml
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ require 'mixml/template/text'
2
+
3
+ module Mixml
4
+ class Selection
5
+ attr_reader :nodes
6
+
7
+ def initialize(nodes)
8
+ @nodes = nodes
9
+ end
10
+
11
+ def remove
12
+ @nodes.remove
13
+ end
14
+
15
+ def replace(template)
16
+ if template.is_a?(String) then
17
+ template = Template::Text.new(template)
18
+ end
19
+
20
+ @nodes.each do |node|
21
+ value = template.evaluate(node)
22
+ node.replace(value)
23
+ end
24
+ end
25
+
26
+ def value(template)
27
+ if template.is_a?(String) then
28
+ template = Template::Text.new(template)
29
+ end
30
+
31
+ @nodes.each do |node|
32
+ value = template.evaluate(node)
33
+ node.value = value
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require 'erubis'
2
+
3
+ module Mixml
4
+ module Template
5
+ class Template
6
+ attr_reader :text
7
+
8
+ def initialize(text)
9
+ @text = text
10
+ end
11
+
12
+ def evaluate(node)
13
+ template = Erubis::Eruby.new(@text, :pattern => '{ }')
14
+ context = {:node => node}
15
+ template.result(context)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Mixml
2
+ module Template
3
+ class Text
4
+ attr_reader :text
5
+
6
+ def initialize(text)
7
+ @text = text
8
+ end
9
+
10
+ def evaluate(node)
11
+ @text
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,124 @@
1
+ require 'mixml/selection'
2
+ require 'mixml/document'
3
+ require 'mixml/template/template'
4
+ require 'docile'
5
+ require 'nokogiri'
6
+
7
+ module Mixml
8
+ class Tool
9
+ attr_reader :documents
10
+ attr_accessor :pretty
11
+ attr_accessor :save
12
+ attr_accessor :indent
13
+
14
+ def initialize
15
+ @indent = 4
16
+ @pretty = false
17
+ @save = false
18
+ @documents = []
19
+ end
20
+
21
+ def load(*file_names)
22
+ file_names.flatten.each do |file_name|
23
+ xml = File.open(file_name, 'r') do |file|
24
+ Nokogiri::XML(file) do |config|
25
+ if @pretty then
26
+ config.default_xml.noblanks
27
+ end
28
+ end
29
+ end
30
+ @documents << Document.new(file_name, xml)
31
+ end
32
+ end
33
+
34
+ def save_all
35
+ options = {}
36
+
37
+ if @pretty then
38
+ options[:indent] = @indent
39
+ end
40
+
41
+ @documents.each do |document|
42
+ File.open(document.name, 'w') do |file|
43
+ document.xml.write_xml_to(file, options)
44
+ end
45
+ end
46
+ end
47
+
48
+ def print_all
49
+ options = {}
50
+
51
+ if @pretty then
52
+ options[:indent] = @indent
53
+ end
54
+
55
+ @documents.each do |document|
56
+ if @documents.size > 1 then
57
+ puts '-' * document.name.length
58
+ puts document.name
59
+ puts '-' * document.name.length
60
+ end
61
+ puts document.xml.to_xml(options)
62
+ end
63
+ end
64
+
65
+ def remove_all
66
+ @documents = []
67
+ end
68
+
69
+ def flush
70
+ if @save then
71
+ save_all
72
+ else
73
+ print_all
74
+ end
75
+
76
+ remove_all
77
+ end
78
+
79
+ def work(*file_names, &block)
80
+ remove_all
81
+
82
+ file_names.each do |file_name|
83
+ load(file_name)
84
+ end
85
+
86
+ if not block.nil? then
87
+ execute(&block)
88
+ end
89
+
90
+ flush
91
+ end
92
+
93
+ def process
94
+ @documents.each do |document|
95
+ yield document.xml
96
+ end
97
+ end
98
+
99
+ def xpath(query, &block)
100
+ process do |xml|
101
+ nodes = xml.xpath(query)
102
+ selection = Selection.new(nodes)
103
+
104
+ if block_given? then
105
+ Docile.dsl_eval(selection, &block)
106
+ end
107
+ end
108
+ end
109
+
110
+ def template(text)
111
+ Template::Template.new(text)
112
+ end
113
+
114
+ def execute(program = nil, &block)
115
+ if not program.nil? then
116
+ instance_eval(program)
117
+ end
118
+
119
+ if not block.nil? then
120
+ Docile.dsl_eval(self, &block)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ module Mixml
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jochen Seeber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: erubis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: docile
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: qed
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '2.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '2.9'
111
+ description: XML tool to easily manipulate multiple XML files at once
112
+ email:
113
+ - jochen@seeber.me
114
+ executables:
115
+ - mixml
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - bin/mixml
120
+ - lib/mixml.rb
121
+ - lib/mixml/document.rb
122
+ - lib/mixml/selection.rb
123
+ - lib/mixml/template/template.rb
124
+ - lib/mixml/template/text.rb
125
+ - lib/mixml/tool.rb
126
+ - lib/mixml/version.rb
127
+ homepage: http://www.noussommesdesoles.net
128
+ licenses:
129
+ - AGPL-3.0
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.2.2
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: XML tool to easily manipulate multiple XML files at once
151
+ test_files: []
152
+ has_rdoc: