# Remove Excessive Links global text filter for Movable Type

# From the book Movable Type Bible Desktop Edition
# by Rogers Cadenhead, copyright 2004

# declare a class name
package MT::Plugin::RemoveExcessiveLinks;

# make classes available to this script
use MT::Template::Context;
use MT::Blog;
use MT::Sanitize;

# register a global filter
MT::Template::Context->add_global_filter(remove_excessive_links => \&remove);

# define the remove_excessive_links filter method
sub remove { 
    my $text = shift;
    my $att_value = shift;
    my $ctx = shift;
    my $blog = $ctx->stash('blog');
    my $good_html = $blog->sanitize_spec || "b,br,p,strong,em,ul,li,blockquote";
    $good_html =~ s/a href,//i;
    my $count = 0;
    $count++ while $text =~ /a href/gi;
    if ($count >= $att_value) {
        $text = MT::Sanitize->sanitize($text, $good_html);
    } 
    return $text;
}
1;