rix 0.1.1 → 0.2.0
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.
- data/bin/rix +3 -0
- data/lib/rix.rb +4 -0
- data/lib/rix/command.rb +28 -18
- data/lib/rix/commands/add_attribute.rb +32 -0
- data/lib/rix/commands/add_element.rb +2 -2
- data/lib/rix/commands/count.rb +2 -2
- data/lib/rix/commands/help.rb +7 -5
- data/lib/rix/commands/remove.rb +7 -4
- data/lib/rix/commands/rename.rb +6 -2
- data/lib/rix/commands/show.rb +3 -3
- data/lib/rix/commands/trim.rb +37 -0
- data/lib/rix/commands/unknown.rb +2 -2
- data/lib/rix/version.rb +1 -1
- metadata +4 -2
data/bin/rix
CHANGED
@@ -6,8 +6,10 @@ require 'rix'
|
|
6
6
|
require 'optparse'
|
7
7
|
|
8
8
|
Rix::Registry.instance.register 'add-element', Rix::Commands::AddElement
|
9
|
+
Rix::Registry.instance.register 'add-attribute', Rix::Commands::AddAttribute
|
9
10
|
Rix::Registry.instance.register 'remove', Rix::Commands::Remove
|
10
11
|
Rix::Registry.instance.register 'rename', Rix::Commands::Rename
|
12
|
+
Rix::Registry.instance.register 'trim', Rix::Commands::Trim
|
11
13
|
Rix::Registry.instance.register 'count', Rix::Commands::Count
|
12
14
|
Rix::Registry.instance.register 'show', Rix::Commands::Show
|
13
15
|
Rix::Registry.instance.register 'help', Rix::Commands::Help
|
@@ -17,6 +19,7 @@ if ARGV.size == 0
|
|
17
19
|
elsif not ARGV[0].start_with? '-'
|
18
20
|
name = ARGV.shift
|
19
21
|
command = Rix::Registry.instance[name]
|
22
|
+
command.out = $stdout
|
20
23
|
begin
|
21
24
|
command.execute
|
22
25
|
rescue => e
|
data/lib/rix.rb
CHANGED
@@ -3,10 +3,14 @@ require 'rix/registry'
|
|
3
3
|
require 'rix/command'
|
4
4
|
require 'rix/commands/help'
|
5
5
|
require 'rix/commands/add_element'
|
6
|
+
require 'rix/commands/add_attribute'
|
6
7
|
require 'rix/commands/remove'
|
7
8
|
require 'rix/commands/rename'
|
9
|
+
require 'rix/commands/trim'
|
8
10
|
require 'rix/commands/count'
|
9
11
|
require 'rix/commands/show'
|
10
12
|
require 'rix/commands/unknown'
|
11
13
|
require 'rix/help'
|
12
14
|
require 'rix/version'
|
15
|
+
|
16
|
+
include REXML
|
data/lib/rix/command.rb
CHANGED
@@ -2,6 +2,7 @@ require 'rix/commands/unknown'
|
|
2
2
|
|
3
3
|
module Rix
|
4
4
|
class Command
|
5
|
+
attr_accessor :out
|
5
6
|
attr_accessor :files
|
6
7
|
attr_accessor :xpath
|
7
8
|
|
@@ -33,13 +34,7 @@ module Rix
|
|
33
34
|
# on Mac, Linux etc. this will have no effect.
|
34
35
|
Dir[pattern].each do |path|
|
35
36
|
File.open(path, 'r') do |file|
|
36
|
-
|
37
|
-
nodes = REXML::XPath.match(document, @xpath)
|
38
|
-
before(path, nodes)
|
39
|
-
nodes.each do |node|
|
40
|
-
on_node(node)
|
41
|
-
end
|
42
|
-
after(path, document)
|
37
|
+
on_source(file, path)
|
43
38
|
end
|
44
39
|
end
|
45
40
|
end
|
@@ -47,22 +42,37 @@ module Rix
|
|
47
42
|
after_all
|
48
43
|
end
|
49
44
|
|
50
|
-
def
|
45
|
+
def on_source(source, name)
|
46
|
+
document = Document.new(source, :ignore_whitespace_nodes => :all, :attribute_quote => :quote)
|
47
|
+
on_document(document, name)
|
51
48
|
end
|
52
49
|
|
53
|
-
def
|
50
|
+
def on_document(document, name)
|
51
|
+
nodes = XPath.match(document, @xpath)
|
52
|
+
before(name, document, nodes)
|
53
|
+
nodes.each do |node|
|
54
|
+
on_node(node)
|
55
|
+
on_element(node) if node.is_a? Element
|
56
|
+
on_attribute(node) if node.is_a? Attribute
|
57
|
+
on_text(node) if node.is_a? Text
|
58
|
+
end
|
59
|
+
after(name, document)
|
54
60
|
end
|
55
61
|
|
56
|
-
def
|
57
|
-
end
|
62
|
+
def before_all; end
|
58
63
|
|
59
|
-
def
|
60
|
-
#File.open(path, 'w') do |file|
|
61
|
-
# document.write file
|
62
|
-
#end
|
63
|
-
end
|
64
|
+
def before(name, document, nodes); end
|
64
65
|
|
65
|
-
def
|
66
|
-
|
66
|
+
def on_node(node); end
|
67
|
+
|
68
|
+
def on_element(element); end
|
69
|
+
|
70
|
+
def on_attribute(attribute); end
|
71
|
+
|
72
|
+
def on_text(text); end
|
73
|
+
|
74
|
+
def after(name, document); end
|
75
|
+
|
76
|
+
def after_all; end
|
67
77
|
end
|
68
78
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Rix
|
2
|
+
module Commands
|
3
|
+
class AddAttribute < Command
|
4
|
+
attr_accessor :name
|
5
|
+
attr_accessor :value
|
6
|
+
|
7
|
+
def opts
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: rix add-attribute [options] <xpath> <files>"
|
10
|
+
opts.separator ""
|
11
|
+
opts.separator "Options:"
|
12
|
+
opts.on("-n", "--name NAME", "The name of the new attribute") { |name| @name = name }
|
13
|
+
opts.on("-v", "--value VALUE", "The value of the new attribute") { |value| @value = value }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def before_all
|
18
|
+
raise "Missing option: -n, --name" if @name.nil? or @name.empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_element(element)
|
22
|
+
element.add_attribute(@name, @value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def after(path, document)
|
26
|
+
File.open(path, 'w') do |file|
|
27
|
+
document.write file
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/rix/commands/count.rb
CHANGED
data/lib/rix/commands/help.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module Rix
|
2
2
|
module Commands
|
3
3
|
class Help
|
4
|
+
attr_accessor :out
|
5
|
+
|
4
6
|
def help
|
5
7
|
HELP
|
6
8
|
end
|
@@ -8,17 +10,17 @@ module Rix
|
|
8
10
|
def execute
|
9
11
|
case ARGV.size
|
10
12
|
when 0
|
11
|
-
puts Rix::HELP
|
13
|
+
out.puts Rix::HELP
|
12
14
|
when 1
|
13
15
|
name = ARGV[0]
|
14
16
|
if name.downcase == 'commands'
|
15
|
-
puts "Available commands:"
|
16
|
-
Registry.instance.commands.each_key { |command| puts " #{command}" }
|
17
|
+
out.puts "Available commands:"
|
18
|
+
Registry.instance.commands.each_key { |command| out.puts " #{command}" }
|
17
19
|
else
|
18
|
-
puts Registry.instance[name].help
|
20
|
+
out.puts Registry.instance[name].help
|
19
21
|
end
|
20
22
|
else
|
21
|
-
puts HELP
|
23
|
+
out.puts HELP
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
data/lib/rix/commands/remove.rb
CHANGED
@@ -6,10 +6,13 @@ module Rix
|
|
6
6
|
"Usage: rix remove <xpath> <files>"
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def on_element(element)
|
10
|
+
element.parent.delete_element element
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_attribute(attribute)
|
14
|
+
# BUG: 'attribute.element.delete_attribute attribute' doesn't work
|
15
|
+
attribute.element.delete_attribute attribute.name
|
13
16
|
end
|
14
17
|
|
15
18
|
def after(path, document)
|
data/lib/rix/commands/rename.rb
CHANGED
@@ -16,8 +16,12 @@ module Rix
|
|
16
16
|
raise "Missing option: -n, --name" if @name.nil? or @name.empty?
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
20
|
-
|
19
|
+
def on_element(element)
|
20
|
+
element.name = @name
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_attribute(attribute)
|
24
|
+
attribute.name = @name
|
21
25
|
end
|
22
26
|
|
23
27
|
def after(path, document)
|
data/lib/rix/commands/show.rb
CHANGED
@@ -2,7 +2,7 @@ module Rix
|
|
2
2
|
module Commands
|
3
3
|
class Show < Command
|
4
4
|
def initialize
|
5
|
-
@formatter =
|
5
|
+
@formatter = Formatters::Pretty.new
|
6
6
|
@formatter.compact = true
|
7
7
|
end
|
8
8
|
|
@@ -11,8 +11,8 @@ module Rix
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def on_node(node)
|
14
|
-
@formatter.write(node,
|
15
|
-
puts
|
14
|
+
@formatter.write(node, out)
|
15
|
+
out.puts
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Rix
|
2
|
+
module Commands
|
3
|
+
class Trim < Command
|
4
|
+
|
5
|
+
def help
|
6
|
+
"Usage: rix trim <xpath> <files>"
|
7
|
+
end
|
8
|
+
|
9
|
+
def on_element(element)
|
10
|
+
element.text = element.text.strip if element.has_text?
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_attribute(attribute)
|
14
|
+
attribute.element.attributes[attribute.name] = attribute.to_s.strip
|
15
|
+
attribute.element.attributes[attribute.name]
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_text(text)
|
19
|
+
stripped = text.to_s.strip
|
20
|
+
# Leave 'empty' nodes intact
|
21
|
+
if stripped.size > 0
|
22
|
+
child = Text.new(stripped)
|
23
|
+
text.replace_with(child)
|
24
|
+
child.to_s
|
25
|
+
else
|
26
|
+
text.to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def after(path, document)
|
31
|
+
File.open(path, 'w') do |file|
|
32
|
+
document.write file
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/rix/commands/unknown.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Rix
|
2
2
|
module Commands
|
3
3
|
class UnknownCommand
|
4
|
-
attr_accessor :name
|
4
|
+
attr_accessor :out, :name
|
5
5
|
|
6
6
|
def message
|
7
7
|
"Unknown command: #{name}. Type 'rix help commands' for a list of available commands."
|
@@ -12,7 +12,7 @@ module Rix
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def execute
|
15
|
-
puts message
|
15
|
+
out.puts message
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/lib/rix/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-28 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Command-line XML editing and refactoring
|
15
15
|
email: richard.hubers@gmail.com
|
@@ -20,12 +20,14 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- bin/rix
|
22
22
|
- lib/rix/command.rb
|
23
|
+
- lib/rix/commands/add_attribute.rb
|
23
24
|
- lib/rix/commands/add_element.rb
|
24
25
|
- lib/rix/commands/count.rb
|
25
26
|
- lib/rix/commands/help.rb
|
26
27
|
- lib/rix/commands/remove.rb
|
27
28
|
- lib/rix/commands/rename.rb
|
28
29
|
- lib/rix/commands/show.rb
|
30
|
+
- lib/rix/commands/trim.rb
|
29
31
|
- lib/rix/commands/unknown.rb
|
30
32
|
- lib/rix/help.rb
|
31
33
|
- lib/rix/registry.rb
|