1#============================================================= -*-Perl-*-
2#
3# Template::Plugin::Wrap
4#
5# DESCRIPTION
6#   Plugin for wrapping text via the Text::Wrap module.
7#
8# AUTHOR
9#   Andy Wardley   <abw@wardley.org>
10#
11# COPYRIGHT
12#   Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.
13#
14#   This module is free software; you can redistribute it and/or
15#   modify it under the same terms as Perl itself.
16#
17#============================================================================
18
19package Template::Plugin::Wrap;
20
21use strict;
22use warnings;
23use base 'Template::Plugin';
24use Text::Wrap;
25
26our $VERSION = 2.68;
27
28sub new {
29    my ($class, $context, $format) = @_;;
30    $context->define_filter('wrap', [ \&wrap_filter_factory => 1 ]);
31    return \&tt_wrap;
32}
33
34sub tt_wrap {
35    my $text  = shift;
36    my $width = shift || 72;
37    my $itab  = shift;
38    my $ntab  = shift;
39    $itab = '' unless defined $itab;
40    $ntab = '' unless defined $ntab;
41    $Text::Wrap::columns = $width;
42    Text::Wrap::wrap($itab, $ntab, $text);
43}
44
45sub wrap_filter_factory {
46    my ($context, @args) = @_;
47    return sub {
48        my $text = shift;
49        tt_wrap($text, @args);
50    }
51}
52
53
541;
55
56__END__
57
58=head1 NAME
59
60Template::Plugin::Wrap - Plugin interface to Text::Wrap
61
62=head1 SYNOPSIS
63
64    [% USE wrap %]
65
66    # call wrap subroutine
67    [% wrap(mytext, width, initial_tab,  subsequent_tab) %]
68
69    # or use wrap FILTER
70    [% mytext FILTER wrap(width, initital_tab, subsequent_tab) %]
71
72=head1 DESCRIPTION
73
74This plugin provides an interface to the L<Text::Wrap> module which
75provides simple paragraph formatting.
76
77It defines a C<wrap> subroutine which can be called, passing the input
78text and further optional parameters to specify the page width (default:
7972), and tab characters for the first and subsequent lines (no defaults).
80
81    [% USE wrap %]
82
83    [% text = BLOCK %]
84    First, attach the transmutex multiplier to the cross-wired
85    quantum homogeniser.
86    [% END %]
87
88    [% wrap(text, 40, '* ', '  ') %]
89
90Output:
91
92    * First, attach the transmutex
93      multiplier to the cross-wired quantum
94      homogeniser.
95
96It also registers a C<wrap> filter which accepts the same three optional
97arguments but takes the input text directly via the filter input.
98
99Example 1:
100
101    [% FILTER bullet = wrap(40, '* ', '  ') -%]
102    First, attach the transmutex multiplier to the cross-wired quantum
103    homogeniser.
104    [%- END %]
105
106Output:
107
108    * First, attach the transmutex
109      multiplier to the cross-wired quantum
110      homogeniser.
111
112Example 2:
113
114    [% FILTER bullet -%]
115    Then remodulate the shield to match the harmonic frequency, taking
116    care to correct the phase difference.
117    [% END %]
118
119Output:
120
121    * Then remodulate the shield to match
122      the harmonic frequency, taking
123      care to correct the phase difference.
124
125=head1 AUTHOR
126
127Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
128
129The L<Text::Wrap> module was written by David Muir Sharnoff
130with help from Tim Pierce and many others.
131
132=head1 COPYRIGHT
133
134Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.
135
136This module is free software; you can redistribute it and/or
137modify it under the same terms as Perl itself.
138
139=head1 SEE ALSO
140
141L<Template::Plugin>, L<Text::Wrap>
142
143