FromMail.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 222 SCAR 69.0
CC
31
Cov
0%
export
90.0
expandFiles
72.0
init
30.0
CRAP 992.0
1 # Copyrights 2003-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution HTML-FromMail. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package HTML::FromMail;
10 use vars '$VERSION';
11 $VERSION = '0.12';
12  
13 use base 'Mail::Reporter';
14  
15 use strict;
16 use warnings;
17  
18 use File::Spec::Functions;
19 use File::Basename qw/basename dirname/;
20  
21 my %default_producers = # classes will be compiled automatically when used
22 ( 'Mail::Message' => 'HTML::FromMail::Message'
23 , 'Mail::Message::Head' => 'HTML::FromMail::Head'
24 , 'Mail::Message::Field' => 'HTML::FromMail::Field'
25 );
26  
27  
28 sub init($)
29 { my ($self, $args) = @_;
30  
31 $self->SUPER::init($args) or return;
32  
33 # Defining the formatter to be used
34 my $form = $args->{formatter} || {};
35 if(!ref $form)
36 { eval "require $form";
37 die "ERROR: Formatter $form can not be used:\n$@" if $@;
38 $form = $form->new;
39 }
40 elsif(ref $form eq 'HASH')
41 { require HTML::FromMail::Format::OODoc;
42 $form = HTML::FromMail::Format::OODoc->new(%$form);
43 }
44  
45 die "ERROR: Formatter $form could not be instantiated\n"
46 unless defined $form;
47  
48 $self->{HF_formatter} = $form;
49  
50 # Defining the producers
51 my %prod = %default_producers; # copy
52 my $prod = $args->{producers} || {};
53 @prod{ keys %$prod } = values %$prod;
54 while( my($class, $impl) = each %prod)
55 { $self->producer($class, $impl);
56 }
57  
58 # Collect the settings
59 my $settings = $args->{settings} || {};
60 while( my ($topic, $defaults) = each %$settings)
61 { $self->settings($topic, $defaults);
62 }
63  
64 $self->{HF_templates} = $args->{templates} || '.';
65 $self;
66 }
67  
68  
69 sub formatter() { shift->{HF_formatter} }
70  
71  
72 sub producer($;$)
73 { my ($self, $thing) = (shift, shift);
74 my $class = ref $thing || $thing;
75  
76 return ($self->{HF_producer}{$class} = shift) if @_;
77 if(my $prod = $self->{HF_producer}{$class})
78 { eval "require $prod";
79 return $prod->new unless $@;
80  
81 $self->log(ERROR => "Cannot use $prod for $class:\n$@");
82 return undef;
83 }
84  
85 # Look for producer in the inheritance structure
86 no strict 'refs';
87 foreach ( @{"$class\::ISA"} )
88 { my $prod = $self->producer($_);
89 return $prod if defined $prod;
90 }
91  
92 undef;
93 }
94  
95  
96 sub templates(;$)
97 { my $self = shift;
98 return $self->{HF_templates} unless @_;
99  
100 my $topic = ref $_[0] ? shift->topic : shift;
101 my $templates= $self->{HF_templates};
102  
103 my $filename = catfile($templates, $topic);
104 return $filename if -f $filename;
105  
106 my $dirname = catdir($templates, $topic);
107 return $dirname if -d $dirname;
108  
109 $self->log(ERROR =>
110 "Cannot find template file or directory '$topic' in '$templates'.\n");
111 undef;
112 }
113  
114  
115 sub settings($;@)
116 { my $self = shift;
117 my $topic = ref $_[0] ? shift->topic : shift;
118 return $self->{HF_settings}{$topic} unless @_;
119  
120 $self->{HF_settings}{$topic} = @_ == 1 ? shift : { @_ };
121 }
122  
123  
124 sub export($@)
125 { my ($self, $object, %args) = @_;
126  
127 my $producer = $self->producer($object);
128 $self->log(ERROR => "No producer for ",ref($object), " objects."), return
129 unless defined $producer;
130  
131 my $output = $args{output};
132 $self->log(ERROR => "No output directory or file specified."), return
133 unless defined $output;
134  
135 # this cannot be right when $output isa filename?
136 # $self->log(ERROR => "Cannot create output directory $output: $!"), return
137 # unless -d $output || mkdir $output;
138  
139 my $topic = $producer->topic;
140 my @files;
141 if(my $input = $args{use})
142 { # some template files are explicitly named
143 my $templates = $self->templates;
144  
145 foreach my $in (ref $input ? @$input : $input)
146 { my $fn = file_name_is_absolute($in) ? $in
147 : catfile($templates, $in);
148  
149 $self->log(WARNING => "No template file $fn"), next
150 unless -f $fn;
151  
152 push @files, $fn;
153 }
154 }
155 else
156 { my $templates = $self->templates($topic);
157 $self->log(WARNING => "No templates for $topic objects."), return
158 unless defined $templates;
159  
160 @files = $self->expandFiles($templates);
161 $self->log(WARNING => "No templates found in $templates directory.")
162 unless @files;
163 }
164  
165 my $formatter = $self->formatter(settings => $self->{HF_settings});
166 my @outfiles;
167  
168 foreach my $infile (@files)
169 { my $basename = basename $infile;
170 my $outfile = catfile($output, $basename);
171 push @outfiles, $outfile;
172  
173 $formatter->export
174 ( %args
175 , object => $object, input => $infile
176 , producer => $producer, formatter => $formatter
177 , output => $outfile, outdir => $output
178 , main => $self
179 );
180 }
181  
182 $outfiles[0];
183 }
184  
185  
186 sub expandFiles($)
187 { my ($self, $thing) = @_;
188 return @$thing if ref $thing eq 'ARRAY';
189 return $thing if -f $thing;
190  
191 $self->log(WARNING => "Cannot find $thing"), return ()
192 unless -d $thing;
193  
194 $self->log(ERROR => "Cannot read from directory $thing: $!"), return ()
195 unless opendir DIR, $thing;
196  
197 my @files;
198 while(my $item = readdir DIR)
199 { next if $item eq '.' || $item eq '..';
200  
201 my $full = catfile $thing, $item;
202 if(-f $full)
203 { push @files, $full;
204 next;
205 }
206  
207 $full = catdir $thing, $item;
208 if(-d $full)
209 { push @files, $self->expandFiles($full);
210 next;
211 }
212  
213 $self->log(WARNING =>
214 "Skipping $full, which is neither file or directory.");
215 }
216  
217 closedir DIR;
218 @files;
219 }
220  
221  
222 1;