App-Templer

 view release on metacpan or  search on metacpan

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


=cut

=head1 METHODS

=cut


use strict;
use warnings;


package Templer::Site::Page;



=head2 new

The constructor.

The single appropriate argument is the hash-key "file", pointing to the
page-file on-disk.

=cut

sub new
{
    my ( $proto, %supplied ) = (@_);
    my $class = ref($proto) || $proto;

    my $self = {};

    #
    #  Allow user supplied values to override our defaults
    #
    foreach my $key ( keys %supplied )
    {
        $self->{ lc $key } = $supplied{ $key };
    }

    bless( $self, $class );
    $self->_parse_page( $self->{ 'file' } ) if ( $self->{ 'file' } );
    return $self;
}


=head2 _parse_page

Read the file, and parse the header/content.

This is an internal method.

=cut

sub _parse_page
{
    my ( $self, $filename ) = (@_);

    open( my $handle, "<:utf8", $filename ) or
      die "Failed to read '$filename' - $!";
    binmode( $handle, ":utf8" );

    my $header = 1;

    while ( my $line = <$handle> )
    {

        # strip trailing newline.
        $line =~ s/[\r\n]*//g;

        if ($header)
        {
            if ( $line =~ /^([^:]+):(.*)$/ )
            {
                my $key = $1;
                my $val = $2;
                $key = lc($key);
                $key =~ s/^\s+|\s+$//g;
                $val =~ s/^\s+|\s+$//g;

                $self->{ $key } = $val;
                print "Templer::Site::Page set: $key => $val\n"
                  if ( $self->{ 'debug' } );
            }
            if ( $line =~ /^----[\r\n]*$/ )
            {
                $header = undef;
            }
        }
        else
        {
            $self->{ 'content' } .= $line . "\n";
        }
    }

    #
    # If we're still in the header at the end of the file
    # then something has gone wrong.
    #
    if ($header)
    {
        print "WARNING: No header found in $filename\n";
    }

    close($handle);
}




=head2 content

Return the body of the page.

Here we perform the textile/markdown expansion if possible via the use
plugins loaded by L<Templer::Plugin::Factory>.

=cut

sub content
{



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