Skip to content

Instantly share code, notes, and snippets.

@koseki
Created December 22, 2008 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koseki/38820 to your computer and use it in GitHub Desktop.
Save koseki/38820 to your computer and use it in GitHub Desktop.
undefined
#! /usr/bin/env ruby
# unescape Unicode escaped YAML
# http://d.hatena.ne.jp/cesar/20070401/p1
module Unicode
def escape(str)
ary = str.unpack("U*").map!{|i| "\\u#{i.to_s(16)}"}
ary.join
end
UNESCAPE_WORKER_ARRAY = []
def unescape(str)
str.gsub(/\\u([0-9a-f]{4})/) {
UNESCAPE_WORKER_ARRAY[0] = $1.hex
UNESCAPE_WORKER_ARRAY.pack("U")
}
end
module_function :escape, :unescape
end
require 'yaml'
a = YAML.load_file(ARGV[0]) # Hash Array
puts "["
a.each do |h|
i = 0
print "{"
h.each do |k,v|
print " " if 0 < i
print %{ "#{k}":"#{Unicode.unescape(v)}"}
i+=1
puts "," if h.length != i
end
puts
print "}"
puts "," if a.last != h
end
puts "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment