ppcommand 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/bin/pp +13 -0
- data/lib/ppcommand.rb +149 -0
- data/ppcommand.gemspec +64 -0
- data/spec/ppcommand_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +97 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 KOSEKI Kengo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= ppcommand
|
2
|
+
|
3
|
+
pretty print YAML/JSON/XML.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2010 KOSEKI Kengo. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ppcommand"
|
8
|
+
gem.summary = %Q{pretty print YAML/JSON/XML}
|
9
|
+
gem.description = %Q{pretty print YAML/JSON/XML}
|
10
|
+
gem.email = "koseki@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/koseki/ppcommand"
|
12
|
+
gem.authors = ["KOSEKI Kengo"]
|
13
|
+
gem.add_dependency('json')
|
14
|
+
gem.add_dependency('xml-simple')
|
15
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "ppcommand #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/pp
ADDED
data/lib/ppcommand.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require 'yaml'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
$KCODE = 'utf8'
|
6
|
+
|
7
|
+
class PPCommand
|
8
|
+
|
9
|
+
def self.execute
|
10
|
+
self.new.execute(ARGV)
|
11
|
+
end
|
12
|
+
|
13
|
+
def pp_xml(source)
|
14
|
+
require 'rubygems'
|
15
|
+
require 'rexml/document'
|
16
|
+
puts pretty_xml(REXML::Document.new(source))
|
17
|
+
end
|
18
|
+
|
19
|
+
def pp_xmlsimple(source)
|
20
|
+
require 'rubygems'
|
21
|
+
require 'xmlsimple'
|
22
|
+
pp XmlSimple.xml_in(source)
|
23
|
+
end
|
24
|
+
|
25
|
+
def pp_json(source)
|
26
|
+
require 'rubygems'
|
27
|
+
require 'json'
|
28
|
+
pp JSON.parse(source)
|
29
|
+
end
|
30
|
+
|
31
|
+
def pp_yaml(source)
|
32
|
+
YAML.each_document(StringIO.new(source)) do |obj|
|
33
|
+
pp obj
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute(argv)
|
38
|
+
opts = {:type => "auto"}
|
39
|
+
opp = OptionParser.new
|
40
|
+
|
41
|
+
opp.banner = "pp [options] [file|URI]"
|
42
|
+
opp.on("-y", "--yaml", "parse YAML and pp."){|x| opts[:type] = "yaml"}
|
43
|
+
opp.on("-j", "--json", "parse JSON and pp."){|x| opts[:type] = "json"}
|
44
|
+
opp.on("-x", "--xml", "parse XML using REXML and pp."){|x| opts[:type] = "xml"}
|
45
|
+
opp.on("-X", "--xmlsimple", "parse XML using XMLSimple and pp."){|x| opts[:type] = "xmlsimple"}
|
46
|
+
opp.on("-t", "--text", "do not parse. print plain text."){|x| opts[:type] = "text"}
|
47
|
+
opp.parse!(argv)
|
48
|
+
|
49
|
+
file = argv.shift
|
50
|
+
|
51
|
+
source = ""
|
52
|
+
if file.nil?
|
53
|
+
source = STDIN.read
|
54
|
+
elsif file=~ %r{^https?://}
|
55
|
+
require 'open-uri'
|
56
|
+
open(file) do |io|
|
57
|
+
source = io.read
|
58
|
+
if opts[:type] == "auto"
|
59
|
+
t = io.content_type
|
60
|
+
if t =~ /json/
|
61
|
+
opts[:type] = "json"
|
62
|
+
elsif t =~ /yaml/
|
63
|
+
opts[:type] = "yaml"
|
64
|
+
elsif t =~ /xml/
|
65
|
+
opts[:type] = "xml"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
else
|
70
|
+
source = File.read(file)
|
71
|
+
end
|
72
|
+
|
73
|
+
if opts[:type] == "auto"
|
74
|
+
if file =~ /\.xml$/
|
75
|
+
opts[:type] = "xml"
|
76
|
+
elsif file =~ /\.json$/
|
77
|
+
opts[:type] = "json"
|
78
|
+
else
|
79
|
+
opts[:type] = "yaml"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
case opts[:type]
|
84
|
+
when "xml"
|
85
|
+
pp_xml(source)
|
86
|
+
when "xmlsimple"
|
87
|
+
pp_xmlsimple(source)
|
88
|
+
when "json"
|
89
|
+
pp_json(source)
|
90
|
+
when "text"
|
91
|
+
puts source
|
92
|
+
else "yaml"
|
93
|
+
pp_yaml(source)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
# original from http://snippets.dzone.com/posts/show/4953
|
99
|
+
def x_pretty_print(parent_node, itab)
|
100
|
+
buffer = ''
|
101
|
+
parent_node.elements.each do |node|
|
102
|
+
buffer += ' ' * itab + "<#{node.name}#{get_att_list(node, itab)}"
|
103
|
+
if node.to_a.length > 0
|
104
|
+
buffer += ">"
|
105
|
+
if node.text.nil?
|
106
|
+
buffer += "\n"
|
107
|
+
buffer += x_pretty_print(node,itab+2)
|
108
|
+
buffer += ' ' * itab + "</#{node.name}>\n"
|
109
|
+
else
|
110
|
+
node_text = node.text.strip
|
111
|
+
if node_text != ''
|
112
|
+
buffer += node_text
|
113
|
+
buffer += "</#{node.name}>\n"
|
114
|
+
else
|
115
|
+
buffer += "\n" + x_pretty_print(node,itab+2)
|
116
|
+
buffer += ' ' * itab + "</#{node.name}>\n"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
else
|
120
|
+
buffer += " />\n"
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
buffer
|
125
|
+
end
|
126
|
+
|
127
|
+
def get_att_list(node, itab = 0)
|
128
|
+
att_list = ''
|
129
|
+
node.attributes.each { |attribute, val| att_list += "\n" + (" " * itab) + " #{attribute} = \"#{val}\"" }
|
130
|
+
att_list
|
131
|
+
end
|
132
|
+
|
133
|
+
def pretty_xml(doc)
|
134
|
+
buffer = ''
|
135
|
+
xml_declaration = doc.to_s.match('<\?.*\?>').to_s
|
136
|
+
buffer += "#{xml_declaration}\n" if not xml_declaration.nil?
|
137
|
+
xml_doctype = doc.to_s.match('<\!.*\">').to_s
|
138
|
+
buffer += "#{xml_doctype}\n" if not xml_doctype.nil?
|
139
|
+
buffer += "<#{doc.root.name}#{get_att_list(doc.root)}"
|
140
|
+
if doc.root.to_a.length > 0
|
141
|
+
buffer +=">\n#{x_pretty_print(doc.root,2)}</#{doc.root.name}>"
|
142
|
+
else
|
143
|
+
buffer += " />\n"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
|
data/ppcommand.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ppcommand}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["KOSEKI Kengo"]
|
12
|
+
s.date = %q{2010-01-08}
|
13
|
+
s.default_executable = %q{pp}
|
14
|
+
s.description = %q{pretty print YAML/JSON/XML}
|
15
|
+
s.email = %q{koseki@gmail.com}
|
16
|
+
s.executables = ["pp"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".gitignore",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"bin/pp",
|
29
|
+
"lib/ppcommand.rb",
|
30
|
+
"ppcommand.gemspec",
|
31
|
+
"spec/ppcommand_spec.rb",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/koseki/ppcommand}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{pretty print YAML/JSON/XML}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/ppcommand_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
51
|
+
s.add_runtime_dependency(%q<xml-simple>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<json>, [">= 0"])
|
55
|
+
s.add_dependency(%q<xml-simple>, [">= 0"])
|
56
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
57
|
+
end
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<json>, [">= 0"])
|
60
|
+
s.add_dependency(%q<xml-simple>, [">= 0"])
|
61
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ppcommand
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- KOSEKI Kengo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-08 00:00:00 +09:00
|
13
|
+
default_executable: pp
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: xml-simple
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.2.9
|
44
|
+
version:
|
45
|
+
description: pretty print YAML/JSON/XML
|
46
|
+
email: koseki@gmail.com
|
47
|
+
executables:
|
48
|
+
- pp
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.rdoc
|
54
|
+
files:
|
55
|
+
- .document
|
56
|
+
- .gitignore
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- bin/pp
|
62
|
+
- lib/ppcommand.rb
|
63
|
+
- ppcommand.gemspec
|
64
|
+
- spec/ppcommand_spec.rb
|
65
|
+
- spec/spec.opts
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/koseki/ppcommand
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: "0"
|
87
|
+
version:
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.3.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: pretty print YAML/JSON/XML
|
95
|
+
test_files:
|
96
|
+
- spec/ppcommand_spec.rb
|
97
|
+
- spec/spec_helper.rb
|