File Coverage

blib/lib/Text/Decorator/Filter/TTBridge.pm
Criterion Covered Total %
statement 20 20 100.0
branch 3 4 75.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 29 30 96.6


line stmt bran cond sub pod time code
1             package Text::Decorator::Filter::TTBridge;
2              
3             # Avoid rewriting big wheels, use TT's filters
4 2     2   11 use strict;
  2         5  
  2         66  
5              
6 2     2   10 use base 'Text::Decorator::Filter';
  2         3  
  2         1233  
7              
8 2     2   1278 use Text::Decorator::Group;
  2         523  
  2         30  
9 2     2   1924 use Template::Filters;
  2         20383  
  2         31  
10              
11             =head1 NAME
12              
13             Text::Decorator::Filter::TTBridge - Use Template Toolkit filters
14              
15             =head1 SYNOPSIS
16              
17             $decorator->add_filter(TTBridge => all => "trim");
18             $decorator->add_filter(TTBridge => all => "indent" => 4);
19             $decorator->add_filter(TTBridge => html => "uri");
20              
21             =head1 DESCRIPTION
22              
23             =head2 filter_node
24              
25             This bridge allows Text::Decorator to make use of Template Toolkit's
26             standard filters.
27              
28             First you need to specify which representations this filter applies to;
29             "all" will convert all representations. Next you give the name of the TT
30             filter, and following that, any arguments to pass to the filter.
31              
32             =cut
33              
34             sub filter_node {
35 3     3 1 7 my ($class, $args, $node) = @_;
36 3         6 my ($where, $filter, @args) = @$args;
37              
38 3         27 ($filter) =
39             Template::Filters->new(TOLERANT => 1)->fetch($filter, \@args, undef);
40 3 50       195 return $node unless ref $filter eq "CODE";
41              
42 3 100       15 for my $format (
  1         5  
43             $where eq "all"
44             ? keys %{ $node->{representations} }
45             : $where
46             ) {
47 3         18 $node->{representations}{$format} = $filter->($node->format_as($format));
48             }
49 3         54 return $node;
50             }
51              
52             1;