#!/usr/bin/perl -Tw use strict; $|++; use CGI qw(:all); use HTML::CalendarMonthSimple; use Cache::FileCache; use URI::Find; my $PLANFILE = "/home/merlyn/.plan"; my ($formonth, $foryear); { my @NOW = localtime; $formonth = param('month'); $formonth = $NOW[4]+1 unless defined $formonth and $formonth !~ /\D/ and $formonth >= 1 and $formonth <= 12; $foryear = param('year'); $foryear = $NOW[5]+1900 unless defined $foryear and $foryear !~ /\D/ and $foryear >= 2001 and $foryear <= 2005; } my $cache = Cache::FileCache->new({namespace => 'whereami', username => 'nobody'}); my %events; my @nowidentity = (stat($PLANFILE))[0,1,9]; if (my $cached = $cache->get('data')) { my ($events, @identity) = @$cached; if ("@nowidentity" eq "@identity") { ## we have a valid cache %events = %$events; } } unless (%events) { ## no cache, so compute from scratch require Date::Manip; local $ENV{PATH} = ""; Date::Manip->import; @ARGV = $PLANFILE; while (<>) { next unless /^(\d+)\s+to\s+(\d+)(\s+\S+\s+\d+):\s+(.*)/ or /^(\d+\s+\S+)\s+to\s+(\d+\s+\S+)(\s+\d+):\s+(.*)/ or /^(\d+\s+\S+\s+\d+)\s+to\s+(\d+\s+\S+\s+\d+)():\s+(.*)/; my ($start, $end, $where) = ("$1$3","$2$3", $4); $end = DateCalc($end, "+ 1 day"); for (ParseRecur("every day", undef, $start, $end)) { my ($y, $m, $d) = UnixDate($_, "%Y", "%m", "%d"); push @{$events{0+$y}{0+$m}}, [$d, $where]; } } $cache->set('data', [\%events, @nowidentity]); } my $cal = HTML::CalendarMonthSimple->new(year => $foryear, month => $formonth); $cal->width('100%'); $cal->bgcolor('white'); $cal->todaycolor('grey'); $cal->bordercolor('black'); $cal->contentcolor('black'); $cal->todaycontentcolor('black'); $cal->headercolor('#ccffcc'); { my $myself = url(-relative => 1); my $previous = sprintf "%s?year=%d&month=%d", $myself, $formonth == 1 ? ($foryear - 1, 12) : ($foryear, $formonth - 1); my $next = sprintf "%s?year=%d&month=%d", $myself, $formonth == 12 ? ($foryear + 1, 1) : ($foryear, $formonth + 1); $cal->header(table({width => '100%', border => 0, cellspacing => 0, cellpadding => 2}, Tr(td({align => 'left', width => '1*'}, a({href => $previous}, "previous")), td({align => 'center', width => '1*'}, b($cal->monthname, $cal->year)), td({align => 'right', width => '1*'}, a({href => $next}, "next"))))); } print header, start_html("My Calendar for ".$cal->monthname." ".$cal->year); for (@{$events{0+$foryear}{0+$formonth}}) { my ($d, $where) = @$_; for ($where) { find_uris($_, sub {my ($uri, $text) = @_; qq{\1\1$text\1\1} }); s/\G(.*?)(?:\001(.*?)\001)?/escapeHTML($1).(defined $2 ? $2 : "")/eig; } $cal->addcontent(0+$d, $where); } { local $^W = 0; print $cal->as_HTML; } print end_html;