# Glue.pl # Movable Type plugin tags for inserting "glue" between entries in a listing # by Kevin Shay # http://www.staggernation.com/mtplugins/ # last modified July 12, 2004 package MT::Plugin::Glue; use strict; use vars qw( $VERSION ); $VERSION = '1.1'; use MT; use MT::Template::Context; eval{ require MT::Plugin }; unless ($@) { my $plugin = { name => "Glue $VERSION", description => 'Specify "glue" text or HTML that will go between items in a listing (but not after the last item).', doc_link => 'http://www.staggernation.com/mtplugins/Glue' }; MT->add_plugin(new MT::Plugin($plugin)); } MT::Template::Context->add_container_tag('GlueContainer' => sub{&_hdlr_glue_container;}); MT::Template::Context->add_container_tag('Glue' => sub{&_hdlr_glue;}); MT::Template::Context->add_tag('GlueOmitPrevious' => sub{&_hdlr_glue_omit_previous;}); # placeholder strings my $ph = '__MTGLUE__'; my $omit_me = '__OMITME__'; my $omit_ph = '__MTGLUEOMITPREV__'; sub _hdlr_glue_container { # handler for MT container tag that goes around what we want to glue my ($ctx, $args, $cond) = @_; # build our contents, within which MTGlue will insert a placeholder # indicating where the glue should go defined(my $text = $ctx->stash('builder')->build($ctx, $ctx->stash('tokens'), $cond)) || return $ctx->error($ctx->errstr); my $glue = defined($ctx->stash('glue_glue')) ? $ctx->stash('glue_glue') : ''; # find the last occurrence of the placeholder... my $pos = rindex($text, $ph); if ($pos > -1) { # ...and get rid of it $text = substr($text, 0, $pos) . substr($text, $pos + length($ph)); } # was MTGlueOmitPrevious tag used? if ($text =~ /$omit_ph/) { # loop backwards through text, finding each "omit previous" placeholder my $omit_pos = length($text); while (($omit_pos = rindex($text, $omit_ph, $omit_pos)) > -1) { # find the glue placeholder that precedes this omit placeholder $pos = rindex($text, $ph, $omit_pos); if ($pos > -1) { # replace the glue placeholder with an "omit me" placeholder # that's the same length, so we don't screw up our positions $text = substr($text, 0, $pos) . $omit_me . substr($text, $pos + length($ph)); } $omit_pos--; } # now strip out all the "omit me" placeholders $text =~ s/$omit_me//g; # and finally, strip the "omit previous" placeholders $text =~ s/$omit_ph//g; } $text =~ s/$ph/$glue/g; return $text; } sub _hdlr_glue { # handler for MT container tag that contains the glue. # we're just going to stash our contents and return a placeholder my ($ctx, $args, $cond) = @_; defined(my $text = $ctx->stash('builder')->build($ctx, $ctx->stash('tokens'), $cond)) || return $ctx->error($ctx->errstr); $ctx->stash('glue_glue', $text); return $ph; } sub _hdlr_glue_omit_previous { # handler for MT tag to insert a placeholder that tells MTGlueContainer to # omit the glue instance that precedes it my ($ctx, $args) = @_; return $omit_ph; } 1;