Skip to content

Instantly share code, notes, and snippets.

@sundevilyang
Forked from hpyhacking/add_pinyin_to_vcard.rb
Created December 16, 2012 23:22
Show Gist options
  • Save sundevilyang/4314125 to your computer and use it in GitHub Desktop.
Save sundevilyang/4314125 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'chinese_pinyin'
name = []
cp = false
def p(z)
Pinyin.t(z, '')
end
ARGF.readlines.each do |x|
if cp and (not (/X-PHONETIC/ =~ x))
if name and name.length == 2
puts "X-PHONETIC-FIRST-NAME:#{p(name[0])}"
puts "X-PHONETIC-LAST-NAME:#{p(name[1])}"
elsif name and name.length == 1
puts "X-PHONETIC-LAST-NAME:#{p(name[0])}"
else
raise "ERROR NAME FORMAT #{name}"
end
end
cp = (/FN:/ =~ x)
name = x.split(':')[1].split(' ') if cp
puts x
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment