Created
March 15, 2012 01:10
-
-
Save amir20/2040955 to your computer and use it in GitHub Desktop.
Converts YAML to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
if ARGV.size.zero? | |
puts %Q[ | |
Usage: | |
./to_csv.rb file.yml > out.csv | |
] | |
exit | |
end | |
require 'yaml' | |
def traverse(obj, keys = [], &block) | |
case obj | |
when Hash | |
obj.each do |k,v| | |
keys << k | |
traverse(v, keys, &block) | |
keys.pop | |
end | |
when Array | |
obj.each { |v| traverse(obj, keys, &block) } | |
else | |
yield keys, obj | |
end | |
end | |
hash = YAML.load(File.open(ARGV.first)) | |
puts "YAML KEY,VALUE,RECOMMENDATION" | |
traverse(hash) do |keys, value| | |
if value =~ /^(.+)?(\/\*\s*(.+?)\s*\*\/)$/ | |
puts '"' + [keys * '.', $1.strip, $3.strip] * '","' + '"' | |
else | |
puts '"' + [keys * '.', value] * '","' + '"' | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Just a little bug, when Array detected.
Line 22: replace
to
Example for this YAML :
Thank's for your code !