App-Templer

 view release on metacpan or  search on metacpan

lib/Templer/Site.pm  view on Meta::CPAN


            #
            #  Get the dependencies of the page - add in the page source,
            # and the template path.
            #
            my @deps = ( $self->{ 'layout-path' } . "/" . $template,
                         $page->source(), $page->dependencies() );

            foreach my $d (@deps)
            {
                if ( -M $d < -M $dst )
                {
                    $self->{ 'verbose' } &&
                      print "Triggering rebuild: $d is more recent than $dst\n";
                    $rebuild = 1;
                }
            }
        }

        #
        #  Forced rebuild via the command-line.
        #
        $rebuild = 1 if ( $self->{ 'force' } );

        #
        #  OK skip if we're not rebuilding, otherwise increase the count.
        #
        next unless ($rebuild);
        $rebuilt += 1;


        #
        #  Load the HTML::Template module against the body of the page.
        #
        #  (Includes are relative to the path of the input.)
        #
        my $dirName = $page->source();
        if ( $dirName =~ /^(.*)\/(.*)$/ )
        {
            $dirName = $1;
        }
        my $body = HTML::Template->new( scalarref => \$page->content( \%data ),
                                        die_on_bad_params => 0,
                                        path => [@INCLUDES, $dirName],
                                        search_path_on_include => 1,
                                        global_vars            => 1,
                                        loop_context_vars      => 1,
                                        utf8                   => 1,
                                        filter                 => \@filters,
                                      );


        #
        #  Template-expand the body of the page.
        #
        $body->param( \%data );
        $data{ 'content' } = $body->output();


        #
        # Make the (updated) global and per-page data available
        # to the template object.
        #
        $tmpl->param( \%data );

        #
        # Make sure the output path exists.
        #
        my $path = $dst;
        if ( $path =~ /^(.*)\/(.*)$/ )
        {
            $path = $1;
            File::Path::mkpath( $path, { verbose => 0, mode => oct(755) } )
              if ( !-d $path );
        }

        #
        #  Output the expanded template to the destination file.
        #
        open my $handle, ">:utf8", $dst or die "Failed to write to '$dst' - $!";
        binmode( $handle, ":utf8" );
        print $handle $tmpl->output();
        close $handle;
    }

    #
    #  Cleanup any plugins.
    #
    $PLUGINS->cleanup();

    #
    #  Return count of rebuilt pages.
    #
    return ($rebuilt);
}


=head2 copyAssets

Copy all assets from the input directory to the output directory.

This method will use tar to do so semi-efficiently.

=cut

sub copyAssets
{
    my ($self) = (@_);


    #
    #  If we're running in-place then we don't need to copy assets.
    #
    return if ( $self->{ 'in-place' } );

    #
    #  The assets.
    #
    my @assets = $self->assets( directory => $self->{ 'input' } );

    #

lib/Templer/Site.pm  view on Meta::CPAN


    return unless $self->{ 'sync' };

    #
    # Get list of created and existing files
    #
    my @created = sort @{ $self->{ 'output-files' } };

    my @existing = sort
      map {$_->source();}
      $self->_findFiles( object        => "Templer::Site::Asset",
                         directory     => $self->{ 'output' },
                         hide_dotfiles => 0,
                       );

    #
    # Determine files to remove
    #
    my @files = ();
    my @dirs  = ();
    my %count = ();
    foreach ( @created, @existing )
    {
        $count{ $_ }++;
    }
    foreach ( keys %count )
    {
        push( @files, $_ ) if ( $count{ $_ } == 1 && !-d $_ );
        push( @dirs,  $_ ) if ( $count{ $_ } == 1 && -d $_ );
    }
    @files = sort @files;
    @dirs  = sort @dirs;

    #
    # Removing files
    #
    if (@files)
    {
        print "\nRemoving files: @files\n" if ( $self->{ 'verbose' } );

        unlink @files;
    }

    #
    # Removing directories
    #
    if (@dirs)
    {
        print "\nRemoving directories: @dirs\n" if ( $self->{ 'verbose' } );
        foreach (@dirs)
        {
            rmdir $_;
        }
    }
}

=head2 set

Store/update a key/value pair in our internal store.

This allows the values passed in the constructor to be updated/added to.

=cut

sub set
{
    my ( $self, $key, $values ) = (@_);
    $self->{ $key } = $values;
}


=head2 fields

Get all known key + value pairs from our store.

This is called to get all global variables for template interpolation
as part of the build.  (The global variables and the per-page variables
are each fetched and expanded via plugins prior to getting sent to the
HTML::Template object.).

=cut

sub fields
{
    my ($self) = (@_);

    %$self;
}


=head2 get

Get a single value from our store of variables.

=cut

sub get
{
    my ( $self, $field ) = (@_);
    return ( $self->{ $field } );
}



1;



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