ical.lua

Fri, 29 Mar 2013 15:01:47 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 29 Mar 2013 15:01:47 +0000
changeset 3
c16d25785df1
parent 1
fedd29ab7b49
permissions
-rw-r--r--

Perform line unfolding

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
3
c16d25785df1 Perform line unfolding
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
9 curr_event._last_key = k;
0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 curr_event[k] = v;
3
c16d25785df1 Perform line unfolding
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
11 elseif line:sub(1,1) == " " then
c16d25785df1 Perform line unfolding
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
12 curr_event[curr_event._last_key] = curr_event[curr_event._last_key]..line:sub(2);
c16d25785df1 Perform line unfolding
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
13 return;
0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if k == "DTSTAMP" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local t = {};
1
fedd29ab7b49 ical.lua: Handle day-only events (no time)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
18 t.year, t.month, t.day = v:match("^(%d%d%d%d)(%d%d)(%d%d)");
fedd29ab7b49 ical.lua: Handle day-only events (no time)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
19 t.hour, t.min, t.sec = v:match("T(%d%d)(%d%d)(%d%d)Z$");
0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 for k,v in pairs(t) do t[k] = tonumber(v); end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 curr_event.when = os.time(t);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 function load(data)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local ical, stack = {}, {};
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local line_num = 0;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- Parse
3
c16d25785df1 Perform line unfolding
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
30 local hold_buffer;
0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for line in data:gmatch("(.-)[\r\n]+") do
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 line_num = line_num + 1;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if line:match("^BEGIN:") then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local type = line:match("^BEGIN:(%S+)");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 table.insert(stack, type);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 table.insert(ical, { type = type });
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 elseif line:match("^END:") then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if stack[#stack] ~= line:match("^END:(%S+)") then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 return nil, "Parsing error, expected END:"..stack[#stack].." before line "..line_num;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 table.remove(stack);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 elseif handler[stack[#stack]] then
3
c16d25785df1 Perform line unfolding
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
43 handler[stack[#stack]](ical, (hold_buffer or "")..line);
0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- Return calendar
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return ical;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return _M;

mercurial