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