File Coverage

blib/lib/MIME/Visitor.pm
Criterion Covered Total %
statement 30 32 93.7
branch 4 4 100.0
condition n/a
subroutine 12 13 92.3
pod 5 5 100.0
total 51 54 94.4


line stmt bran cond sub pod time code
1 2     2   72949 use strict;
  2         28  
  2         57  
2 2     2   10 use warnings;
  2         4  
  2         111  
3             package MIME::Visitor 0.007;
4             # ABSTRACT: walk through MIME parts and do stuff (like rewrite)
5              
6 2     2   12 use Encode;
  2         5  
  2         155  
7 2     2   801 use MIME::BodyMunger;
  2         6  
  2         849  
8              
9             #pod =head1 SYNOPSIS
10             #pod
11             #pod # This will reverse all lines in each text part, taking care of all encoding
12             #pod # for you.
13             #pod
14             #pod MIME::Visitor->rewrite_all_lines(
15             #pod $mime_entity,
16             #pod sub { chomp; $_ = reverse . "\n"; },
17             #pod );
18             #pod
19             #pod =head1 DESCRIPTION
20             #pod
21             #pod MIME::Visitor provides a simple way to walk through the parts of a MIME
22             #pod message, taking action on each one. In general, this is not a very complicated
23             #pod thing to do, but having it in one place is convenient.
24             #pod
25             #pod The most powerful feature of MIME::Visitor, though, is its methods for
26             #pod rewriting text parts. These methods take care of character sets for you so
27             #pod that you can treat everything like text instead of worrying about content
28             #pod transfer encoding or character set encoding.
29             #pod
30             #pod At present, only MIME::Entity messages can be handled. Other types will be
31             #pod added in the future.
32             #pod
33             #pod =method walk_parts
34             #pod
35             #pod MIME::Visitor->walk_parts($root, sub { ... });
36             #pod
37             #pod This method calls the given code on every part of the given root message,
38             #pod including the root itself.
39             #pod
40             #pod =cut
41              
42             sub walk_parts {
43 14     14 1 30 my ($self, $root, $code) = @_;
44              
45 14         36 $code->($root);
46 14         2403 for my $part ($root->parts) {
47 8         102 $self->walk_parts($part, $code);
48             }
49             }
50              
51             #pod =method walk_leaves
52             #pod
53             #pod MIME::Visitor->walk_leaves($root, sub { ... });
54             #pod
55             #pod This method calls the given code on every leaf part of the given root message.
56             #pod It descends into multipart parts of the message without calling the callback on
57             #pod them.
58             #pod
59             #pod =cut
60              
61             sub walk_leaves {
62 6     6 1 16 my ($self, $root, $code) = @_;
63              
64             $self->walk_parts(
65             $root,
66             sub {
67 14 100   14   56 return if $_[0]->is_multipart;
68 10         1645 $code->($_[0]);
69             },
70 6         33 );
71             }
72              
73             #pod =method walk_text_leaves
74             #pod
75             #pod MIME::Visitor->walk_text_leaves($root, sub { ... });
76             #pod
77             #pod This method behaves like C, but only calls the callback on parts
78             #pod with a content type of text/plain or text/html.
79             #pod
80             #pod =cut
81              
82             sub walk_text_leaves {
83 6     6 1 41 my ($self, $root, $code) = @_;
84             $self->walk_leaves($root, sub {
85 10 100   10   29 return unless $_[0]->effective_type =~ qr{\Atext/(?:html|plain)(?:$|;)}i;
86 8         1148 $code->($_[0]);
87 6         30 });
88             }
89              
90             #pod =method rewrite_parts
91             #pod
92             #pod MIME::Visitor->rewrite_parts($root, sub { ... });
93             #pod
94             #pod This method walks the text leaves of the MIME message, rewriting the content of
95             #pod the parts. For each text leaf, the callback is invoked like this:
96             #pod
97             #pod $code->(\$content, $part);
98             #pod
99             #pod For more information, see L.
100             #pod
101             #pod =cut
102              
103             sub rewrite_parts {
104 1     1 1 15332 my ($self, $root, $code) = @_;
105              
106             $self->walk_text_leaves($root, sub {
107 0     0   0 my ($part) = @_;
108 0         0 MIME::BodyMunger->rewrite_content($part, $code);
109 1         11 });
110             }
111              
112             #pod =method rewrite_all_lines
113             #pod
114             #pod MIME::Visitor->rewrite_all_lines($root, sub { ... });
115             #pod
116             #pod This method behaves like C, but the callback is called for each
117             #pod line of each relevant part, rather than for the part's body as a whole.
118             #pod
119             #pod =cut
120              
121             sub rewrite_all_lines {
122 5     5 1 55548 my ($self, $root, $code) = @_;
123              
124             $self->walk_text_leaves($root, sub {
125 8     8   18 my ($part) = @_;
126 8         49 MIME::BodyMunger->rewrite_lines($part, $code);
127 5         36 });
128             }
129              
130             #pod =head1 THANKS
131             #pod
132             #pod Thanks to Pobox.com and Listbox.com, who sponsored the development of this
133             #pod module.
134             #pod
135             #pod =cut
136              
137             1;
138              
139             __END__