The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

HTML::Make::Calendar - Make an HTML calendar

SYNOPSIS

    use HTML::Make::Calendar 'calendar';
    my $cal = calendar ();
    print $cal->text ();
    my $oldcal = calendar (year => 1966, month => 3);
    print $oldcal->text ();
    

The output HTML looks like this:

January 2021
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
March 1966
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

(This example is included as synopsis.pl in the distribution.)

VERSION

This documents version 0.01 of HTML-Make-Calendar corresponding to git commit 0a44bc7f4d5e94e6c7729395bd3083814175fb90 released on Sun Mar 14 09:01:16 2021 +0900.

DESCRIPTION

This module constructs HTML calendars.

FUNCTIONS

calendar

    my $out = calendar (year => 2010, month => 10);

Make the calendar. The return value is an HTML::Make object. To get the actual HTML, call its text method:

    use HTML::Make::Calendar 'calendar';
    my $out = calendar (year => 2021, month => 1);
    print $out->text ();

The output HTML looks like this:

January 2021
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

(This example is included as calendar.pl in the distribution.)

The possible arguments are

cdata

Callback data, see "dayc".

day_html

Override the HTML element used to make the "day" cells. The default is td. If you override this then you also need to override the parent elements, otherwise HTML::Make will fuss about compatibility.

dayc

Day callback which fills in the "day" cell of the calendar. If this is omitted, a default element is added. The day callback is called with three arguments, first "cdata", your data, second the date as a hash reference with arguments year, month and dom (day of month, a number from 1 to 31), and third the HTML element to attach the return value to, representing the cell of the calendar, like this:

    &{$dayc} ($cdata, {year => 2020, month => 12, dom => 21}, $td);

where $td is an HTML::Make object.

daynames

Specify the names of the days. See "Japanese calendar" for an example.

first

The first day of the week. The default is 1 for Monday. Specify 7 to start on Sunday:

    use HTML::Make::Calendar 'calendar';
    my $cal = calendar (first => 7);
    print $cal->text ();
    

The output HTML looks like this:

January 2021
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

(This example is included as first.pl in the distribution.)

Any other day of the week may also be used, for example specify 3 to start the weeks on Wednesdays.

month

The month, as a number from 1 to 12. If the month is omitted, the current month is used as given by "Today" in Date::Calc.

month_html

The HTML element used to make a month of the calendar. The default is table. You don't need to supply < and >, just the alphabetic part of the HTML element, as with the parent module HTML::Make.

monthc

Callback for month and year name. See "Japanese calendar" for an example.

week_html

The HTML element used to make a week of the calendar. The default is tr. You don't need to supply < and >, just the alphabetic part of the HTML element, as with the parent module HTML::Make.

weekless

Set to a true value to not use weeks. If you switch off weeks, the return value is the HTML elements but not subdivided into week blocks but whose parent is the month. This is for people who want to style their calendars with CSS, such as a CSS grid, rather than using HTML tables.

year

The year, as a four-digit number like 2020. If the year is omitted, the current year is used, as given by "Today" in Date::Calc.

Phases of the moon

This example demonstrates the use of "dayc" and "cdata" by adding the phase of the moon to your calendar. It requires Astro::MoonPhase (not included with this distribution).

    use utf8;
    use HTML::Make::Calendar 'calendar';
    use Astro::MoonPhase;
    use Date::Calc 'Date_to_Time';
    my @moons = qw!🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘!;
    my $cal = calendar (dayc => \&daymoon, cdata => \@moons);
    print $cal->text ();
    exit;
    
    sub daymoon
    {
        my ($moons, $date, $element) = @_;
        my $epochtime = Date_to_Time ($date->{year}, $date->{month},
                                      $date->{dom}, 0, 0, 0);
        my ($phase) = phase ($epochtime);
        my $text = $moons->[int (8*$phase)] . " <b>$date->{dom}</b>";
        $element->add_text ($text);
    }
    

The output HTML looks like this:

March 2021
Mo Tu We Th Fr Sa Su
๐ŸŒ• 1 ๐ŸŒ• 2 ๐ŸŒ– 3 ๐ŸŒ– 4 ๐ŸŒ– 5 ๐ŸŒ– 6 ๐ŸŒ— 7
๐ŸŒ— 8 ๐ŸŒ— 9 ๐ŸŒ˜ 10 ๐ŸŒ˜ 11 ๐ŸŒ˜ 12 ๐ŸŒ˜ 13 ๐ŸŒ‘ 14
๐ŸŒ‘ 15 ๐ŸŒ‘ 16 ๐ŸŒ‘ 17 ๐ŸŒ’ 18 ๐ŸŒ’ 19 ๐ŸŒ’ 20 ๐ŸŒ’ 21
๐ŸŒ“ 22 ๐ŸŒ“ 23 ๐ŸŒ“ 24 ๐ŸŒ“ 25 ๐ŸŒ” 26 ๐ŸŒ” 27 ๐ŸŒ” 28
๐ŸŒ• 29 ๐ŸŒ• 30 ๐ŸŒ• 31

