File Coverage

blib/lib/Text/Decorator/Filter.pm
Criterion Covered Total %
statement 28 31 90.3
branch 7 12 58.3
condition 4 9 44.4
subroutine 5 5 100.0
pod 1 1 100.0
total 45 58 77.5


line stmt bran cond sub pod time code
1             package Text::Decorator::Filter;
2              
3 5     5   29 use strict;
  5         11  
  5         146  
4 5     5   27 use warnings;
  5         39  
  5         133  
5              
6 5     5   27 use Carp "confess";
  5         9  
  5         2392  
7              
8             =head1 NAME
9              
10             Text::Decorator::Filter - Base class for text decorator filters
11              
12             =head1 DESCRIPTION
13              
14             This is the base class that all filters should inherit from.
15              
16             =head1 METHODS
17              
18             =head2 filter
19              
20             This base class provides the all-important C method that you
21             really don't want to implement yourself. Instead, you should provide
22             one of these methods:
23              
24             =head2 filter_text
25              
26             This should simply modify C<$_>. It's called once for each
27             representation, with the representation as the first parameter to the
28             method.
29              
30             =head2 filter_node
31              
32             This gets called as
33              
34             $class->filter_node($args, $node)
35              
36             for every textual node, and is expected to return one or more modified
37             node objects.
38              
39             There's also C which you may want to provide, which does
40             the same but for C objects.
41              
42             =head2 filter_anynode
43              
44             This is the same, but gets called for both Group and Node objects.
45              
46             =cut
47              
48             sub filter {
49 6     6 1 14 my ($class, $args, @nodelist) = @_;
50 6         9 my @newnodes;
51 6         36 for my $node (@nodelist) {
52 6 50       24 confess "Dead node in nodelist!" unless $node;
53 6 50       86 if ($node->isa("Text::Decorator::Group")) {
54 0         0 $node->{nodes} = [ $class->filter($args, $node->nodes) ];
55             }
56 6         36 push @newnodes, $class->_do_filter($args, $node);
57             }
58 6         24 return @newnodes;
59             }
60              
61             sub _do_filter {
62 6     6   11 my ($class, $args, $node) = @_;
63              
64             # We're prepared to do it all!
65 6 50       73 return $class->filter_anynode($args, $node)
66             if $class->can("filter_anynode");
67              
68             # Taking short-cuts
69 6 100 66     61 if ($class->can("filter_text") and $node->isa("Text::Decorator::Node")) {
70 1         3 for my $format (keys %{ $node->{representations} }) {
  1         5  
71 1         4 local $_ = $node->{representations}->{$format};
72 1         5 $class->filter_text($format);
73 1         4 $node->{representations}->{$format} = $_;
74             }
75 1         5 return $node;
76             }
77              
78 5 50 33     46 if ($class->can("filter_group") and $node->isa("Text::Decorator::Group")) {
79 0         0 return $class->filter_group($args, $node);
80             }
81              
82 5 50 33     54 if ($class->can("filter_node") and $node->isa("Text::Decorator::Node")) {
83 5         21 return $class->filter_node($args, $node);
84             }
85              
86 0           return $node;
87             }
88              
89             1;