Class: PPCommand
- Inherits:
-
Object
- Object
- PPCommand
- Defined in:
- lib/ppcommand.rb
Class Method Summary collapse
Instance Method Summary collapse
- #execute(argv) ⇒ Object
- #get_att_list(node, itab = 0) ⇒ Object
- #pp_csv(source) ⇒ Object
- #pp_csvtable(source) ⇒ Object
- #pp_html(source) ⇒ Object
- #pp_json(source) ⇒ Object
- #pp_xml(source) ⇒ Object
- #pp_xmlsimple(source) ⇒ Object
- #pp_yaml(source) ⇒ Object
- #pretty_xml(doc) ⇒ Object
-
#x_pretty_print(parent_node, itab) ⇒ Object
original from snippets.dzone.com/posts/show/4953.
Class Method Details
.execute ⇒ Object
9 10 11 |
# File 'lib/ppcommand.rb', line 9 def self.execute self.new.execute(ARGV) end |
Instance Method Details
#execute(argv) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ppcommand.rb', line 77 def execute(argv) opts = {:type => "auto"} opp = OptionParser.new opp. = "pp [options] [file|URI]" opp.on_tail("-h", "--help", "show this help.") do puts opp exit end opp.on_tail("-v", "--version", "show version.") do puts "ppcommand " + File.read(File.dirname(__FILE__) + "/../VERSION") exit end opp.on("-c", "--csv", "parse CSV and pp."){|x| opts[:type] = "csv"} opp.on("-C", "--csvtable", "parse CSV, add labels and pp."){|x| opts[:type] = "csvtable"} opp.on("-H", "--html", "parse HTML and pp."){|x| opts[:type] = "html"} opp.on("-j", "--json", "parse JSON and pp."){|x| opts[:type] = "json"} opp.on("-x", "--xml", "parse XML using REXML and pp."){|x| opts[:type] = "xml"} opp.on("-X", "--xmlsimple", "parse XML using XMLSimple and pp."){|x| opts[:type] = "xmlsimple"} opp.on("-y", "--yaml", "parse YAML and pp."){|x| opts[:type] = "yaml"} opp.on("-t", "--text", "do not parse. print plain text."){|x| opts[:type] = "text"} opp.parse!(argv) file = argv.shift source = "" if file.nil? source = STDIN.read elsif file=~ %r{^https?://} require 'open-uri' open(file) do |io| source = io.read if opts[:type] == "auto" t = io.content_type if t =~ /json/ opts[:type] = "json" elsif t =~ /yaml/ opts[:type] = "yaml" elsif t =~ /csv/ opts[:type] = "csv" elsif t =~ /html/ opts[:type] = "html" elsif t =~ /xml/ opts[:type] = "xml" end end end else source = File.read(file) end if opts[:type] == "auto" if file =~ /\.xml$/i opts[:type] = "xml" elsif file =~ /\.json$/i opts[:type] = "json" elsif file =~ /\.(?:csv|txt)$/i opts[:type] = "csv" elsif file =~ /\.html$/i opts[:type] = "html" else opts[:type] = "yaml" end end case opts[:type] when "xml" pp_xml(source) when "xmlsimple" pp_xmlsimple(source) when "json" pp_json(source) when "csv" pp_csv(source) when "csvtable" pp_csvtable(source) when "html" pp_html(source) when "text" puts source else "yaml" pp_yaml(source) end end |
#get_att_list(node, itab = 0) ⇒ Object
195 196 197 198 199 |
# File 'lib/ppcommand.rb', line 195 def get_att_list(node, itab = 0) att_list = '' node.attributes.each { |attribute, val| att_list += "\n" + (" " * itab) + " #{attribute} = \"#{val}\"" } att_list end |
#pp_csv(source) ⇒ Object
37 38 39 40 |
# File 'lib/ppcommand.rb', line 37 def pp_csv(source) require 'csv' pp CSV.parse(source) end |
#pp_csvtable(source) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ppcommand.rb', line 42 def pp_csvtable(source) require 'csv' data = CSV.parse(source) keys = data.shift result = [] data.each do |values| entry = [] i = nil keys.each_with_index do |k, i| entry << [i, k, values[i]] end if keys.length < values.length values[i + 1 .. -1].each_with_index do |v, j| entry << [i + j + 1, nil, v] end end result << entry end pp result # data.map {|values| Hash[* [keys,values].transpose.flatten] } end |
#pp_html(source) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ppcommand.rb', line 64 def pp_html(source) begin require 'nokogiri' rescue Exception => e STDERR.puts "'nokogiri' is required to parse HTML." STDERR.puts "$ sudo gem install nokorigi" return end doc = Nokogiri.HTML(source) # doc.serialize(:encoding => 'UTF-8', :save_with => Nokogiri::XML::Node::SaveOptions::FORMAT | Nokogiri::XML::Node::SaveOptions::AS_XML) pp doc end |
#pp_json(source) ⇒ Object
25 26 27 28 29 |
# File 'lib/ppcommand.rb', line 25 def pp_json(source) require 'rubygems' require 'json' pp JSON.parse(source) end |
#pp_xml(source) ⇒ Object
13 14 15 16 17 |
# File 'lib/ppcommand.rb', line 13 def pp_xml(source) require 'rubygems' require 'rexml/document' puts pretty_xml(REXML::Document.new(source)) end |
#pp_xmlsimple(source) ⇒ Object
19 20 21 22 23 |
# File 'lib/ppcommand.rb', line 19 def pp_xmlsimple(source) require 'rubygems' require 'xmlsimple' pp XmlSimple.xml_in(source) end |
#pp_yaml(source) ⇒ Object
31 32 33 34 35 |
# File 'lib/ppcommand.rb', line 31 def pp_yaml(source) YAML.each_document(StringIO.new(source)) do |obj| pp obj end end |
#pretty_xml(doc) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/ppcommand.rb', line 201 def pretty_xml(doc) buffer = '' xml_declaration = doc.to_s.match('<\?.*\?>').to_s buffer += "#{xml_declaration}\n" if not xml_declaration.nil? xml_doctype = doc.to_s.match('<\!.*\">').to_s buffer += "#{xml_doctype}\n" if not xml_doctype.nil? buffer += "<#{doc.root.name}#{get_att_list(doc.root)}" if doc.root.to_a.length > 0 buffer +=">\n#{x_pretty_print(doc.root,2)}</#{doc.root.name}>" else buffer += " />\n" end end |
#x_pretty_print(parent_node, itab) ⇒ Object
original from snippets.dzone.com/posts/show/4953
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ppcommand.rb', line 167 def x_pretty_print(parent_node, itab) buffer = '' parent_node.elements.each do |node| buffer += ' ' * itab + "<#{node.name}#{get_att_list(node, itab)}" if node.to_a.length > 0 buffer += ">" if node.text.nil? buffer += "\n" buffer += x_pretty_print(node,itab+2) buffer += ' ' * itab + "</#{node.name}>\n" else node_text = node.text.strip if node_text != '' buffer += node_text buffer += "</#{node.name}>\n" else buffer += "\n" + x_pretty_print(node,itab+2) buffer += ' ' * itab + "</#{node.name}>\n" end end else buffer += " />\n" end end buffer end |