(This example is included as moon.pl in the distribution.)

Daily menu

This example demonstrates the use of "dayc" and "cdata", and how to add your own HTML into the cells of the calendar.

    use utf8;
    use FindBin '$Bin';
    use HTML::Make::Calendar 'calendar';
    my @foods = split '', <<EOF;
    🍇🍈🍉🍊🍋🍌🍍🥭🍎🍏🍐🍑🍒🍓🥝🍅🥝
    🍅🥒🥬🥦🧄🧅🍄🥜🌰🍘🍙🍚🍛🍜🍝🍠🍢
    🍣🍤🍥🥮🍡🥟🥠🥡🦪🍦🍧🍨🍩🍪🎂🍰🧁
    EOF
    @foods = grep {!/\s/} @foods;
    my $cal = calendar (cdata => \@foods, dayc => \&add_food);
    print $cal->text ();
    exit;
    
    sub add_food
    {
        my ($foods, $date, $element) = @_;
        my $today = 
        $element->push ('span', text => $date->{dom});
        my $menu = HTML::Make->new ('ol');
        for (1..3) {
            my $food = $foods->[int (rand (@$foods))];
            $menu->push ('li', text => $food);
        }
        $element->push ($menu);
    }

The output HTML looks like this:

January 2021
Mo Tu We Th Fr Sa Su
1
  1. ๐Ÿ
  2. ๐Ÿ…
  3. ๐Ÿ…
2
  1. ๐Ÿง„
  2. ๐ŸŽ‚
  3. ๐ŸŒฐ
3
  1. ๐ŸŒ
  2. ๐Ÿ…
  3. ๐Ÿง„
4
  1. ๐Ÿช
  2. ๐Ÿง…
  3. ๐Ÿ‘
5
  1. ๐ŸŽ
  2. ๐Ÿฅฆ
  3. ๐Ÿ‡
6
  1. ๐Ÿ›
  2. ๐Ÿ…
  3. ๐Ÿฆช
7
  1. ๐Ÿ“
  2. ๐Ÿ…
  3. ๐Ÿ 
8
  1. ๐Ÿ‘
  2. ๐Ÿ
  3. ๐Ÿ’
9
  1. ๐Ÿ‡
  2. ๐Ÿ
  3. ๐Ÿฅ
10
  1. ๐Ÿฅ 
  2. ๐ŸŒ
  3. ๐Ÿฅ
11
  1. ๐Ÿฅœ
  2. ๐Ÿฅœ
  3. ๐ŸŒ
12
  1. ๐Ÿ‘
  2. ๐Ÿ™
  3. ๐Ÿฆช
13
  1. ๐Ÿฅฎ
  2. ๐Ÿ‘
  3. ๐Ÿœ
14
  1. ๐Ÿš
  2. ๐Ÿง
  3. ๐Ÿ„
15
  1. ๐Ÿœ
  2. ๐Ÿฆช
  3. ๐Ÿš
16
  1. ๐ŸŒฐ
  2. ๐Ÿฉ
  3. ๐Ÿˆ
17
  1. ๐ŸฅŸ
  2. ๐Ÿฆ
  3. ๐ŸŒฐ
18
  1. ๐Ÿ
  2. ๐Ÿ„
  3. ๐Ÿฅฎ
19
  1. ๐Ÿ…
  2. ๐Ÿก
  3. ๐Ÿ
20
  1. ๐Ÿ
  2. ๐Ÿ‹
  3. ๐Ÿ
21
  1. ๐Ÿฅญ
  2. ๐Ÿค
  3. ๐Ÿข
22
  1. ๐Ÿœ
  2. ๐Ÿค
  3. ๐Ÿฃ
23
  1. ๐Ÿง
  2. ๐Ÿ’
  3. ๐Ÿ…
24
  1. ๐Ÿง
  2. ๐Ÿ˜
  3. ๐ŸŽ‚
25
  1. ๐Ÿฐ
  2. ๐Ÿ’
  3. ๐Ÿค
26
  1. ๐Ÿ“
  2. ๐Ÿ‘
  3. ๐Ÿ 
27
  1. ๐Ÿ
  2. ๐Ÿ„
  3. ๐Ÿง
28
  1. ๐Ÿฅ 
  2. ๐Ÿฃ
  3. ๐Ÿ’
29
  1. ๐Ÿง„
  2. ๐Ÿ“
  3. ๐Ÿง
30
  1. ๐Ÿฅ’
  2. ๐Ÿง…
  3. ๐Ÿฅก
31
  1. ๐Ÿง„
  2. ๐ŸฅŸ
  3. ๐Ÿ‡

(This example is included as menu.pl in the distribution.)

Japanese calendar

