# TextWrap.pl # Movable Type plugin tags for wrapping text # by Kevin Shay # http://www.staggernation.com/mtplugins/ # last modified July 12, 2004 package MT::Plugin::TextWrap; use strict; use vars qw( $VERSION ); $VERSION = '1.1'; use MT; use MT::Template::Context; use MT::Util qw( decode_html ); eval{ require MT::Plugin }; unless ($@) { my $plugin = { name => "TextWrap $VERSION", description => 'Wrap text into lines of a specified length.', doc_link => 'http://www.staggernation.com/mtplugins/TextWrap' }; MT->add_plugin(new MT::Plugin($plugin)); } MT::Template::Context->add_container_tag('TextWrap' => sub{&_hdlr_text_wrap;}); MT::Template::Context->add_container_tag('TextFill' => sub{&_hdlr_text_fill;}); MT::Template::Context->add_global_filter('wrap' => sub{&_hdlr_wrap_filter;}); sub _hdlr_wrap_filter { my ($text, $arg, $ctx) = @_; return $text unless $arg; return wrap_it($text, $arg); } sub _hdlr_text_wrap { return fill_or_wrap(@_, 'wrap'); } sub _hdlr_text_fill { return fill_or_wrap(@_, 'fill'); } sub fill_or_wrap { my ($ctx, $args, $cond, $action) = @_; defined(my $text = $ctx->stash('builder')->build($ctx, $ctx->stash('tokens'), $cond)) || return $ctx->error($ctx->errstr); for my $key (qw(initial_tab subsequent_tab)) { $args->{$key} = decode_html($args->{$key}); } return wrap_it($action, $text, @$args{qw(columns initial_tab subsequent_tab)}); } sub wrap_it { my ($action, $text, $columns, $initial_tab, $subsequent_tab) = @_; require Text::Wrap; my $sub = ($action eq 'fill') ? \&Text::Wrap::fill : \&Text::Wrap::wrap; $Text::Wrap::columns = $columns || 72; $text = &{$sub}($initial_tab || '', $subsequent_tab || '', $text); return $text; } 1;