App-Templer

 view release on metacpan or  search on metacpan

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


    File::Path::mkpath( $output, { verbose => 0, mode => oct(755) } )
      if ( !-d $output && ( !$inplace ) );

    #
    # We will store the list of all destination files
    #
    $self->set( "output-files", [] );
}


=head2 pages

A site comprises of a collection of pages and a collection of static resources
which aren't touched/modified - these are "assets".

Return a C<Templer::Site::Page> object for each page we've found.

B<NOTE> We don't process pages with a "." prefix, i.e. dotfiles.

=cut

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

    my $dir =
      $args{ 'directory' } || $self->{ 'directory' } || $self->{ 'input' };
    my $suffix = $args{ 'suffix' } || $self->{ 'suffix' };

    return (
             $self->_findFiles( must_match    => $suffix . "\$",
                                object        => "Templer::Site::Page",
                                directory     => $dir,
                                hide_dotfiles => 1,
                              ) );
}


=head2 assets

A site comprises of a collection of pages and a collection of static resources
which aren't touched/modified - these are "assets".

Return a C<Templer::Site::Asset> object for each asset we find.

B<NOTE> We include files which have a "." prefix here - to correctly
copy files such as ".htpasswd", ".htaccess", etc.

=cut

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

    my $dir =
      $args{ 'directory' } || $self->{ 'directory' } || $self->{ 'input' };
    my $suffix = $args{ 'suffix' } || $self->{ 'suffix' };

    return (
             $self->_findFiles( must_not_match => $suffix . "\$",
                                object         => "Templer::Site::Asset",
                                directory      => $dir,
                                hide_dotfiles  => 0,
                              ) );

}


=head2 _findFiles

Internal method to find files beneath the given directory and return a new object
for each one.

We assume that the object constructor receives a hash as its sole
argument with the key "file" containing the file path.

=cut

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

    #
    # Remove the trailing "/" on the end of the directory to search.
    #
    $args{ 'directory' } =~ s/\/$//g;

    #
    # Should we hide dotfiles?
    #
    my $dotfiles = $args{ 'hide_dotfiles' };


    #
    #  Files we've found.  Ignoring the suffix just now.
    #
    my %files;

    File::Find::find( {
           wanted => sub {
               my $name = $File::Find::name;
               $files{ $name } += 1 unless ( $dotfiles && ( $name =~ /\/\./ ) );
           },
           follow   => 0,
           no_chdir => 1
        },
        $args{ 'directory' } );

    #
    # Remove the input
    #
    delete $files{ $args{ 'directory' } };

    #
    #  OK now we need to find the matches.
    #
    my @matches;

    #
    #  The class-object we're going to construct.
    #
    my $class = $args{ 'object' };

    if ( $args{ 'must_match' } )
    {
        foreach my $file ( sort keys %files )
        {
            next if ( -d $file );
            next unless ( $file =~ /$args{'must_match'}/ );
            push( @matches, $class->new( file => $file ) );
        }
    }
    elsif ( $args{ 'must_not_match' } )
    {
        foreach my $file ( sort keys %files )
        {
            next if ( $file =~ /$args{'must_not_match'}/ );
            push( @matches, $class->new( file => $file ) );
        }
    }
    else
    {
        @matches = map {$class->new( file => $_ )} keys %files;
    }

    #
    # Every pages depend on the global configuration file
    #
    if ( $args{ 'object' } eq "Templer::Site::Page" )
    {
        foreach my $page (@matches)
        {
            $page->add_dependency( $self->{ 'config' } );
        }
    }

    @matches;
}



=head2 build

Build the site.

This is the method which does all the page-expansion, site-generation, etc.

The return value is the count of pages built.

=cut

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

    #
    #  If we have a plugin directory then load the plugins beneath it.
    #
    #  NOTE:  The bundled/built-in plugins will always be available.
    #
    my $PLUGINS = Templer::Plugin::Factory->new();
    if ( -d $self->{ 'plugin-path' } )
    {
        print "Loading plugins from :  $self->{ 'plugin-path' }\n"
          if ( $self->{ 'verbose' } );

        $PLUGINS->load_plugins( $self->{ 'plugin-path' } );
    }

    #
    #  Initialize all plugins.
    #
    $PLUGINS->init($self);

    #
    #  Setup an array of include-paths.
    #



( run in 2.585 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )