ical.lua

Mon, 26 Oct 2009 03:12:41 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 26 Oct 2009 03:12:41 +0000
changeset 0
11a0cbd6c216
child 1
fedd29ab7b49
permissions
-rw-r--r--

Initial commit

0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module("ical", package.seeall)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local handler = {};
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 function handler.VEVENT(ical, line)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local k,v = line:match("^(%w+):(.*)$");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local curr_event = ical[#ical];
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 if k and v then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 curr_event[k] = v;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if k == "DTSTAMP" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local t = {};
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 t.year, t.month, t.day, t.hour, t.min, t.sec = v:match("^(%d%d%d%d)(%d%d)(%d%d)T(%d%d)(%d%d)(%d%d)Z$");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 for k,v in pairs(t) do t[k] = tonumber(v); end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 curr_event.when = os.time(t);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function load(data)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local ical, stack = {}, {};
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local line_num = 0;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 -- Parse
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for line in data:gmatch("(.-)[\r\n]+") do
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 line_num = line_num + 1;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if line:match("^BEGIN:") then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local type = line:match("^BEGIN:(%S+)");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 table.insert(stack, type);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 table.insert(ical, { type = type });
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 elseif line:match("^END:") then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if stack[#stack] ~= line:match("^END:(%S+)") then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return nil, "Parsing error, expected END:"..stack[#stack].." before line "..line_num;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 table.remove(stack);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 elseif handler[stack[#stack]] then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 handler[stack[#stack]](ical, line);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 -- Return calendar
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return ical;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return _M;

mercurial