This example shows making a Japanese calendar using "daynames" as well as "monthc" to put the month name into Japanese. It uses Calendar::Japanese::Holiday, Date::Qreki, Lingua::JA::Numbers, and Lingua::JA::FindDates to make various bits of information typically found on Japanese calendars.

    use utf8;
    use HTML::Make::Calendar 'calendar';
    use Date::Qreki 'rokuyou_unicode';
    use Calendar::Japanese::Holiday;
    use Lingua::JA::Numbers 'num2ja';
    use Lingua::JA::FindDates 'seireki_to_nengo';
    my @daynames = (qw!月 火 水 木 金 土 日!);
    my $calendar = calendar (daynames => \@daynames,
                             monthc => \&jmonth,
                             dayc => \&jday, first => 7);
    print $calendar->text ();
    exit;
    
    sub jday
    {
        my (undef, $date, $element) = @_;
        my @jdate = ($date->{year}, $date->{month}, $date->{dom});
        my $name = isHoliday (@jdate);
        my $rokuyou = rokuyou_unicode (@jdate);
        $element->push ('span', text => num2ja ($date->{dom}));
        $element->push ('br');
        $element->push ('span', text => $rokuyou, attr => {class => 'rokuyou'});
        if ($name) {
            $element->push ('br');
            $element->push ('b', text => $name);
            $element->add_class ('holiday');
        }
    }
    
    sub jmonth
    {
        my (undef, $date, $element) = @_;
        my $month = $date->{month} . '月';
        my $year = seireki_to_nengo ("$date->{year}年");
        my $ym = "$year$month";
        $ym =~ s/([0-9]+)/num2ja($1)/ge;
        $element->add_text ($ym);
    }

The output HTML looks like this:

ไปคๅ’Œไธ‰ๅนดไธ‰ๆœˆ
ๆ—ฅ ๆœˆ ็ซ ๆฐด ๆœจ ้‡‘ ๅœŸ
ไธ€
่ตคๅฃ
ไบŒ
ๅ…ˆๅ‹
ไธ‰
ๅ‹ๅผ•
ๅ››
ๅ…ˆ่ฒ 
ไบ”
ไปๆป…
ๅ…ญ
ๅคงๅฎ‰
ไธƒ
่ตคๅฃ
ๅ…ซ
ๅ…ˆๅ‹
ไน
ๅ‹ๅผ•
ๅ
ๅ…ˆ่ฒ 
ๅไธ€
ไปๆป…
ๅไบŒ
ๅคงๅฎ‰
ๅไธ‰
ๅ‹ๅผ•
ๅๅ››
ๅ…ˆ่ฒ 
ๅไบ”
ไปๆป…
ๅๅ…ญ
ๅคงๅฎ‰
ๅไธƒ
่ตคๅฃ
ๅๅ…ซ
ๅ…ˆๅ‹
ๅไน
ๅ‹ๅผ•
ไบŒๅ
ๅ…ˆ่ฒ 
ๆ˜ฅๅˆ†ใฎๆ—ฅ
ไบŒๅไธ€
ไปๆป…
ไบŒๅไบŒ
ๅคงๅฎ‰
ไบŒๅไธ‰
่ตคๅฃ
ไบŒๅๅ››
ๅ…ˆๅ‹
ไบŒๅไบ”
ๅ‹ๅผ•
ไบŒๅๅ…ญ
ๅ…ˆ่ฒ 
ไบŒๅไธƒ
ไปๆป…
ไบŒๅๅ…ซ
ๅคงๅฎ‰
ไบŒๅไน
่ตคๅฃ
ไธ‰ๅ
ๅ…ˆๅ‹
ไธ‰ๅไธ€
ๅ‹ๅผ•

(This example is included as japanese.pl in the distribution.)

DEFAULT HTML ELEMENTS AND CSS CLASSES

The elements of the calendar have the following default HTML elements and CSS default style names:

calendar

The default HTML element for calendar (the calendar itself) is <table> with class calendar.

month

The default HTML element for month (a month) is <table> with class cal-month.

week

The default HTML element for week (a week) is <tr> with class cal-week.

day

The default HTML element for day (a day) is <td> with class cal-day as well as class cal-mon, cal-tue, etc.

dow

The default HTML element for dow (the day of the week (Monday, Tuesday, etc.)) is <th> with class cal-dow.

TERMINOLOGY

dow = day of the week (Monday, Tuesday, etc.)
dom = day of the month (1 to 31)
wom = week of the month (corresponds to the rows of the calendar)

DEPENDENCIES

Date::Calc

Date::Calc supplies the date information for the calendar.

HTML::Make

HTML::Make is used to generate the HTML for the calendar.

Table::Readable

Table::Readable is used to read a table of HTML element and CSS class defaults.

SCRIPT

See html-cal in the distribution.

SEE ALSO

Other CPAN modules

Calendar::List
Calendar::Schedule
Calendar::Simple
Date::Calendar

Includes a script cal2html for making HTML.

HTML::Calendar::Monthly

Fork of "HTML::Calendar::Simple". The documentation is largely copy-pasted from that with some alterations.

HTML::Calendar::Simple
HTML::CalendarMonth
HTML::CalendarMonthSimple
SVG::Calendar

Other HTML calendar generators

Python

The defaults of HTML calendar are somewhat based on Python's calendar.HTMLCalendar.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

This package and associated files are copyright (C) 2021 Ben Bullock.

You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.