App-Templer
view release on metacpan or search on metacpan
lib/Templer/Plugin/TimeStamp.pm view on Meta::CPAN
{
my ( $proto, %supplied ) = (@_);
my $class = ref($proto) || $proto;
my $self = {};
bless( $self, $class );
return $self;
}
=head2 expand_variables
This is the method which is called by the L<Templer::Plugin::Factory>
to expand the variables contained in a L<Templer::Site::Page> object.
Variables are written in the file in the form "key: value", and are
internally stored within the Page object as a hash.
This method iterates over each key & value and updates any that
seem to refer to timestamps.
=cut
sub expand_variables
{
my ( $self, $site, $page, $data ) = (@_);
#
# Get the page-variables in the template.
#
my %hash = %$data;
#
# The mtime of the page-source. Cached.
#
my $mtime;
#
# Look for a value of "timestamp" in each key.
#
foreach my $key ( keys %hash )
{
if ( $hash{ $key } =~ /^timestamp\((.*)\)/ )
{
#
# The format to use.
#
my $ts = $1;
#
# Strip leading/trailing whitespace.
#
$ts =~ s/^\s+|\s+$//g;
#
# Get mtime of the source file - if we've not already done so.
#
if ( !$mtime )
{
$mtime = ( stat( $page->source() ) )[9];
}
#
# Store formatted modification time
#
my @mtime = localtime($mtime);
$hash{ $key } = POSIX::strftime $ts, @mtime;
}
}
#
# Return.
#
return ( \%hash );
}
#
# Register the plugin.
#
Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::TimeStamp");
( run in 1.055 second using v1.01-cache-2.11-cpan-39bf76dae61 )