-
-
Save Phazz/8ce179b1f2a24cb0c3e3 to your computer and use it in GitHub Desktop.
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