Skip to content

Instantly share code, notes, and snippets.

@hpyhacking
Last active February 6, 2022 13:09

Revisions

  1. hpyhacking revised this gist Jan 10, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion add_pinyin_to_vcard.rb
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@
    cp = false

    def p(z)
    Pinyin.t(z, '')
    pinyin = Pinyin.t(z, '')
    pinyin[0] = pinyin[0].upcase
    pinyin
    end

    ARGF.readlines.each do |x|
  2. hpyhacking created this gist Dec 16, 2012.
    27 changes: 27 additions & 0 deletions add_pinyin_to_vcard.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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