) {
+ chomp;
+ last if /^\# rem2ps end$/;
+ next if /^\#/;
+ next unless m/^(\d*).(\d*).(\d*)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/;
+ my ($y, $m, $d, $special, $tag, $duration, $time, $body) =
+ ($1, $2, $3, $4, $5, $6, $7, $8);
+ if ($special eq 'HTML') {
+ push(@{$days->[$d]}, $body);
+ } elsif ($special eq 'MOON') {
+ if ($body =~ /(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/) {
+ my ($phase, $moonsize, $fontsize, $msg) = ($1, $2, $3, $4);
+ $moons->[$d]->{'phase'} = $phase;
+ $moons->[$d]->{'msg'} = $msg;
+ }
+ } elsif ($special eq 'SHADE') {
+ if ($body =~ /(\d+)\s+(\d+)\s+(\d+)/) {
+ $shades->[$d] = sprintf("#%02X%02X%02X",
+ $1 % 255, $2 % 255, $3 % 255);
+ }
+ } elsif ($special eq 'COLOR') {
+ if ($body =~ /(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/) {
+ my($r, $g, $b, $text) = ($1, $2, $3, $4);
+ my $color = sprintf("style=\"color: #%02X%02X%02X;\"",
+ $r % 255, $g % 255, $b % 255);
+ push(@{$days->[$d]}, '' . escape_html($text) . '
');
+ }
+ } elsif ($special eq '*') {
+ push(@{$days->[$d]}, '' . escape_html($body) . '
');
+ }
+ }
+ return $found_data;
+}
+
+=head2 small_calendar MONTH, MONLEN, URL, FIRST_COL
+
+Draw a small calendar
+
+=cut
+
+sub small_calendar
+{
+ my($month, $monlen, $url, $first_col) = @_;
+ if ($Mondayfirst) {
+ $first_col--;
+ if ($first_col < 0) {
+ $first_col = 6;
+ }
+ }
+
+ print "\n";
+ print "\n";
+ print "";
+ print "" if ($url);
+ print $month;
+ print "" if ($url);
+ print "\n";
+
+ print '';
+ if (!$Mondayfirst) {
+ print '| ' . substr($Daynames[0], 0, 1) . ' | ';
+ }
+ for (my $i=1; $i<7; $i++) {
+ print '' . substr($Daynames[$i], 0, 1) . ' | ';
+ }
+ if ($Mondayfirst) {
+ print '' . substr($Daynames[0], 0, 1) . ' | ';
+ }
+ print(" \n");
+ my $col = 0;
+ for (; $col<$first_col; $col++) {
+ if ($col == 0) {
+ print("\n");
+ }
+ print("| | \n");
+ }
+
+ for (my $day=1; $day <= $monlen; $day++) {
+ if ($col == 0) {
+ print(" \n");
+ }
+ $col++;
+ print("| $day | \n");
+ if ($col == 7) {
+ print(" \n");
+ $col = 0;
+ }
+ }
+ if ($col) {
+ while ($col < 7) {
+ print(" | \n");
+ $col++;
+ }
+ print("\n");
+ }
+ print(" \n");
+ print " | \n";
+}
+
+=head2 output_calendar
+
+Take the parsed input and output a calendar
+
+=cut
+
+sub output_calendar
+{
+ # Which column is 1st of month in?
+ my $first_col = $Firstwkday;
+ if ($Mondayfirst) {
+ $first_col--;
+ if ($first_col < 0) {
+ $first_col = 6;
+ }
+ }
+
+ # Last column
+ my $last_col = ($first_col + $Numdays - 1) % 7;
+
+
+ # Start the table
+ print '' .
+ $Month . ' ' . $Year . '' . "\n";
+ print '';
+ if (!$Mondayfirst) {
+ print '| ' . $Daynames[0] . ' | ';
+ }
+ for (my $i=1; $i<7; $i++) {
+ print '' . $Daynames[$i] . ' | ';
+ }
+ if ($Mondayfirst) {
+ print '' . $Daynames[0] . ' | ';
+ }
+ print "
\n";
+
+ # Start the calendar rows
+ my $col = 0;
+ print "\n";
+ if ($first_col > 0) {
+ small_calendar($Prevmon, $Prevlen, $Options{'backurl'},
+ ($Firstwkday - $Prevlen + 35) % 7);
+ $col++;
+ }
+
+ if ($last_col == 6) {
+ small_calendar($Nextmon, $Nextlen, $Options{'forwurl'},
+ ($Firstwkday + $Numdays) % 7);
+ $col++;
+ }
+ while ($col < $first_col) {
+ print("| | \n");
+ $col++;
+ }
+
+ for (my $day=1; $day<=$Numdays; $day++) {
+ draw_day_cell($day);
+ $col++;
+ if ($col == 7) {
+ $col = 0;
+ print "
\n";
+ if ($day < $Numdays) {
+ print "\n";
+ }
+ }
+ }
+
+ if ($col) {
+ while ($col < 7) {
+ if ($col == 5) {
+ if ($first_col == 0) {
+ small_calendar($Prevmon, $Prevlen, $Options{'backurl'},
+ ($Firstwkday - $Prevlen + 35) % 7);
+ } else {
+ print("| | \n");
+ }
+ } elsif ($col == 6) {
+ small_calendar($Nextmon, $Nextlen, $Options{'forwurl'},
+ ($Firstwkday + $Numdays) % 7);
+ } else {
+ print(" | \n");
+ }
+ $col++;
+ }
+ print "
\n";
+ }
+
+ # End the table
+ print "
\n";
+}
+
+sub draw_day_cell
+{
+ my($day) = @_;
+ my $shade = $shades->[$day];
+ if ($shade) {
+ $shade = " style=\"background: $shade;\"";
+ } else {
+ $shade = "";
+ }
+ print "\n";
+ if ($moons->[$day]) {
+ my $phase = $moons->[$day]->{'phase'};
+ my $msg = $moons->[$day]->{'msg'};
+ $msg ||= '';
+ if ($msg ne '') {
+ $msg = ' ' . escape_html($msg);
+ }
+ my $img;
+ if ($phase == 0) {
+ $img = 'newmoon.png';
+ } elsif ($phase == 1) {
+ $img = 'firstquarter.png';
+ } elsif ($phase == 2) {
+ $img = 'fullmoon.png';
+ } else {
+ $img = 'lastquarter.png';
+ }
+ print(" $msg ");
+ }
+
+ print "$day \n";
+ if ($days->[$day]) {
+ print(join("\n", @{$days->[$day]}));
+ }
+
+ print " | \n";
+}
+
+sub escape_html
+{
my($in) = @_;
$in =~ s/\&/\&/g;
$in =~ s/\\</g;
@@ -104,313 +386,31 @@ sub escape_html {
return $in;
}
-sub parse_options {
- %Options = ();
-
- GetOptions (\%Options, "help|h",
- "version",
- "border|b=i",
- "cellspace|t=i",
- "backurl|bu:s", "forwurl|fu:s",
- "tableonly|to",
- "prologue|p=s",
- "append|a=s",
- "bgcolor|g=s",
- "fs=s", "fh=s", "ft=s", "fe=s", "fd=s",
- "sh=i", "st=i", "se=i", "sd=i",
- );
-
- $Options{'border'} = "BORDER=" . ($Options{'border'} || 1);
- $Options{'cellspace'} &&= "CELLSPACING=$Options{'cellspace'}";
- $Options{'bgcolor'} &&= "BGCOLOR=$Options{'bgcolor'}";
+parse_options();
+if ($Options{'help'}) {
+ usage();
+} elsif ($Options{'version'}) {
+ print "rem2html version $rem2html_version.\n";
+ exit(0);
}
-sub parse_input {
- local $where = 0;
- local $msg;
- local $type;
- local $day;
- @days = ();
- @shades = ();
- @moons = ();
- while (<>) {
- chomp($_);
- if (/rem2(html|ps) begin/) {
- } elsif (!$where) {
- next;
- } elsif ($where == 1) {
- local ($month, $year);
- ($month, $year, $month_length, $firstday, $mfirst) = split(" ");
- $caption = "$month, $year";
- for $i ( 1 .. $month_length) { push(@days, ""); }
- } elsif ($where == 2) {
- @DayNames = split(" ");
- } elsif ($where == 3) {
- @prevsc = split(" ");
- } elsif ($where == 4) {
- @nextsc = split(" ");
- } else {
- return 1 if /rem2(html|ps) end/;
- next if /^\#/;
- next unless m/^(\d*).(\d*).(\d*)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/;
- $type = $4;
- $msg = $8;
- $day = $3;
- if ($type eq "HTML") {
- $days[$day] .= "$msg ";
- } elsif ($type eq "MOON") {
- my($phase, $text);
- if ($msg =~ /^\s*(\d+)\s+\S+\s+\S+\s+(.*)$/) {
- $phase = $1;
- $text = $2;
- } elsif ($msg =~ /^\s*(\d+)/) {
- $phase = $1;
- $text = "";
- } else {
- next;
- }
- next if ($phase > 3);
- if ($phase == 0) {
- $text = " " . escape_html($text);
- } elsif ($phase == 1) {
- $text = " " . escape_html($text);
- } elsif ($phase == 2) {
- $text = " " . escape_html($text);
- } else {
- $text = " " . escape_html($text);
- }
- $moons[$day] = $text . " ";
- } elsif ($type eq "SHADE") {
- my($red, $green, $blue);
- if ($msg =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/) {
- $red = $1;
- $green = $2;
- $blue = $3;
- } elsif ($msg =~ /^\s*(\d+)\s*$/) {
- $red = $1;
- $green = $1;
- $blue = $1;
- } else {
- next;
- }
- next if ($red > 255 || $green > 255 || $blue > 255);
- $shades[$day] = sprintf(" BGCOLOR=\"#%02X%02X%02X\"",
- $red, $green, $blue);
- } elsif ($type eq "COLOR") {
- my($red, $green, $blue, $stuff);
- if ($msg =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s*(.*)$/) {
- $red = $1;
- $green = $2;
- $blue = $3;
- $stuff = $4;
- next if ($red > 255 || $green > 255 || $blue > 255);
- $days[$day] .=
- sprintf("",
- $red, $green, $blue) .
- escape_html($stuff) . "
";
- } else {
- next;
- }
- } elsif ($type eq "*") {
- $msg = &escape_html($msg);
- $days[$day] .= "$msg
";
- }
- }
- $where++;
- }
- if ($where) {
- return 1;
- }
- return 0;
+if (-t STDIN) {
+ print STDERR "$0: Input should not come from a terminal.\n\n";
+ usage();
}
-sub output_header {
- local ($title, $dayheader);
-
- if (!$Options{'tableonly'}) {
- print <
- Reminders for $caption
-
-EndOfHTML
- }
-
- print <) and Remind (written by David F. Skoll). -->
-EndOfHTML
-
- if ($Options{'prologue'}) {
- open(PROLOGUE, "< $Options{'prologue'}");
- print while ( );
- close(PROLOGUE);
- }
-
- $caption = &format_font($caption, $Options{'ft'}, $Options{'st'} || "+1");
-
- print <
- $caption
-
-EndOfHTML
-
- $mfirst || &print_day_header($DayNames[0]);
-
- for($i=1; $i<7; $i++) {
- &print_day_header($DayNames[$i]);
- }
-
- $mfirst && &print_day_header($DayNames[0]);
- print "
\n";
+my $found_something = 0;
+while(1) {
+ last if (!parse_input());
+ start_output() unless $found_something;
+ $found_something = 1;
+ output_calendar();
+}
+if ($found_something) {
+ end_output();
+ exit(1);
+} else {
+ print STDERR "$0: Could not find any calendar data on STDIN.\n";
+ exit(1);
}
-sub output_footer {
- print "