File Coverage

blib/lib/Template/Plugin/Autoformat.pm
Criterion Covered Total %
statement 37 37 100.0
branch 12 12 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 59 59 100.0


line stmt bran cond sub pod time code
1             #============================================================= -*-Perl-*-
2             #
3             # Template::Plugin::Autoformat
4             #
5             # DESCRIPTION
6             # Plugin interface to Damian Conway's Text::Autoformat module.
7             #
8             # AUTHORS
9             # Robert McArthur
10             # - original plugin code
11             #
12             # Andy Wardley
13             # - added FILTER registration, support for forms and some additional
14             # documentation
15             #
16             # COPYRIGHT
17             # Copyright (C) 2000-2008 Robert McArthur, Andy Wardley.
18             # All Rights Reserved.
19             #
20             # This module is free software; you can redistribute it and/or
21             # modify it under the same terms as Perl itself.
22             #
23             #============================================================================
24              
25             package Template::Plugin::Autoformat;
26              
27 2     2   844372 use strict;
  2         11  
  2         115  
28 2     2   14 use warnings;
  2         5  
  2         180  
29 2     2   15 use base 'Template::Plugin';
  2         5  
  2         1462  
30 2     2   2775 use Text::Autoformat;
  2         59282  
  2         1191  
31              
32             our $VERSION = '2.79';
33              
34             sub new {
35 23     23 1 177164 my ( $class, $context, $options ) = @_;
36 23         64 my $filter_factory;
37             my $plugin;
38              
39 23 100       111 if ($options) {
40              
41             # create a closure to generate filters with additional options
42             $filter_factory = sub {
43 4     4   300 my $context = shift;
44 4 100       22 my $filtopt = ref $_[-1] eq 'HASH' ? pop : {};
45 4         42 @$filtopt{ keys %$options } = values %$options;
46             return sub {
47 4         143 _tt_autoformat( @_, $filtopt );
48 4         32 };
49 12         92 };
50              
51             # and a closure to represent the plugin
52             $plugin = sub {
53 16 100   16   6529 my $plugopt = ref $_[-1] eq 'HASH' ? pop : {};
54 16         90 @$plugopt{ keys %$options } = values %$options;
55 16         65 _tt_autoformat( @_, $plugopt );
56 12         74 };
57             }
58             else {
59             # simple filter factory closure (no legacy options from constructor)
60             $filter_factory = sub {
61 5     5   348 my $context = shift;
62 5 100       24 my $filtopt = ref $_[-1] eq 'HASH' ? pop : {};
63             return sub {
64 7         208 _tt_autoformat( @_, $filtopt );
65 5         35 };
66 11         92 };
67              
68             # plugin without options can be static
69 11         32 $plugin = \&_tt_autoformat;
70             }
71              
72             # now define the filter and return the plugin
73 23         171 $context->define_filter( 'Autoformat', [ $filter_factory => 1 ] );
74 23         949 return $plugin;
75             }
76              
77             sub _tt_autoformat {
78 33 100   33   304 my $options = ref $_[-1] eq 'HASH' ? pop : {};
79 33         91 my $form = $options->{form};
80 33 100       199 my $out
81             = $form
82             ? Text::Autoformat::form( $options, $form, @_ )
83             : Text::Autoformat::autoformat( join( '', @_ ), $options );
84 33         100666 return $out;
85             }
86              
87             1;
88              
89             __END__