Skip to content

Instantly share code, notes, and snippets.

@samaaron
Created March 24, 2010 23:32
Show Gist options
  • Save samaaron/342958 to your computer and use it in GitHub Desktop.
Save samaaron/342958 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -wKU
require 'rubygems'
require 'ya2yaml'
$KCODE="UTF8"
lines = File.readlines(ARGV[0])
lines.map!{|l| l.gsub(/[;"\r\n]/, '').split(" ", 2)}
lines.map!{|m| [m[0], m[1].strip]}
lines.map!{|m| [m[0].split('-'), m[1]]}
#hash with recusive magic
l = lambda{|h,k| h[k] = Hash.new(&l)}
h = Hash.new(&l)
def get_nested_value(h, keys)
first = keys.first
rest = keys[1..-1]
val = h.send(:[], first)
if rest.empty?
return val
else
return get_nested_value(val, rest)
end
end
lines.shift
lines.each do |line|
keys = line.first
last = keys[-1]
fest = keys[0..-2]
val = get_nested_value(h, fest)
val.send(:[]=, last, line[1])
end
yaml = h.ya2yaml
File.open(ARGV[0] + '-new.yaml', 'w') {|f| f.puts yaml}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment