diff --git a/rem2html/rem2html.in b/rem2html/rem2html.in index 7e1f0876..18031dce 100644 --- a/rem2html/rem2html.in +++ b/rem2html/rem2html.in @@ -447,16 +447,16 @@ sub parse_input_ppp print STDERR "Could not decode JSON.\n"; exit(1); } - if (exists($array->[0]{caltype}) && - $array->[0]{caltype} ne 'monthly') { - print STDERR "Sorry, $TIDY_PROGNAME can only produce monthly calendars.\n"; - exit(1); - } if (!$did_a_calendar) { start_output(); $did_a_calendar = 1; } - emit_ppp_calendars($array); + if (exists($array->[0]{caltype}) && + $array->[0]{caltype} eq 'weekly') { + emit_ppp_calendars($array, 'weekly'); + } else { + emit_ppp_calendars($array, 'monthly'); + } $json = ''; } } @@ -471,15 +471,15 @@ sub parse_input_ppp sub emit_ppp_calendars { - my ($array) = @_; + my ($array, $type) = @_; foreach my $cal (@$array) { - emit_one_ppp_calendar($cal); + emit_one_ppp_calendar($cal, $type); } } sub emit_one_ppp_calendar { - my ($c) = @_; + my ($c, $type) = @_; undef $days; undef $shades; @@ -487,19 +487,32 @@ sub emit_one_ppp_calendar undef $classes; undef $weeks; + my $dates_to_day_index; + my $cols_to_date_info; if (exists($c->{translations})) { $Translations = $c->{translations}; } - $Month = $c->{monthname}; - $Year = $c->{year}; - $Numdays = $c->{daysinmonth}; - $Firstwkday = $c->{firstwkday}; - $Mondayfirst = $c->{mondayfirst}; - @Daynames = @{$c->{daynames}}; - $Prevmon = $c->{prevmonthname}; - $Prevlen = $c->{daysinprevmonth}; - $Nextmon = $c->{nextmonthname}; - $Nextlen = $c->{daysinnextmonth}; + if ($type eq 'monthly') { + $Month = $c->{monthname}; + $Year = $c->{year}; + $Numdays = $c->{daysinmonth}; + $Firstwkday = $c->{firstwkday}; + $Mondayfirst = $c->{mondayfirst}; + @Daynames = @{$c->{daynames}}; + $Prevmon = $c->{prevmonthname}; + $Prevlen = $c->{daysinprevmonth}; + $Nextmon = $c->{nextmonthname}; + $Nextlen = $c->{daysinnextmonth}; + } else { + my $idx = 0; + $Numdays = 7; + foreach my $date (@{$c->{dates}}) { + $Daynames[$idx] = $date->{dayname}; + $dates_to_day_index->{$date->{date}} = $idx; + $cols_to_date_info->[$idx] = $date; + $idx++; + } + } my $class; if ($Options{nostyle}) { @@ -512,37 +525,42 @@ sub emit_one_ppp_calendar my $y = $1; my $m = $2; my $d = $3; + my $col; + if ($type eq 'weekly') { + $col = $dates_to_day_index->{$obj->{date}}; + } else { + $col = $d; + $col =~ s/^0+//; + } my $special = $obj->{passthru} || '*'; my $tag = $obj->{tags} || '*'; my $duration = $obj->{duration} || '*'; my $time = $obj->{time} || '*'; my $body = $obj->{body}; - my $d1 = $d; - $d1 =~ s/^0+//; $special = uc($special); if ($special eq 'HTML') { - push(@{$days->[$d]}, $body); + push(@{$days->[$col]}, $body); } elsif ($special eq 'HTMLCLASS') { - $classes->[$d] = $body; + $classes->[$col] = $body; } elsif ($special eq 'WEEK') { $body =~ s/^\s+//; $body =~ s/\s+$//; - $weeks->{$d1} = $body; + $weeks->{$col} = $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; + $moons->[$col]->{'phase'} = $phase; + $moons->[$col]->{'msg'} = $msg; } elsif ($body =~ /(\S+)/) { - $moons->[$d]->{'phase'} = $1; - $moons->[$d]->{'msg'} = ''; + $moons->[$col]->{'phase'} = $1; + $moons->[$col]->{'msg'} = ''; } } elsif ($special eq 'SHADE') { if ($body =~ /(\d+)\s+(\d+)\s+(\d+)/) { - $shades->[$d] = sprintf("#%02X%02X%02X", + $shades->[$col] = sprintf("#%02X%02X%02X", ($1 % 256), ($2 % 256), ($3 % 256)); } elsif ($body =~ /(\d+)/) { - $shades->[$d] = sprintf("#%02X%02X%02X", + $shades->[$col] = sprintf("#%02X%02X%02X", ($1 % 256), ($1 % 256), ($1 % 256)); } } elsif ($special eq 'COLOR' || $special eq 'COLOUR') { @@ -550,13 +568,13 @@ sub emit_one_ppp_calendar my($r, $g, $b, $text) = ($1, $2, $3, $4); my $color = sprintf("style=\"color: #%02X%02X%02X;\"", $r % 256, $g % 256, $b % 256); - push(@{$days->[$d]}, "
" . fix_whitespace(escape_html($text)) . '
'); + push(@{$days->[$col]}, "" . fix_whitespace(escape_html($text)) . '
'); } } elsif ($special eq '*') { - push(@{$days->[$d]}, "" . fix_whitespace(escape_html($body)) . '
'); + push(@{$days->[$col]}, "" . fix_whitespace(escape_html($body)) . '
'); } } - output_calendar(); + output_calendar($type, $cols_to_date_info); } sub fix_whitespace @@ -664,23 +682,33 @@ sub small_calendar 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; - } - } + my ($type, $date_info) = @_; - # Last column - my $last_col = ($first_col + $Numdays - 1) % 7; + my ($first_col, $last_col, $number_of_rows); - # Figure out how many rows - my $number_of_rows = int(($first_col + $Numdays ) / 7 + 0.999); + if ($type eq 'monthly') { + $first_col = $Firstwkday; + if ($Mondayfirst) { + $first_col--; + if ($first_col < 0) { + $first_col = 6; + } + } - # Add a row for small calendars if necessary - if ($first_col == 0 && $last_col == 6) { + # Last column + $last_col = ($first_col + $Numdays - 1) % 7; + + # Figure out how many rows + $number_of_rows = int(($first_col + $Numdays ) / 7 + 0.999); + + # Add a row for small calendars if necessary + if ($first_col == 0 && $last_col == 6) { $number_of_rows++; + } + } else { + $first_col = 0; + $last_col = 6; + $number_of_rows = 1; } # Start the table @@ -691,22 +719,36 @@ sub output_calendar print '| " . $Daynames[0] . ' | '; + if ($type eq 'monthly') { + if (!$Mondayfirst) { + print "" . $Daynames[0] . ' | '; + } + for (my $i=1; $i<7; $i++) { + print "" . $Daynames[$i] . ' | '; + } + if ($Mondayfirst) { + print "" . $Daynames[0] . ' | '; + } + } else { + for (my $i=0; $i<7; $i++) { + my $inf = $date_info->[$i]; + print "" . $inf->{dayname} . " " . + $inf->{day} . ' ' . + $inf->{month} . ' ' . + $inf->{year} . ' | ';
+ }
}
- for (my $i=1; $i<7; $i++) {
- print "" . $Daynames[$i] . ' | '; - } - if ($Mondayfirst) { - print "" . $Daynames[0] . ' | '; - } - print "
|---|---|---|---|---|---|---|
| \n"); - } - } elsif ($col == 6) { - small_calendar($Nextmon, $Nextlen, $Options{forwurl}, - ($Firstwkday + $Numdays) % 7); - } else { - print(" | \n"); - } - $col++; - } - print " | \n"); + } + } elsif ($col == 6) { + small_calendar($Nextmon, $Nextlen, $Options{forwurl}, + ($Firstwkday + $Numdays) % 7); + } else { + print(" | \n"); + } + $col++; + } + print "\n"; + } - # Add a row for small calendars if they were not yet done! - if ($first_col == 0 && $last_col == 6) { + # Add a row for small calendars if they were not yet done! + if ($first_col == 0 && $last_col == 6) { if ($Options{nostyle}) { - print " | |||
\n"; + if ($type eq 'monthly') { + if ($Options{nostyle}) { + print "
\n"; + } else { + print "
\n"; + } else { + print "