Wed, 14 Mar 2012 12:22:21 +0000
test.lua: Use arg[1] as input file if present
module("ical", package.seeall) local handler = {}; function handler.VEVENT(ical, line) local k,v = line:match("^(%w+):(.*)$"); local curr_event = ical[#ical]; if k and v then curr_event[k] = v; end if k == "DTSTAMP" then local t = {}; t.year, t.month, t.day = v:match("^(%d%d%d%d)(%d%d)(%d%d)"); t.hour, t.min, t.sec = v:match("T(%d%d)(%d%d)(%d%d)Z$"); for k,v in pairs(t) do t[k] = tonumber(v); end curr_event.when = os.time(t); end end function load(data) local ical, stack = {}, {}; local line_num = 0; -- Parse for line in data:gmatch("(.-)[\r\n]+") do line_num = line_num + 1; if line:match("^BEGIN:") then local type = line:match("^BEGIN:(%S+)"); table.insert(stack, type); table.insert(ical, { type = type }); elseif line:match("^END:") then if stack[#stack] ~= line:match("^END:(%S+)") then return nil, "Parsing error, expected END:"..stack[#stack].." before line "..line_num; end table.remove(stack); elseif handler[stack[#stack]] then handler[stack[#stack]](ical, line); end end -- Return calendar return ical; end return _M;