Class: PPCommand::Main
- Inherits:
-
Object
- Object
- PPCommand::Main
- Defined in:
- lib/ppcommand/main.rb
Instance Method Summary collapse
- #execute(opts, file) ⇒ 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_syck(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.
Instance Method Details
#execute(opts, file) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ppcommand/main.rb', line 8 def execute(opts, file) source = '' if file.nil? source = STDIN.read elsif file=~ %r{^https?://} 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 when 'syck' pp_syck(source) else "yaml" pp_yaml(source) end end |
#get_att_list(node, itab = 0) ⇒ Object
171 172 173 174 175 |
# File 'lib/ppcommand/main.rb', line 171 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
102 103 104 105 |
# File 'lib/ppcommand/main.rb', line 102 def pp_csv(source) require 'csv' ap CSV.parse(source) end |
#pp_csvtable(source) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ppcommand/main.rb', line 107 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 ap result # data.map {|values| Hash[* [keys,values].transpose.flatten] } end |
#pp_html(source) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ppcommand/main.rb', line 129 def pp_html(source) begin require 'nokogiri' rescue Exception => e STDERR.puts "'nokogiri' is required to parse HTML." STDERR.puts "$ 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) ap doc end |
#pp_json(source) ⇒ Object
82 83 84 85 86 |
# File 'lib/ppcommand/main.rb', line 82 def pp_json(source) require 'rubygems' require 'json' ap JSON.parse(source) end |
#pp_syck(source) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/ppcommand/main.rb', line 94 def pp_syck(source) require 'syck' YAML::ENGINE.yamler = 'syck' objs = YAML.load_document(StringIO.new(source)) objs = objs[0] if objs.length == 1 ap objs end |
#pp_xml(source) ⇒ Object
70 71 72 73 74 |
# File 'lib/ppcommand/main.rb', line 70 def pp_xml(source) require 'rubygems' require 'rexml/document' puts pretty_xml(REXML::Document.new(source)) end |
#pp_xmlsimple(source) ⇒ Object
76 77 78 79 80 |
# File 'lib/ppcommand/main.rb', line 76 def pp_xmlsimple(source) require 'rubygems' require 'xmlsimple' ap XmlSimple.xml_in(source) end |
#pp_yaml(source) ⇒ Object
88 89 90 91 92 |
# File 'lib/ppcommand/main.rb', line 88 def pp_yaml(source) objs = YAML.load_stream(source) objs = objs[0] if objs.length == 1 ap objs end |
#pretty_xml(doc) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ppcommand/main.rb', line 177 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ppcommand/main.rb', line 143 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 |