ical.lua

changeset 3
c16d25785df1
parent 1
fedd29ab7b49
equal deleted inserted replaced
2:19d109c1625f 3:c16d25785df1
4 4
5 function handler.VEVENT(ical, line) 5 function handler.VEVENT(ical, line)
6 local k,v = line:match("^(%w+):(.*)$"); 6 local k,v = line:match("^(%w+):(.*)$");
7 local curr_event = ical[#ical]; 7 local curr_event = ical[#ical];
8 if k and v then 8 if k and v then
9 curr_event._last_key = k;
9 curr_event[k] = v; 10 curr_event[k] = v;
11 elseif line:sub(1,1) == " " then
12 curr_event[curr_event._last_key] = curr_event[curr_event._last_key]..line:sub(2);
13 return;
10 end 14 end
11 15
12 if k == "DTSTAMP" then 16 if k == "DTSTAMP" then
13 local t = {}; 17 local t = {};
14 t.year, t.month, t.day = v:match("^(%d%d%d%d)(%d%d)(%d%d)"); 18 t.year, t.month, t.day = v:match("^(%d%d%d%d)(%d%d)(%d%d)");
21 function load(data) 25 function load(data)
22 local ical, stack = {}, {}; 26 local ical, stack = {}, {};
23 local line_num = 0; 27 local line_num = 0;
24 28
25 -- Parse 29 -- Parse
30 local hold_buffer;
26 for line in data:gmatch("(.-)[\r\n]+") do 31 for line in data:gmatch("(.-)[\r\n]+") do
27 line_num = line_num + 1; 32 line_num = line_num + 1;
28 if line:match("^BEGIN:") then 33 if line:match("^BEGIN:") then
29 local type = line:match("^BEGIN:(%S+)"); 34 local type = line:match("^BEGIN:(%S+)");
30 table.insert(stack, type); 35 table.insert(stack, type);
33 if stack[#stack] ~= line:match("^END:(%S+)") then 38 if stack[#stack] ~= line:match("^END:(%S+)") then
34 return nil, "Parsing error, expected END:"..stack[#stack].." before line "..line_num; 39 return nil, "Parsing error, expected END:"..stack[#stack].." before line "..line_num;
35 end 40 end
36 table.remove(stack); 41 table.remove(stack);
37 elseif handler[stack[#stack]] then 42 elseif handler[stack[#stack]] then
38 handler[stack[#stack]](ical, line); 43 handler[stack[#stack]](ical, (hold_buffer or "")..line);
39 end 44 end
40 end 45 end
41 46
42 -- Return calendar 47 -- Return calendar
43 return ical; 48 return ical;

mercurial