Skip to content

Instantly share code, notes, and snippets.

@simonthum
Created November 25, 2012 20:27
Show Gist options
  • Save simonthum/4145201 to your computer and use it in GitHub Desktop.
Save simonthum/4145201 to your computer and use it in GitHub Desktop.
create org-contacs entries from vCard input. Only telephone and name so far.
#!/usr/bin/env ruby
#
# Converts VCard to org-mode/org-contacts files
#
# 2012 by Simon Thum
#
# Requires the vpim gem, which in turn needs patches to run (github xing/vpim)
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# http://creativecommons.org/publicdomain/zero/1.0/
require 'rubygems'
gem 'vpim'
require 'erb'
require 'vpim/vcard'
# simple ISO 8601
def isoDate(t)
"%04d-%02d-%02d" % [ t.year, t.month, t.day ]
end
# put errors to stderr
def putError(err, vc)
warn err
warn err.backtrace
warn "------ (vcard) --------"
warn vc
warn "-----------------------"
end
# TODO include some richer vCards, normalize number prefix (+49)
OrgContactTemplate = ERB.new <<-'EOT', nil, "%<>"
<%#-*- coding: UTF-8 -*-%>
** <%= result[:fullName] %>
:PROPERTIES:
:TEL: <%= vc.telephones.first %>
:BIRTHDAY: <%= if (vc.birthday.nil?) then "missing" else isoDate(vc.birthday) end %>
:END:
:VCARD:
<%= vc.to_s %>
:END:
EOT
# this can be used to fix up stuff before the template processing starts
def evaluateContact(vc)
{
:fullName => vc.name.fullname,
# :bday =>
:tel => vc.telephone.to_s
}
end
def orgContactSection(vc)
result = evaluateContact(vc)
OrgContactTemplate.result(binding)
rescue StandardError => e
putError(e, ev)
end
# filter contacts, e.g. see if VCard already in some file
def includeContact?(vc)
true
end
vcf = (ARGV[0].nil?) ? STDIN : open(ARGV[0])
cards = Vpim::Vcard.decode(vcf)
puts "* Imported using vcard2org.rb"
cards.each do |vcard|
puts orgContactSection(vcard) if includeContact?(vcard)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment