Skip to content

Instantly share code, notes, and snippets.

@pen
Created November 6, 2011 09:33
Show Gist options
  • Save pen/1342694 to your computer and use it in GitHub Desktop.
Save pen/1342694 to your computer and use it in GitHub Desktop.
HTML calendar 2
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Time::Piece;
use Text::Xslate;
use Encode;
render_calendar(@ARGV); # cal_html.pl 5 2011
sub render_calendar
{
my ($month, $year) = @_;
my $vars = {
month => $month,
year => $year,
tp => localtime() + 0,
};
my $template = do { local $/; <DATA> };
my $tx = Text::Xslate->new();
print encode('utf-8', $tx->render_string($template, $vars));
}
__DATA__
<style type="text/css">
<!--
td { text-align: right; }
.dow_0 { background-color: pink; }
.dow_6 { background-color: skyblue; }
.out { color: #999; }
-->
</style>
<table border="1">
<tr>
: constant DAYNAME = [ "日", "月", "火", "水", "木", "金", "土" ];
: for [0..6] -> $n {
<th class="dow_<: $n :>"><: DAYNAME[$n] :></th>
: }
</tr>
<tr>
: my $f = $tp.strptime(($year || $tp.year) ~ " " ~ ($month || $tp.mon), "%Y %m");
: for [-$f._wday .. -1] -> $n {
: my $d = $f.add($n * 86400);
<td class="dow_<: $d._wday :> out"><: $d.mday :></td>
: }
: for [0 .. $f.month_last_day-1] -> $n {
: my $d = $f.add($n * 86400);
: if $d._wday == 0 && $n > 0 {
</tr>
<tr>
: }
<td class="dow_<: $d._wday :>"><: $d.mday :></td>
: }
: my $f2 = $f.add($f.month_last_day * 86400);
: for [0 .. (7 - $f2._wday) % 7 - 1] -> $n {
: my $d = $f2.add($n * 86400);
<td class="dow_<: $d._wday :> out"><: $d.mday :></td>
: }
</tr>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment