App-Templer
view release on metacpan or search on metacpan
lib/Templer/Plugin/FileGlob.pm view on Meta::CPAN
# Make sure we're relative to the directory containing
# the actual input-page.
#
# This way globs will match what the author expected.
#
my $dirName = $page->source();
if ( $dirName =~ /^(.*)\/(.*)$/ )
{
$dirName = $1;
}
my $pwd = Cwd::cwd();
chdir( $dirName . "/" );
#
# The value we'll set the variable to.
#
my $ref;
#
# The suffix of files that are Templer input-files.
#
my $suffix = $site->{ suffix };
#
# Run the glob.
#
foreach my $file ( glob($pattern) )
{
#
# The page dependency also includes the matching filename now.
#
if ( $file =~ m{^/} )
{
$page->add_dependency($file);
}
else
{
$page->add_dependency( $dirName . "/" . $file );
}
#
# 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;
( run in 0.631 second using v1.01-cache-2.11-cpan-df04353d9ac )