#!/usr/bin/lua
-local Vcard = {}
+local Vcard = {
+ _component_fields = { N = true; GENDER = true; ADR = true; ORG = true; }
+}
-- constructor, synonymous to load()
function Vcard:new(path)
function Vcard:parse(data)
local line, key, attr, value, i, v, n
local r = {}
+ setmetatable(r, { __index = self })
-- unwrap line continuations, remove carriage return at EOL
data = data:gsub("\r?\n[ \t]", "")
for line in data:gmatch("[^\n]+") do
key, attr, value = self:_parse_line(line)
if not r[key] then r[key] = { attr = {} } end
- for i,v in ipairs(self:_split_by(value, ",")) do
+ if Vcard._component_fields[key] then
+ table.insert(r[key], self:components(value));
+ r[key].attr[#r[key]] = attr
+ r[key][#r[key]][0] = value;
+ else for i,v in ipairs(self:_split_by(value, ",")) do
table.insert(r[key], v)
r[key].attr[#r[key]] = attr
- end
+ end end
end
-- try to ensure existence of FN field
if not r["FN"] and r["N"] then
- n = self:components(r["N"][1])
+ n = r["N"][1]
r["FN"] = { (n[4] or " ") .. " " .. (n[2] or " ") .. " " ..
(n[3] or " ") .. " " .. (n[1] or " ") .. " " ..
(n[5] or " ") }
r["FN"][1] = r["FN"][1]:gsub("%s+", " "):gsub("^ ", ""):gsub(" $", "")
end
- setmetatable(r, { __index = self })
return r
end
assert( vcf["PHONE"][3] == "987654321", "Phone/3 wrong number" )
assert( vcf["PHONE"].attr[3][2] == "TYPE=Cell", "Phone/3 attr type=cell" )
assert( vcf["FN"][1] == "Title Firstname Middle Names Lastname Suffix" )
+ assert( vcf["N"][1][1] == "Lastname" )
+ assert( vcf["N"][1][4] == " Title" )
vcf = self:parse([=[
BEGIN:VCARD