RecentImages plugin
Excellent.
After some trial and error, I have v1 of my RecentImages plugin for MT available.
Basically, this just loops through your entries until it finds
To install it, simply make a file called recent_images.pl in your mt plugins directory and copy the following code into it.
After some trial and error, I have v1 of my RecentImages plugin for MT available.
Basically, this just loops through your entries until it finds
num_imagesworth of images, and will print the ALT text and the link to the image. Use it like so:
<MTRecentImages num_images="10"> <a href="<$MTRecentImageUrl$>"><$MTRecentImageName$></a><br/> </MTRecentImages>
To install it, simply make a file called recent_images.pl in your mt plugins directory and copy the following code into it.
#!/usr/bin/perl -w
use strict;
use lib '../lib';
use MT::Entry;
use MT::Template::Context;
use vars qw( $error );
use constant NUM_IMAGES => 10;
BEGIN {
eval {
require HTML::TokeParser;
};
$error = $@;
}
MT::Template::Context->add_container_tag(RecentImages => \&images);
MT::Template::Context->add_tag(RecentImageUrl => sub { shift->stash('image_url') });
MT::Template::Context->add_tag(RecentImageName => sub { shift->stash('image_name') });
sub images {
my ($ctx, $args) = @_;
if ($error) {
return $ctx->error('Error with : required modules not installed');
}
my $num_images = $args->{num_images} || NUM_IMAGES;
my $blog_id = $ctx->stash('blog_id');
my $iter = MT::Entry->load_iter(
{ blog_id => $blog_id }, { sort => 'created_on', direction => 'descend' }
);
my @images;
while (my $entry = $iter->()) {
push @images, extract_images($entry->text);
# push @images, { image_url => 'asdf', image_name => 'foobar' };
last if @images >= $num_images;
}
my $res = '';
my $builder = $ctx->stash('builder');
my $tokens = $ctx->stash('tokens');
foreach my $image (splice @images, 0, $num_images) {
$ctx->stash('image_url', $image->{image_url} );
$ctx->stash('image_name', $image->{image_name} );
# defined (my $out = $builder->build($ctx, $tokens))
# or return $ctx->error($ctx->errstr);
my $out = $builder->build($ctx, $tokens);
$res .= $out;
}
return $res;
}
sub extract_images {
my $content = shift;
return unless $content;
my @found_images;
my $stream = HTML::TokeParser->new( \$content );
while (my $token = $stream->get_token ) {
if ( $token->[0] eq 'S' and lc($token->[1]) eq "img" ) {
my $img_attr = $token->[2]; # the attributes for this image tag.
push @found_images, { image_name => $img_attr->{'alt'}, image_url => $img_attr->{'src'} };
}
}
return @found_images;
}