Calendar-Plugin-Renderer

 view release on metacpan or  search on metacpan

lib/Calendar/Plugin/Renderer.pm  view on Meta::CPAN


=back

=head1 SYNOPSIS

    package Cal;

    use Moo;
    use namespace::autoclean;
    with 'Calendar::Plugin::Renderer';

    package main;

    use strict; use warnings;
    use Cal;

    my $cal = Cal->new;
    print $cal->svg_calendar({
        start_index => 5,
        month_name  => 'January',
        days        => 31,
        year        => 2016 });

    print $cal->text_calendar({
        start_index => 5,
        month_name  => 'January',
        days        => 31,
        year        => 2016 });

=head1 METHODS

=head2 text_calendar(\%params)

Returns the color coded calendar as a scalar string.

Expected paramaeters are as below:

    +-------------+-------------------------------------------------------------+
    | Key         | Description                                                 |
    +-------------+-------------------------------------------------------------+
    | start_index | Index of first day of the month. (0-Sun,1-Mon etc)          |
    | month_name  | Calendar month.                                             |
    | days        | Days count in the month.                                    |
    | year        | Calendar year.                                              |
    | day_names   | Ref to a list of day name starting with Sunday. (Optional)  |
    +-------------+-------------------------------------------------------------+

=cut

sub text_calendar {
    my ($self, $params) = @_;

    unless (exists $params->{day_names}) {
        $params->{day_names} = [qw(Sun Mon Tue Wed Thu Fri Sat)];
    }

    my $text = Calendar::Plugin::Renderer::Text->new($params);

    my $line1 = $text->get_dashed_line;
    my $line2 = $text->get_month_header;
    my $line3 = $text->get_blocked_line;
    my $line4 = $text->get_day_header;
    my $empty = $text->get_empty_space;
    my $dates = $text->get_dates;

    my $calendar = join("\n", $line1, $line2, $line3, $line4, $line3, $empty.$dates)."\n";

    return Term::ANSIColor::Markup->colorize($calendar);
}

=head2 svg_calendar(\%params)

Returns the requested calendar month in SVG format.

Expected paramaeters are as below:

    +---------------+-----------------------------------------------------------+
    | Key           | Description                                               |
    +---------------+-----------------------------------------------------------+
    | start_index   | Index of first day of the month. (0-Sun,1-Mon etc)        |
    | month_name    | Calendar month.                                           |
    | days          | Days count in the month.                                  |
    | year          | Calendar year.                                            |
    | adjust_height | Adjust height of the rows in Calendar. (Optional)         |
    +---------------+-----------------------------------------------------------+

=cut

sub svg_calendar {
    my ($self, $params) = @_;

    my $svg = Calendar::Plugin::Renderer::SVG->new($params);
    $svg->process;

    return $svg->as_string;
}

=head2 validate_params($month, $year)

Validate given  C<$month>  and C<$year> as per the Calendar guidelines. C<$month>
can be "name" or "integer". If C<$month> is passed as "name"  then it converts it
it into its "integer" equivalent. Both parameters are optional. In  case they are
missing, it returns the current month and year of the selected calendar.

=cut

sub validate_params {
    my ($self, $month, $year) = @_;

    if (defined $month && defined $year) {
        $self->date->validate_month($month);
        $self->date->validate_year($year);

        if ($month !~ /^\d+$/) {
            $month = $self->date->get_month_number($month);
        }
    }
    else {
        $month = $self->month;
        $year  = $self->year;
    }



( run in 1.277 second using v1.01-cache-2.11-cpan-e1769b4cff6 )