File Coverage

blib/lib/SVN/Notify/Alternative.pm
Criterion Covered Total %
statement 23 23 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 3 3 100.0
pod 1 1 100.0
total 36 36 100.0


line stmt bran cond sub pod time code
1             package SVN::Notify::Alternative;
2              
3 32     32   180288 use strict;
  32         64  
  32         608  
4 32     32   16384 use SVN::Notify ();
  32         64  
  32         7072  
5              
6             $SVN::Notify::Alternative::VERSION = '1.0';
7             @SVN::Notify::Alternative::ISA = qw(SVN::Notify);
8              
9             __PACKAGE__->register_attributes(
10             alternatives => 'alternative|alt=s@',
11             );
12              
13             =head1 Name
14              
15             SVN::Notify::Alternative - MIME multipart/alternative notification
16              
17             =head1 Synopsis
18              
19             Use F in F:
20              
21             svnnotify --repos-path "$1" --revision "$2" \
22             --to developers@example.com --handler Alternative [options]
23              
24             For example:
25              
26             svnnotify --repos-path "$1" --revision "$2" \
27             --to developers@example.com --handler Alternative \
28             --alternative HTML::ColorDiff
29              
30             Use the class in a custom script:
31              
32             use SVN::Notify::Alternative;
33              
34             my $notifier = SVN::Notify::Alternative->new(%params);
35             $notifier->prepare;
36             $notifier->execute;
37              
38             =head1 Description
39              
40             This subclass of L sends MIME multipart/alternative
41             email messages for Subversion activity. The messages contain both the standard
42             SVN::Notify plain text change notification and one or more alternative formats
43             of the message. The default alternative format is L.
44              
45             Note that this means that many or all of the processing of a subversion commit
46             will be executed multiple times, once for the plain text version and then
47             again for each alternative version. This will therefore increase resource
48             usage on your Subversion server (mainly processor time, but also possibly
49             memory).
50              
51             It also means that the size of the outgoing message will increase for each
52             alternative. If you're using C<--with-diff>, then those messages could be very
53             large indeed for large commits. If, however, you use C<--attach-diff>, the
54             diff will only be attached to the last alternative.
55              
56             =head1 Usage
57              
58             To use SVN::Notify::Alternative, simply follow the
59             L in SVN::Notify, but when using F,
60             use the C<--alternative> option to add one or more alternative formats.
61              
62             =cut
63              
64             ##############################################################################
65              
66             =head1 Class Interface
67              
68             =head2 Constructor
69              
70             =head3 new
71              
72             my $notifier = SVN::Notify::Alternative->new(%params);
73              
74             Constructs and returns a new SVN::Notify object. All parameters supported by
75             SVN::Notity are supported here, as are the options of all specified
76             alternative formats, but SVN::Notify::Alternative supports an additional
77             parameter:
78              
79             =over
80              
81             =item alternatives
82              
83             svnnotify --alternative HTML
84             svnnotify --alt HTML --alt HTML::ColorDiff
85              
86             An array reference that specifies the SVN::Notify handlers (subclasses) to be
87             used for formatting the alternative parts of the notification message. The
88             command-line option may be called as either C<--alternative> or C<--alt>, and
89             the value is the same as that of the SVN::Notify C<--handler> parameter, i.e.
90             the module name without the "SVN::Notify::" prefix. Specify the option
91             multiple times to specify multiple alternative handlers. Defaults to
92             C<['HTML']> if not specified.
93              
94             =back
95              
96             =cut
97              
98             ##############################################################################
99              
100             =head1 Instance Interface
101              
102             =head2 Instance Methods
103              
104             =head3 output
105              
106             $notifier->output($file_handle);
107              
108             Overrides the C method of SVN::Notify to replace the standard
109             message output with a MIME C skeleton. It then creates
110             new instances of the standard SVN::Notify plain text formatter and each of the
111             configured alternative formatters, and uses those instances to fill in the
112             alternative parts of the message. If C is true, it will be used
113             only in the last alternative to be output, which should also be the richest
114             format.
115              
116             =cut
117              
118             sub output {
119 109     109 1 964 my ($self, $out) = @_;
120              
121             # Output the headers. Leave out the attachment header.
122 109         3015 my $attach = $self->attach_diff;
123 109 100       747 if ($attach) {
124 10         400 $self->attach_diff(undef);
125 10         740 $self->with_diff(undef);
126             }
127 109         2103 $self->output_headers($out);
128              
129             # Output the multipart/alternative header.
130 109         385 my $bound = join '', ('a'..'z', 'A'..'Z', 0..9)[ map { rand 62 } 0..10];
  1199         3497  
131 109         779 print $out qq{Content-Type: multipart/alternative; boundary="$bound"\n},
132             "Content-Transfer-Encoding: 8bit\n\n";
133              
134             # Determine all of the handlers to use.
135 109         319 my $alts = $self->{alternatives};
136 109 100       692 $alts = ['HTML'] unless $alts;
137 109         416 unshift @$alts, ''; # Plain text first.
138              
139             # Now output each of the alternatives.
140 109         666 while (@$alts) {
141 228         1478 print $out "--$bound\n";
142             # Attach diff only to last version.
143 228 100 100     11313 SVN::Notify->new(
144             %$self,
145             handler => shift @$alts,
146             ($attach && !@$alts ? ( attach_diff => 1) : ()),
147             )->output($out, 1);
148             }
149              
150             # Finish up!
151 99         795 print $out "--$bound--\n";
152 99         662 return $self;
153             }
154              
155             ##############################################################################
156              
157             =head2 Accessors
158              
159             In addition to those supported by L,
160             SVN::Notify::Alternative supports the following accessors:
161              
162             =head3 alternatives
163              
164             my $alts = $notify->alternatives;
165             $notify->alternatives($alts);
166              
167             Gets or sets the value of the C attribute, which must always be
168             set to an array reference.
169              
170             =cut
171              
172             1;
173             __END__