App-Templer
view release on metacpan or search on metacpan
lib/Templer/Plugin/FileGlob.pm view on Meta::CPAN
#
# Data reference - moved here so we can add height/width if the
# glob refers to an image, and if we have Image::Size installed.
#
my %meta = ( file => $file );
#
# Populate filename parts.
#
my ( $basename, $dirname, $extension ) = fileparse($file);
( $meta{ 'dirname' } = $dirname ) =~ s{/$}{};
( $meta{ 'basename' } = $basename ) =~ s{(.*)\.([^.]*)$}{$1};
$meta{ 'extension' } = $2;
#
# If the file is an image AND we have Image::Size
# then populate the height/width too.
#
if ( $file =~ /\.(jpe?g|png|gif)$/i )
{
my $module = "use Image::Size;";
## no critic (Eval)
eval($module);
## use critic
if ( !$@ )
{
if ( -e $file )
{
( $meta{ 'width' }, $meta{ 'height' } ) =
imgsize($file);
}
else
{
print
"WARNING: Attempting to invoke Image::Size on a file that doesn't exist: $file\n";
}
}
}
elsif ( ($suffix) && ( $file =~ /$suffix$/i ) )
{
#
# If the file is a Templer input file
# then populate templer variables
#
my $pageClass = ref $page;
my $globPage = $pageClass->new( file => $file );
while ( my ( $k, $v ) = each %{ $globPage } )
{
$meta{ $k } = $v;
}
}
else
{
#
# If it isn't an image we'll make the content available
#
if ( open( my $handle, "<:utf8", $file ) )
{
binmode( $handle, ":utf8" );
while ( my $line = <$handle> )
{
$meta{ 'contents' } .= $line;
}
close($handle);
}
}
push( @$ref, \%meta );
}
#
# If we found at least one file then we'll update
# the variable value to refer to the populated structure.
#
if ($ref)
{
$hash{ $key } = $ref;
}
else
{
print
"WARNING: pattern '$pattern' matched zero files for page " .
$page->source() . "\n";
delete $hash{ $key };
}
#
# Restore the PWD.
#
chdir($pwd);
}
}
return ( \%hash );
}
#
# Register the plugin.
#
Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::FileGlob");
( run in 1.497 second using v1.01-cache-2.11-cpan-39bf76dae61 )