From: Paul Hänsch Date: Sat, 7 Sep 2024 11:10:52 +0000 (+0200) Subject: process split fields as arrays X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=confetti process split fields as arrays --- diff --git a/lua/vcard.lua b/lua/vcard.lua index ab81248..3066737 100644 --- a/lua/vcard.lua +++ b/lua/vcard.lua @@ -1,6 +1,8 @@ #!/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) @@ -25,6 +27,7 @@ end 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]", "") @@ -34,15 +37,19 @@ function Vcard:parse(data) 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 " ") } @@ -53,7 +60,6 @@ function Vcard:parse(data) r["FN"][1] = r["FN"][1]:gsub("%s+", " "):gsub("^ ", ""):gsub(" $", "") end - setmetatable(r, { __index = self }) return r end @@ -145,6 +151,8 @@ END:VCARD 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