Comparison

util/vcard.lua @ 8382:e5d00bf4a4d5

util: Various minor changes to please [luacheck]
author Kim Alvefur <zash@zash.se>
date Fri, 10 Nov 2017 05:42:32 +0100 (2017-11-10)
parent 6400:95241c0f9244
child 9058:35421a289a75
comparison
equal deleted inserted replaced
8381:7f6184474149 8382:e5d00bf4a4d5
8 -- Fix folding. 8 -- Fix folding.
9 9
10 local st = require "util.stanza"; 10 local st = require "util.stanza";
11 local t_insert, t_concat = table.insert, table.concat; 11 local t_insert, t_concat = table.insert, table.concat;
12 local type = type; 12 local type = type;
13 local next, pairs, ipairs = next, pairs, ipairs; 13 local pairs, ipairs = pairs, ipairs;
14 14
15 local from_text, to_text, from_xep54, to_xep54; 15 local from_text, to_text, from_xep54, to_xep54;
16 16
17 local line_sep = "\n"; 17 local line_sep = "\n";
18 18
19 local vCard_dtd; -- See end of file 19 local vCard_dtd; -- See end of file
20 local vCard4_dtd; 20 local vCard4_dtd;
21
22 local function fold_line()
23 error "Not implemented" --TODO
24 end
25 local function unfold_line()
26 error "Not implemented"
27 -- gsub("\r?\n[ \t]([^\r\n])", "%1");
28 end
29 21
30 local function vCard_esc(s) 22 local function vCard_esc(s)
31 return s:gsub("[,:;\\]", "\\%1"):gsub("\n","\\n"); 23 return s:gsub("[,:;\\]", "\\%1"):gsub("\n","\\n");
32 end 24 end
33 25
114 data = data -- unfold and remove empty lines 106 data = data -- unfold and remove empty lines
115 :gsub("\r\n","\n") 107 :gsub("\r\n","\n")
116 :gsub("\n ", "") 108 :gsub("\n ", "")
117 :gsub("\n\n+","\n"); 109 :gsub("\n\n+","\n");
118 local vCards = {}; 110 local vCards = {};
119 local c; -- current item 111 local current;
120 for line in data:gmatch("[^\n]+") do 112 for line in data:gmatch("[^\n]+") do
121 local line = vCard_unesc(line); 113 line = vCard_unesc(line);
122 local name, params, value = line:match("^([-%a]+)(\30?[^\29]*)\29(.*)$"); 114 local name, params, value = line:match("^([-%a]+)(\30?[^\29]*)\29(.*)$");
123 value = value:gsub("\29",":"); 115 value = value:gsub("\29",":");
124 if #params > 0 then 116 if #params > 0 then
125 local _params = {}; 117 local _params = {};
126 for k,isval,v in params:gmatch("\30([^=]+)(=?)([^\30]*)") do 118 for k,isval,v in params:gmatch("\30([^=]+)(=?)([^\30]*)") do
137 end 129 end
138 end 130 end
139 params = _params; 131 params = _params;
140 end 132 end
141 if name == "BEGIN" and value == "VCARD" then 133 if name == "BEGIN" and value == "VCARD" then
142 c = {}; 134 current = {};
143 vCards[#vCards+1] = c; 135 vCards[#vCards+1] = current;
144 elseif name == "END" and value == "VCARD" then 136 elseif name == "END" and value == "VCARD" then
145 c = nil; 137 current = nil;
146 elseif c and vCard_dtd[name] then 138 elseif current and vCard_dtd[name] then
147 local dtd = vCard_dtd[name]; 139 local dtd = vCard_dtd[name];
148 local p = { name = name }; 140 local item = { name = name };
149 c[#c+1]=p; 141 t_insert(current, item);
150 --c[name]=p; 142 local up = current;
151 local up = c; 143 current = item;
152 c = p;
153 if dtd.types then 144 if dtd.types then
154 for _, t in ipairs(dtd.types) do 145 for _, t in ipairs(dtd.types) do
155 local t = t:lower(); 146 t = t:lower();
156 if ( params.TYPE and params.TYPE[t] == true) 147 if ( params.TYPE and params.TYPE[t] == true)
157 or params[t] == true then 148 or params[t] == true then
158 c.TYPE=t; 149 current.TYPE=t;
159 end 150 end
160 end 151 end
161 end 152 end
162 if dtd.props then 153 if dtd.props then
163 for _, p in ipairs(dtd.props) do 154 for _, p in ipairs(dtd.props) do
164 if params[p] then 155 if params[p] then
165 if params[p] == true then 156 if params[p] == true then
166 c[p]=true; 157 current[p]=true;
167 else 158 else
168 for _, prop in ipairs(params[p]) do 159 for _, prop in ipairs(params[p]) do
169 c[p]=prop; 160 current[p]=prop;
170 end 161 end
171 end 162 end
172 end 163 end
173 end 164 end
174 end 165 end
175 if dtd == "text" or dtd.value then 166 if dtd == "text" or dtd.value then
176 t_insert(c, value); 167 t_insert(current, value);
177 elseif dtd.values then 168 elseif dtd.values then
178 local value = "\30"..value; 169 for p in ("\30"..value):gmatch("\30([^\30]*)") do
179 for p in value:gmatch("\30([^\30]*)") do 170 t_insert(current, p);
180 t_insert(c, p); 171 end
181 end 172 end
182 end 173 current = up;
183 c = up;
184 end 174 end
185 end 175 end
186 return vCards; 176 return vCards;
187 end 177 end
188 178
320 end 310 end
321 end 311 end
322 312
323 local vcard4 = { } 313 local vcard4 = { }
324 314
325 function vcard4:text(node, params, value) 315 function vcard4:text(node, params, value) -- luacheck: ignore 212/params
326 self:tag(node:lower()) 316 self:tag(node:lower())
327 -- FIXME params 317 -- FIXME params
328 if type(value) == "string" then 318 if type(value) == "string" then
329 self:tag("text"):text(value):up() 319 self:tag("text"):text(value):up()
330 elseif vcard4[node] then 320 elseif vcard4[node] then