ppcommand 0.1.0 → 0.1.1
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/README.rdoc +33 -1
- data/VERSION +1 -1
- data/lib/ppcommand.rb +59 -3
- data/ppcommand.gemspec +2 -2
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,38 @@
|
|
1
1
|
= ppcommand
|
2
2
|
|
3
|
-
|
3
|
+
Parse and pp YAML/JSON/XML/CSV/HTML.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
$ pp --help
|
8
|
+
pp [options] [file|URI]
|
9
|
+
-y, --yaml parse YAML and pp.
|
10
|
+
-j, --json parse JSON and pp.
|
11
|
+
-x, --xml parse XML using REXML and pp.
|
12
|
+
-X, --xmlsimple parse XML using XMLSimple and pp.
|
13
|
+
-c, --csv parse CSV and pp.
|
14
|
+
-C, --csvtable parse CSV, add labels and pp.
|
15
|
+
-h, --html parse HTML and pp.
|
16
|
+
-t, --text do not parse. print plain text.
|
17
|
+
|
18
|
+
== Examples
|
19
|
+
|
20
|
+
Parse YAML and pp.
|
21
|
+
|
22
|
+
pp ./config/database.yml
|
23
|
+
|
24
|
+
Parse JSON and pp.
|
25
|
+
|
26
|
+
pp http://api.twitter.com/1/statuses/public_timeline.json
|
27
|
+
|
28
|
+
Parse HTML and pp (required nokogiri).
|
29
|
+
|
30
|
+
pp http://www.google.com/
|
31
|
+
|
32
|
+
Get URL contents as text, skip 1 line, convert encoding to UTF-8, parse CSV and pp with labels.
|
33
|
+
|
34
|
+
pp -t http://example.com/example.csv | head -n +1 | nkf -w | pp -C
|
35
|
+
|
4
36
|
|
5
37
|
== Note on Patches/Pull Requests
|
6
38
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/ppcommand.rb
CHANGED
@@ -27,12 +27,51 @@ class PPCommand
|
|
27
27
|
require 'json'
|
28
28
|
pp JSON.parse(source)
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def pp_yaml(source)
|
32
32
|
YAML.each_document(StringIO.new(source)) do |obj|
|
33
33
|
pp obj
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
def pp_csv(source)
|
38
|
+
require 'csv'
|
39
|
+
pp CSV.parse(source)
|
40
|
+
end
|
41
|
+
|
42
|
+
def pp_csv2(source)
|
43
|
+
require 'csv'
|
44
|
+
data = CSV.parse(source)
|
45
|
+
keys = data.shift
|
46
|
+
result = []
|
47
|
+
data.each do |values|
|
48
|
+
entry = []
|
49
|
+
i = nil
|
50
|
+
keys.each_with_index do |k, i|
|
51
|
+
entry << [i, k, values[i]]
|
52
|
+
end
|
53
|
+
if keys.length < values.length
|
54
|
+
values[i + 1 .. -1].each_with_index do |v, j|
|
55
|
+
entry << [i + j + 1, nil, v]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
result << entry
|
59
|
+
end
|
60
|
+
pp result
|
61
|
+
# data.map {|values| Hash[* [keys,values].transpose.flatten] }
|
62
|
+
end
|
63
|
+
|
64
|
+
def pp_html(source)
|
65
|
+
begin
|
66
|
+
require 'nokogiri'
|
67
|
+
rescue Exception => e
|
68
|
+
STDERR.puts "'nokogiri' required to parse HTML."
|
69
|
+
STDERR.puts "$ sudo gem install nokorigi"
|
70
|
+
end
|
71
|
+
doc = Nokogiri.HTML(source)
|
72
|
+
# doc.serialize(:encoding => 'UTF-8', :save_with => Nokogiri::XML::Node::SaveOptions::FORMAT | Nokogiri::XML::Node::SaveOptions::AS_XML)
|
73
|
+
pp doc
|
74
|
+
end
|
36
75
|
|
37
76
|
def execute(argv)
|
38
77
|
opts = {:type => "auto"}
|
@@ -43,6 +82,9 @@ class PPCommand
|
|
43
82
|
opp.on("-j", "--json", "parse JSON and pp."){|x| opts[:type] = "json"}
|
44
83
|
opp.on("-x", "--xml", "parse XML using REXML and pp."){|x| opts[:type] = "xml"}
|
45
84
|
opp.on("-X", "--xmlsimple", "parse XML using XMLSimple and pp."){|x| opts[:type] = "xmlsimple"}
|
85
|
+
opp.on("-c", "--csv", "parse CSV and pp."){|x| opts[:type] = "csv"}
|
86
|
+
opp.on("-C", "--csvtable", "parse CSV, add labels and pp."){|x| opts[:type] = "csvhash"}
|
87
|
+
opp.on("-h", "--html", "parse HTML and pp."){|x| opts[:type] = "html"}
|
46
88
|
opp.on("-t", "--text", "do not parse. print plain text."){|x| opts[:type] = "text"}
|
47
89
|
opp.parse!(argv)
|
48
90
|
|
@@ -61,6 +103,10 @@ class PPCommand
|
|
61
103
|
opts[:type] = "json"
|
62
104
|
elsif t =~ /yaml/
|
63
105
|
opts[:type] = "yaml"
|
106
|
+
elsif t =~ /csv/
|
107
|
+
opts[:type] = "csv"
|
108
|
+
elsif t =~ /html/
|
109
|
+
opts[:type] = "html"
|
64
110
|
elsif t =~ /xml/
|
65
111
|
opts[:type] = "xml"
|
66
112
|
end
|
@@ -71,10 +117,14 @@ class PPCommand
|
|
71
117
|
end
|
72
118
|
|
73
119
|
if opts[:type] == "auto"
|
74
|
-
if file =~ /\.xml$/
|
120
|
+
if file =~ /\.xml$/i
|
75
121
|
opts[:type] = "xml"
|
76
|
-
elsif file =~ /\.json$/
|
122
|
+
elsif file =~ /\.json$/i
|
77
123
|
opts[:type] = "json"
|
124
|
+
elsif file =~ /\.(?:csv|txt)$/i
|
125
|
+
opts[:type] = "csv"
|
126
|
+
elsif file =~ /\.html$/i
|
127
|
+
opts[:type] = "html"
|
78
128
|
else
|
79
129
|
opts[:type] = "yaml"
|
80
130
|
end
|
@@ -87,6 +137,12 @@ class PPCommand
|
|
87
137
|
pp_xmlsimple(source)
|
88
138
|
when "json"
|
89
139
|
pp_json(source)
|
140
|
+
when "csv"
|
141
|
+
pp_csv(source)
|
142
|
+
when "csvhash"
|
143
|
+
pp_csv_as_hash(source)
|
144
|
+
when "html"
|
145
|
+
pp_html(source)
|
90
146
|
when "text"
|
91
147
|
puts source
|
92
148
|
else "yaml"
|
data/ppcommand.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ppcommand}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["KOSEKI Kengo"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-04-09}
|
13
13
|
s.default_executable = %q{pp}
|
14
14
|
s.description = %q{pretty print YAML/JSON/XML}
|
15
15
|
s.email = %q{koseki@gmail.com}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ppcommand
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- KOSEKI Kengo
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-04-09 00:00:00 +09:00
|
13
13
|
default_executable: pp
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|