#!/usr/bin/perl
sub
get_lines {
my
$file
=
shift
;
if
(
$file
=~ m{^https?://}) {
my
$content
= LWP::Simple::get(
$file
);
return
split
$/,
$content
;
}
return
File::Slurp::read_file(
$file
);
}
my
(
$year
,
$month
,
$who
);
my
%months
= (
January
=> 1,
February
=> 2,
March
=> 3,
April
=> 4,
May
=> 5,
June
=> 6,
July
=> 7,
August
=> 8,
September
=> 9,
October
=> 10,
November
=> 11,
December
=> 12 );
sub
guesstimate_event {
my
$event
=
shift
;
my
$when
=
$event
;
my
(
$firstday
,
$lastday
);
if
(
$when
=~ /(\d+)(?:st|nd|rd|th)?\s*(?:to|-)\s*(\d+)/i) {
(
$firstday
,
$lastday
) = ($1, $2);
}
elsif
(
$when
=~ /(\d+)/) {
(
$firstday
,
$lastday
) = ($1, $1);
}
else
{
warn
"didn't handle $event"
;
return
;
}
my
$start
= DateTime->new(
year
=>
$year
,
month
=>
$months
{
$month
},
day
=>
$firstday
,
);
my
$end
= DateTime->new(
year
=>
$year
,
month
=>
$months
{
$month
},
day
=>
$lastday
)->add(
days
=> 1,
(
$firstday
>
$lastday
? (
months
=> 1 ) : () )
);
return
{
type
=>
'VEVENT'
,
properties
=> {
SUMMARY
=> [ {
value
=>
$who
} ],
DESCRIPTION
=> [ {
value
=>
$event
} ],
DTSTART
=> [ {
value
=>
$start
->ymd(
''
),
param
=> {
VALUE
=>
'DATE'
},
} ],
DTEND
=> [ {
value
=>
$end
->ymd(
''
),
param
=> {
VALUE
=>
'DATE'
},
} ],
UID
=> [ {
value
=> md5_hex(
"$year $month $who - $event"
),
} ],
},
};
}
my
$cal
= {
type
=>
'VCALENDAR'
,
properties
=> {
'X-WR-CALNAME'
=> [ {
value
=>
"Fotango Holidays"
} ],
},
objects
=> [],
};
for
(get_lines(
$file
)) {
next
if
/^\s*$/;
/^= (.*) =/ and
do
{
$year
= $1;
next
};
/^== (.*) ==/ and
do
{
$month
= $1;
next
};
/^\* \[\[(.*)\]\]/ and
do
{
$who
= $1;
next
};
/^\*\* (.*)/ and
do
{
push
@{
$cal
->{objects} }, guesstimate_event( $1 );
next
;
};
}
print
CGI->header(
'text/calendar'
);
print
map
"$_\n"
, Text::vFile::asData->generate_lines(
$cal
);