App-Chronicle

 view release on metacpan or  search on metacpan

lib/Chronicle/Plugin/Generate/Pages.pm  view on Meta::CPAN


sub on_generate
{
    my ( $self, %args ) = (@_);

    my $dbh    = $args{ 'dbh' };
    my $config = $args{ 'config' };


    my $all = $dbh->prepare("SELECT id FROM blog ORDER BY date ASC") or
      die "Failed to find posts";

    my $now = time;

    my @all = ();

    $all->execute() or die "Failed to execute:" . $dbh->errstr();
    my $id;
    $all->bind_columns( undef, \$id );

    #
    # Build up the list of all the post IDs
    #
    # We could build these on-demand, but instead maintain a list
    # such that we can add next_link, next_title, etc, and allow
    # paging through blog-entries.
    #
    while ( $all->fetch() )
    {
        push( @all, $id );
    }


    #
    #  Now we have all the posts we iterate over them in-order.
    #
    for my $index ( 0 .. $#all )
    {
        my $id = $all[$index];

        #
        #  The previous blog entry and next blog entry, sequentially
        #
        my $prev_id = undef;
        my $next_id = undef;

        $prev_id = $all[$index - 1] if ( $index > 0 );
        $next_id = $all[$index + 1] if ( $index < $#all );

        #
        #  Read the details of the main entry.
        #
        my $entry = Chronicle::getBlog( dbh    => $dbh,
                                        id     => $id,
                                        config => $config
                                      );

        #
        #  The page we'll output might be down-cased.
        #
        my $page = $entry->{ 'link' }->unescaped;
        if ( $config->{ 'lower-case' } )
        {
            $page = lc($page);
            $entry->{ 'link' } = lc( $entry->{ 'link' } );
        }

        #
        #  The complete path.
        #
        my $out = $config->{ 'output' } . "/" . $page;

        #
        #  We skip posts that are already present:
        #
        #  * Unless they were posted recently and comments enabled.
        #
        # or
        #
        #  * The --force flag was used.
        #

        my $skip = 0;

        #
        #  So skip it if it exists.
        #
        $skip = 1 if ( -e $out );

        #
        # Unless --force overrides that.
        #
        $skip = 0 if ( $config->{ 'force' } );

        #
        # Finally if comments were enabled and this is recent then
        # we'll also force it to be generated
        #
        $skip = 0
          if ( ( $config->{ 'comments' } ) &&
               ( ( $now - $entry->{ 'posted' } ) <
                 ( 60 * 60 * 24 * $config->{ 'comment-days' } ) ) );


        #
        #  Loop again if we're skipping this post.
        #
        next if ($skip);


        $config->{ 'verbose' } &&
          print "Creating : $out\n";

        my $c = Chronicle::load_template( $entry->{ 'template' } );
        return unless ($c);

        $c->param( top => $config->{ 'top' } );
        $c->param($entry);

        #
        #  If we have a prev/next entry then add their details too.



( run in 0.956 second using v1.01-cache-2.11-cpan-39bf76dae61 )