#!/usr/local/bin/perl5 -w # # web.calendar # # Generate a calendar. # # Generate a calendar which fills a page. Get the information # from a calendar file. # # Generate a page for each month which has an activity. # # Usage: # web.calendar [flags] # Flags: # --data datafile # This is the name of the calendar data file. It contains # which are of the form: # # Comment line # Year=1999 Introduces a new year # Month=10 Introduces a new month # 1=event Some event on the specified day. # TBA=event Some event at an unknown date within the month. # The event calendar must be in order. If not specified, the # event calendar's filename is assumed to be "calendar.data". # The events may contain HTML, but keep it simple. # --start 1998/10 # This specifies the starting month. This must be in the # format shown. If unspecified, it starts as early as possible. # --end 1999/05 # This specifies the ending month. If unspecified, the calendar # goes as long as possible. # --bg color # This specifies the background color. This can be anything # that HTML allows. This defaults to no specific color. # --title "some title" # This specifies the title of the calendar. # --banner "some message" # This is the header text of the calendar. # --html directory # This specifies the name of the directory to store # the generated files. By default, they will go into # the current directory. # # use strict; use Getopt::Long; use vars qw(%Activities $EndMonth $EndYear %Months @MonTab @MonTabShort %Params $StartMonth $StartYear $Version); %Params = ( "data" => "calendar.data", "start" => "0000/01", "end" => "9999/12", "bg" => "", "title" => "Calendar", "banner" => "Calendar", "html" => ".", ); @MonTab = ( "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); @MonTabShort = ( "", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"); $StartYear = 1000; $StartMonth = 1; $EndYear = 9999; $EndMonth = 12; $Version = 'web.calendar v0.10 regan@ao.com'; ### ### Main program ### my(@cal, $dom, $dow, $line, $mon, $mon2, $month, $next, $prev, $text, $year, $year2); # chdir($ENV{'HOME'}); GetOptions(\%Params, "data=s", "bg=s", "title=s", "banner=s", "html=s"); $Params{'bg'} = qq|bgcolor="$Params{'bg'}"| if ($Params{'bg'} ne ""); # Split the years and month ($StartYear, $StartMonth) = split(/[\D]/, $Params{'start'}); ($EndYear, $EndMonth) = split(/[\D]/, $Params{'end'}); ReadCalendar($Params{'data'}); for $month (sort(keys %Months)) { print "Handle $month\n"; ($year, $mon) = split(/\./, $month); $year2 = $year; $mon2 = $mon - 1; if ($mon2 == 0) { $mon2 = 12; $year2--; } $prev = "
"; $prev = "Previous" if (defined($Months{"$year2.$mon2"})); $year2 = $year; $mon2 = $mon + 1; if ($mon2 == 13) { $mon2 = 1; $year2++; } $next = "
"; $next = "Next" if (defined($Months{"$year2.$mon2"})); @cal = `cal $mon $year`; if (!open(WEB, ">$Params{'html'}/$year.$mon.html")) { print "Cannot open $Params{'html'}/$year.$mon.html: $!\n"; exit 1; } # Put in month header information print WEB << " EOF"; $Params{'title'} for $MonTab[$mon] $year EOF # Put in events for each of the days for ($line = 2; $line < 7; $line++) { chomp $cal[$line]; last if ($cal[$line] eq ""); print WEB ""; for ($dow = 0; $dow < 7; $dow++) { print WEB "\n"; } } print WEB "
$prev

$Params{'banner'}

$MonTab[$mon]

$year

$next
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
"; $dom = substr("$cal[$line] ", $dow * 3, 3); $dom = 0 if ($dom =~ /^\s*$/); $dom += 0; if ($dom > 0) { print WEB "\n"; print WEB "

$dom

"; $text = $Activities{$year, $mon, $dom} || ""; $text =~ s/\n/
\n/gm; print WEB $text; } print WEB "
\n"; close WEB; } exit 0; ### ### ReadCalendar ### ### Read a calendar file. ### sub ReadCalendar { my($fname) = @_; my($month, $name, $value, $year); if (!open(CAL, "<$fname")) { print "Cannot open $fname\n"; exit 1; } while () { chomp; next if (/^\s*#/ || /^\s*$/); if (/^([^=]*)=\s*(.*)/) { $value = $2; ($name = $1) =~ s/\s*//; if ($name eq "Year") { $year = $value; } elsif ($name eq "Month") { $month = $value; $Months{"$year.$month"} = 1; } elsif ($name =~ /^\d+|TBA$/) { $Months{"$year.$month"} = 1; if ($name eq "TBA") { $name = "1"; $value = "TBA $value"; } if (($year > $StartYear || ($year == $StartYear && $month >= $StartMonth)) && ($year < $EndYear || ($year == $EndYear && $month <= $EndMonth))) { $Activities{$year, $month, $name} .= "$value\n"; } } } } close CAL; }