Bryar

 view release on metacpan or  search on metacpan

lib/Bryar.pm  view on Meta::CPAN

	$self->_doit;
}

sub _doit {
    my $self = shift;
    my %args = $self->config->frontend->parse_args($self->config, @_);
    $self->{arguments} = \%args;

    # The HTTP headers must be reset to allow re-using the Bryar object
    # (e.g. when using FastCGI).
    $self->{http_headers} = { };

    my $cache = $self->config->cache;
    my ($cache_key, @output);

    # try to fetch a complete formatted answer from the cache, if one exists
    if ($cache) {
        $cache_key = $self->cache_key;
        my $object = $cache->get($cache_key);
        @output = @$object if $object;
    }

    # if there is no cached answer we need to collect the data and generate one
    if (not @output) {
        my @documents = $self->config->collector->collect($self->config, %args);

        my $last_modified = 0;
        if (@documents) {
            $last_modified = List::Util::max(map { $_->{epoch} } @documents);
        } else {
            $self->{http_headers}->{Status} = '404 Not Found';
        }

        $args{format} ||= 'html';

        @output = (
            $self->config->renderer->generate(
                $self->{arguments}{format},
                $self,
                @documents
            ),
            $last_modified,
            $self->{http_headers}
        );

        $cache->set($cache_key, \@output) if $cache;
    }

    $self->config->frontend->output(@output);
}

# create the key used to index the cache
sub cache_key {
    my $self = shift;

    return 'Bryar: ' . join(' | ', map {
        $_ . ' => ' . $self->{arguments}->{$_}
    } sort keys %{$self->{arguments}});
}

=head2 posts_calendar

TODO:  Move this out to something that is more flexible.

Return a data structure containing the days and weeks of a given month and
year with blog posts attached. See the C<calendar> template for an example.

=cut

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

    my $today = DateTime->today( time_zone => $self->config->{time_zone} );

	$month ||= $today->month(); 
	$year  ||= $today->year(); 

	my $this_month = DateTime->new( month => $month, year => $year,  time_zone => $self->config->{time_zone} );

	my @documents = $self->config->collector->collect(
		$self->config,
		since => $this_month->epoch()
	);

	# make an hash with keys the days with a post
	my %posts = map { DateTime->from_epoch( epoch => $_->{epoch},  time_zone => $self->config->{time_zone} )->day() => $_->{id} } @documents;

	my @m = calendar($month, $year);
	my @month;
	foreach my $week (@m) {
		my @weekdays;
		foreach my $day (@$week) {
			my $d = { day => $day };
			if ($day and exists $posts{$day}) {
				$d->{idlink} = $posts{$day};
				$d->{link} = "$year/" . $this_month->month_abbr() . "/$day";
			}
			push(@weekdays, $d);
		}

		# mark the first day of the week, if it exists
		$weekdays[0]{sunday} = 1 if defined $weekdays[0]{day};

		push(@month, \@weekdays);
	}

	return { year => $year, month => $month, monthname => $this_month->month_name(), calendar => \@month };
}

=head2 config

Returns the L<Bryar::Config|Bryar::Config> object for this blog. This is
useful as the blog object is passed into the templates by default.

=cut

sub config { return $_[0]->{config} }
sub arguments {return $_[0]->{arguments} }

=head1 LICENSE

This module is free software, and may be distributed under the same
terms as Perl itself.

=head1 THANKS

Steve Peters provided Atom support.
Marco d'Itri contributed the calendar, HTTP validators, caching, FastCGI,
sitemaps, non-ASCII charsets, bug fixes and optimizations.

=head1 AUTHOR

Copyright (C) 2003, Simon Cozens C<simon@cpan.org>

some parts Copyright 2007 David Cantrell C<david@cantrell.org.uk>

some parts Copyright 2009 Marco d'Itri C<md@linux.it>

=cut

1;



( run in 1.964 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )