FromMail.pm

filter: Statementstmt 22.1% 25 / 113 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 7 / 14 Podpod 85.7% 6 / 7 totaltotal 17.1% 38 / 222 CC 43 SCAR 43.9
CC
43
Cov
13%
init
132.0
export
132.0
expandFiles
110.0
CRAP 80.9
« FromMail/Field.pm ↑ Summary FromMail/Head.pm »
1 # This code is part of Perl distribution HTML-FromMail version 4.00.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2003-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package HTML::FromMail;{
13 1 our $VERSION = '4.00';
14 }
15  
16 1 ▶use base 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Log::Report 'html-frommail';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use File::Spec::Functions qw/catfile catdir file_name_is_absolute/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use File::Basename qw/basename dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 my %default_producers = ( # classes will be compiled automatically when used
27 'Mail::Message' => 'HTML::FromMail::Message',
28 'Mail::Message::Head' => 'HTML::FromMail::Head',
29 'Mail::Message::Field' => 'HTML::FromMail::Field',
30 );
31  
32 #--------------------
33  
34 sub init($)
35 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=11 SCAR=48.8
Pod undocumented
36 0 $self->SUPER::init($args);
37  
38 # Defining the formatter to be used
39 0 ▶ my $form = $args->{formatter} || {};
Condition0%0/2
$args->{'formatter'} || {}
l!l
00
MC/DC0%0/1
$args->{'formatter'} || {}
$args->{'formatter'}
Truth table0%0/2
A
A= $args->{'formatter'}
Aresult
11
00
40 0 ▶ if(!ref $form)
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $form) { } 0 0 0
elsif (ref $form eq 'HASH') { } 0 0 0
41 0 { eval "require $form";
42 0 ▶ $@ and panic "Formatter $form can not be used:\n$@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and &panic("Formatter $form can not be used:\n$@") 0 0 0
43 0 $form = $form->new;
44 }
45 elsif(ref $form eq 'HASH')
46 0 { require HTML::FromMail::Format::OODoc;
47 0 $form = HTML::FromMail::Format::OODoc->new(%$form);
48 }
49  
50 0 ▶ defined $form
Branch: 1 branch
BranchTrueFalseTotal
defined $form or &error(&__x("formatter {class} could not be instantiated.", "class", $form)) 0 0 0
51 or error __x"formatter {class} could not be instantiated.", class => $form;
52  
53 0 $self->{HF_formatter} = $form;
54  
55 # Defining the producers
56 0 my %prod = %default_producers; # copy
57 0 ▶ my $prod = $args->{producers} || {};
Condition0%0/2
$args->{'producers'} || {}
l!l
00
MC/DC0%0/1
$args->{'producers'} || {}
$args->{'producers'}
Truth table0%0/2
A
A= $args->{'producers'}
Aresult
11
00
58 0 @prod{ keys %$prod } = values %$prod;
59 0 ▶ while( my($class, $impl) = each %prod)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %prod) 0 0 0
60 0 { $self->producer($class, $impl);
61 }
62  
63 # Collect the settings
64 0 ▶ my $settings = $args->{settings} || {};
Condition0%0/2
$args->{'settings'} || {}
l!l
00
MC/DC0%0/1
$args->{'settings'} || {}
$args->{'settings'}
Truth table0%0/2
A
A= $args->{'settings'}
Aresult
11
00
65 0 ▶ while( my ($topic, $defaults) = each %$settings)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$settings) 0 0 0
66 0 { $self->settings($topic, $defaults);
67 }
68  
69 0 ▶ $self->{HF_templates} = $args->{templates} || '.';
Condition0%0/2
$args->{'templates'} || '.'
l!l
00
MC/DC0%0/1
$args->{'templates'} || '.'
$args->{'templates'}
Truth table0%0/2
A
A= $args->{'templates'}
Aresult
11
00
70 0 $self;
71 }
72  
73 #--------------------
74  
75 0 ▶sub formatter() { $_[0]->{HF_formatter} }
Subroutine formatter: not called CC=1 SCAR=6.9
Pod documented
76  
77 #-----------
78  
79  
80 sub producer($;$)
81 0 ▶{ my ($self, $thing) = (shift, shift);
Subroutine producer: not called CC=7 SCAR=37.1
Pod documented
82 0 ▶ my $class = ref $thing || $thing;
Condition0%0/3
ref $thing || $thing
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $thing || $thing
ref $thing $thing
Truth table0%0/3
A || B
A= ref $thingB= $thing
ABresult
1X1
011
000
83  
84 0 ▶ return ($self->{HF_producer}{$class} = shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
85 0 ▶ if(my $prod = $self->{HF_producer}{$class})
Branch: 1 branch
BranchTrueFalseTotal
if (my $prod = $self->{"HF_producer"}{$class}) 0 0 0
86 0 { eval "require $prod";
87 0 ▶ $@ and error __x"cannot use {producer} for {class}:\n$@", producer => $prod, class => $class, error => $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and &error(&__x("cannot use {producer} for {class}:\n$@", "producer", $prod, "class", $class, "error", $@)) 0 0 0
88 0 return $prod->new;
89 }
90  
91 # Look for producer in the inheritance structure
92 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 0 foreach ( @{"$class\::ISA"} )
94 0 { my $prod = $self->producer($_);
95 0 ▶ return $prod if defined $prod;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prod 0 0 0
96 }
97  
98 0 undef;
99 }
100  
101  
102 sub templates(;$)
103 0 ▶{ my $self = shift;
Subroutine templates: not called CC=5 SCAR=34.0
Pod documented
104 0 ▶ return $self->{HF_templates} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
105  
106 0 ▶ my $topic = blessed $_[0] ? shift->topic : shift;
Branch: 1 branch
BranchTrueFalseTotal
[0] ? : 0 0 0
107 0 my $templates= $self->{HF_templates};
108  
109 0 my $filename = catfile $templates, $topic;
110 0 ▶ return $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 0 0
111  
112 0 my $dirname = catdir $templates, $topic;
113 0 ▶ return $dirname if -d $dirname;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dirname 0 0 0
114  
115 0 error __x"cannot find template file or directory '{topic}' in '{directory}'.",
116 topic => $topic, directory => $templates;
117 }
118  
119  
120 sub settings($;@)
121 0 ▶{ my $self = shift;
Subroutine settings: not called CC=4 SCAR=30.0
Pod documented
122 0 ▶ my $topic = blessed $_[0] ? shift->topic : shift;
Branch: 1 branch
BranchTrueFalseTotal
[0] ? : 0 0 0
123 0 ▶ @_ or return $self->{HF_settings}{$topic};
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self->{"HF_settings"}{$topic} 0 0 0
124  
125 0 ▶ $self->{HF_settings}{$topic} = @_ == 1 ? shift : +{ @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
126 }
127  
128  
129 sub export($@)
130 0 ▶{ my ($self, $object, %args) = @_;
Subroutine export: not called CC=11 SCAR=48.8
Pod documented
131  
132 0 ▶ my $producer = $self->producer($object)
Branch: 1 branch
BranchTrueFalseTotal
my $producer = $self->producer($object) or &error(&__x("no producer for {class} objects.", "class", ref $object)) 0 0 0
133 or error __x"no producer for {class} objects.", class => ref $object;
134  
135 my $output = $args{output}
136 0 ▶ or error __x"no output directory or file specified.";
Branch: 1 branch
BranchTrueFalseTotal
my $output = $args{"output"} or &error(&__x("no output directory or file specified.")) 0 0 0
137  
138 # this cannot be right when $output isa filename?
139 # $self->log(ERROR => "Cannot create output directory $output: $!"), return
140 # unless -d $output || mkdir $output;
141  
142 0 my $topic = $producer->topic;
143 0 my @files;
144 0 ▶ if(my $input = $args{use})
Branch: 1 branch
BranchTrueFalseTotal
if (my $input = $args{'use'}) { } 0 0 0
145 { # some template files are explicitly named
146 0 my $templates = $self->templates;
147  
148 0 ▶ foreach my $in (ref $input ? @$input : $input)
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 0 0 0
149 0 ▶ { my $fn = file_name_is_absolute($in) ? $in : catfile($templates, $in);
Branch: 1 branch
BranchTrueFalseTotal
file_name_is_absolute($in) ? : 0 0 0
150 0 ▶ -f $fn or warning(__x"no template file {file}.", file => $fn), next;
Branch: 1 branch
BranchTrueFalseTotal
-f $fn or &warning(&__x("no template file {file}.", "file", $fn)), next 0 0 0
151  
152 0 push @files, $fn;
153 }
154 }
155 else
156 0 ▶ { my $templates = $self->templates($topic)
Branch: 1 branch
BranchTrueFalseTotal
my $templates = $self->templates($topic) or &warning(&__x("no templates for {topic} objects.", "topic", $topic)), return 0 0 0
157 or warning(__x"no templates for {topic} objects.", topic => $topic), return;
158  
159 0 @files = $self->expandFiles($templates);
160 0 ▶ @files or warning __x"no templates found in {dir} directory.", dir => $templates;
Branch: 1 branch
BranchTrueFalseTotal
@files or &warning(&__x("no templates found in {dir} directory.", "dir", $templates)) 0 0 0
161 }
162  
163 0 my $formatter = $self->formatter(settings => $self->{HF_settings});
164 0 my @outfiles;
165  
166 0 foreach my $infile (@files)
167 0 { my $basename = basename $infile;
168 0 my $outfile = catfile $output, $basename;
169 0 push @outfiles, $outfile;
170  
171 0 $formatter->export(
172 %args,
173 object => $object, input => $infile,
174 producer => $producer, formatter => $formatter,
175 output => $outfile, outdir => $output,
176 main => $self,
177 );
178 }
179  
180 0 $outfiles[0];
181 }
182  
183  
184 sub expandFiles($)
185 0 ▶{ my ($self, $thing) = @_;
Subroutine expandFiles: not called CC=10 SCAR=47.0
Pod documented
186 0 ▶ return @$thing if ref $thing eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $thing eq "ARRAY" 0 0 0
187 0 ▶ return $thing if -f $thing;
Branch: 1 branch
BranchTrueFalseTotal
if -f $thing 0 0 0
188  
189 0 ▶ -d $thing
Branch: 1 branch
BranchTrueFalseTotal
-d $thing or &warning(&__x("cannot find directory {dir}.", "dir", $thing)), (return ()) 0 0 0
190 or warning(__x"cannot find directory {dir}.", dir => $thing), return ();
191  
192 0 ▶ opendir DIR, $thing
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $thing or &fault(&__x("cannot read from directory {dir}", "dir", $thing)) 0 0 0
193 or fault __x"cannot read from directory {dir}", dir => $thing;
194  
195 0 my @files;
196 0 ▶ while(my $item = readdir DIR)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = readdir DIR)) 0 0 0
197 0 ▶ { next if $item eq '.' || $item eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "." or $item eq ".." 0 0 0
Condition0%0/3
$item eq "." or $item eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$item eq "." or $item eq ".."
$item eq "." $item eq ".."
Truth table0%0/3
A or B
A= $item eq "."B= $item eq ".."
ABresult
1X1
011
000
198  
199 0 my $full = catfile $thing, $item;
200 0 ▶ if(-f $full)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $full) 0 0 0
201 0 { push @files, $full;
202 0 next;
203 }
204  
205 0 $full = catdir $thing, $item;
206 0 ▶ if(-d $full)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $full) 0 0 0
207 0 { push @files, $self->expandFiles($full);
208 0 next;
209 }
210  
211 0 warning __x"skipping {name}, which is neither file or directory.", name => $full;
212 }
213  
214 0 closedir DIR;
215 0 @files;
216 }
217  
218 #--------------------
219  
220 1 1;
« FromMail/Field.pm ↑ Summary FromMail/Head.pm »