Module:VCard

From WikiProducers
Jump to: navigation, search

Documentation for this module may be created at Module:VCard/doc

local p = {}

local function esc(s)
  if not s or s == '' then return nil end
  s = tostring(s)
  s = s:gsub("\\","\\\\"):gsub("\n","\\n"):gsub(",","\\,"):gsub(";","\\;")
  return s
end

local function line(label, value)
  if value and value ~= '' then return label .. ':' .. value end
  return nil
end

function p._vcard(a)
  local L = {}
  table.insert(L, 'BEGIN:VCARD')
  table.insert(L, 'VERSION:3.0')
  table.insert(L, line('FN', esc(a.name)))
  table.insert(L, line('ORG', esc(a.org)))
  table.insert(L, line('TITLE', esc(a.title)))
  table.insert(L, line('TEL;TYPE=CELL,VOICE', esc(a.phone)))
  table.insert(L, line('EMAIL;TYPE=INTERNET', esc(a.email)))

  if a.street or a.city or a.state or a.zip or a.country then
    local adr = table.concat({
      '', '', esc(a.street or ''), esc(a.city or ''),
      esc(a.state or ''), esc(a.zip or ''), esc(a.country or 'USA')
    }, ';')
    table.insert(L, 'ADR;TYPE=WORK:' .. adr)
  end

  table.insert(L, line('URL', esc(a.website)))
  if a.linkedin and a.linkedin ~= '' then
    table.insert(L, 'X-SOCIALPROFILE;type=linkedin:' .. esc(a.linkedin))
  end
  if a.instagram and a.instagram ~= '' then
    table.insert(L, 'X-SOCIALPROFILE;type=instagram:' .. esc(a.instagram))
  end
  if a.x and a.x ~= '' then
    table.insert(L, 'X-SOCIALPROFILE;type=x:' .. esc(a.x))
  end

  table.insert(L, 'END:VCARD')
  return table.concat(L, '\r\n')
end

function p.dataURI(frame)
  local a = frame:getParent().args
  local vcard = p._vcard(a)
  return mw.uri.encode(vcard, 'PATH')  -- URI-encode for data: URL
end

function p.plain(frame)
  local a = frame:getParent().args
  return p._vcard(a) -- raw vCard text (useful for QR codes)
end

return p