Labyrinth/Writer.pm

filter: Statementstmt 33.9% 39 / 115 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 66.6% 10 / 15 Podpod 100% 6 / 6 totaltotal 23.0% 55 / 239 CC 35 SCAR 60.5
CC
35
Cov
20%
Publish
756.0
Config
30.0
Transform
30.0
CRAP 423.0
« Labyrinth/Globals.pm ↑ Summary Labyrinth.pm »
1 package Labyrinth::Writer;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::Writer - Output Management for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::Writer;
16 Publish('mytemplate.html');
17 PublishCode('MESSAGE');
18  
19 =head1 DESCRIPTION
20  
21 The Publish package contains one function, Publish(), which handles
22 the parsing of a given template with global variables and prints the result.
23  
24 =head1 EXPORT
25  
26 Publish
27 PublishCode
28 UnPublish
29 Transform
30 Croak
31  
32 =cut
33  
34 # -------------------------------------
35 # Export Details
36  
37 12 require Exporter;
38 12 @ISA = qw( Exporter );
39 12 @EXPORT_OK = qw( Publish PublishCode UnPublish Transform Croak );
40 12 @EXPORT = qw( Publish PublishCode UnPublish Transform Croak );
41  
42 # -------------------------------------
43 # Library Modules
44  
45 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 12 ▶use Labyrinth::Globals;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 12 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 12 ▶use Labyrinth::MLUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # -------------------------------------
54 # Variables
55  
56 12 my ($PARSER,$RENDER);
57  
58 12 my $published;
59  
60 12 my %codes = (
61 BADLAYOUT => 'public/badlayout.html',
62 BADPAGE => 'public/badpage.html',
63 BADCMD => 'public/badcommand.html',
64 MESSAGE => 'public/error_message.html',
65 );
66  
67 12 my %binary = (
68 pdf => 'application/pdf'
69 );
70  
71 12 my %knowntypes = (
72 html => 'text/html',
73 ics => 'text/calendar',
74 #js => 'application/javascript',
75 js => 'text/html',
76 json => 'application/json',
77 rss => 'application/xml',
78 txt => 'text/plain',
79 xml => 'application/xml',
80 yml => 'text/yaml',
81 yaml => 'text/yaml'
82 );
83  
84 # -------------------------------------
85 # The Subs
86  
87 =head1 FUNCTIONS
88  
89 =over 4
90  
91 =item Config()
92  
93 Configure template parser and output method.
94  
95 =cut
96  
97 0 ▶sub Config {
Subroutine Config: not called CC=5 SCAR=34.0
Pod documented
98 0 ▶ $settings{'writer-parser'} ||= 'TT';
Condition0%0/2
$Labyrinth::Writer::settings{"writer-parser"} ||= "TT"
l!l
00
MC/DC0%0/1
$Labyrinth::Writer::settings{"writer-parser"} ||= "TT"
$Labyrinth::Writer::settings{"writer-parser"}
Truth table0%0/2
A
A= $Labyrinth::Writer::settings{"writer-parser"}
Aresult
11
00
99 0 ▶ $settings{'writer-render'} ||= 'CGI';
Condition0%0/2
$Labyrinth::Writer::settings{"writer-render"} ||= "CGI"
l!l
00
MC/DC0%0/1
$Labyrinth::Writer::settings{"writer-render"} ||= "CGI"
$Labyrinth::Writer::settings{"writer-render"}
Truth table0%0/2
A
A= $Labyrinth::Writer::settings{"writer-render"}
Aresult
11
00
100  
101 0 my $parser = 'Labyrinth::Writer::Parser::' . $settings{'writer-parser'};
102 0 my $render = 'Labyrinth::Writer::Render::' . $settings{'writer-render'};
103  
104 0 eval {
105 0 eval "CORE::require $parser";
106 0 $PARSER = $parser->new();
107 };
108 0 ▶ die "Cannot load Writer::Parser package for '$settings{'writer-parser'}': $@" if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
109  
110 0 eval {
111 0 eval "CORE::require $render";
112 0 $RENDER = $render->new();
113 };
114 0 ▶ die "Cannot load Writer::Render package for '$settings{'writer-render'}': $@" if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
115  
116 }
117  
118 =item Publish()
119  
120 Publish() parses a given template, via Template Toolkit, and prints the
121 result.
122  
123 =item PublishCode
124  
125 Publishes a template based on an internal code. Current codes and associated
126 templates are:
127  
128 BADLAYOUT => 'public/badlayout.html',
129 BADPAGE => 'public/badpage.html',
130 BADCMD => 'public/badcommand.html',
131 MESSAGE => 'public/error_message.html',
132  
133 TODO: Provide these and more as configurable codes.
134  
135 =item UnPublish
136  
137 Used to reset publishing status. Usually only applicable in mod_perl
138 environments.
139  
140 =item Transform
141  
142 Given a template and a set of variables, parse without publishing the content.
143  
144 =cut
145  
146 0 ▶sub Publish {
Subroutine Publish: not called CC=27 SCAR=66.3
Pod documented
147 0 ▶ return if($published);
Branch: 1 branch
BranchTrueFalseTotal
if $published 0 0 0
148  
149 0 ▶ Config() unless($PARSER && $RENDER);
Branch: 1 branch
BranchTrueFalseTotal
unless $PARSER and $RENDER 0 0 0
Condition0%0/3
$PARSER and $RENDER
!ll&&!rl&&r
000
MC/DC0%0/2
$PARSER and $RENDER
$PARSER $RENDER
Truth table0%0/3
A and B
A= $PARSERB= $RENDER
ABresult
0X0
100
111
150  
151 # redirects require minimal processing
152 0 ▶ if($tvars{redirect} && $tvars{redirect} ne $tvars{request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Writer::tvars{"redirect"} and $Labyrinth::Writer::tvars{"redirect"} ne $Labyrinth::Writer::tvars{"request"}) 0 0 0
Condition0%0/3
$Labyrinth::Writer::tvars{"redirect"} and $Labyrinth::Writer::tvars{"redirect"} ne $Labyrinth::Writer::tvars{"request"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Writer::tvars{"redirect"} and $Labyrinth::Writer::tvars{"redirect"} ne $Labyrinth::Writer::tvars{"request"}
$Labyrinth::Writer::tvars{"redirect"} $Labyrinth::Writer::tvars{"redirect"} ne $Labyrinth::Writer::tvars{"request"}
Truth table0%0/3
A and B
A= $Labyrinth::Writer::tvars{"redirect"}B= $Labyrinth::Writer::tvars{"redirect"} ne $Labyrinth::Writer::tvars{"request"}
ABresult
0X0
100
111
153 0 $RENDER->redirect($tvars{redirect});
154 0 $published = 1;
155 0 return;
156 }
157  
158 # binary files handled directly
159 0 ▶ if($tvars{contenttype} && $binary{$tvars{contenttype}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Writer::tvars{"contenttype"} and $binary{$Labyrinth::Writer::tvars{"contenttype"}}) 0 0 0
Condition0%0/3
$Labyrinth::Writer::tvars{"contenttype"} and $binary{$Labyrinth::Writer::tvars{"contenttype"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Writer::tvars{"contenttype"} and $binary{$Labyrinth::Writer::tvars{"contenttype"}}
$Labyrinth::Writer::tvars{"contenttype"} $binary{$Labyrinth::Writer::tvars{"contenttype"}}
Truth table0%0/3
A and B
A= $Labyrinth::Writer::tvars{"contenttype"}B= $binary{$Labyrinth::Writer::tvars{"contenttype"}}
ABresult
0X0
100
111
160 0 $tvars{'writer'} = { 'ctype' => $binary{$tvars{'contenttype'}}, 'file' => $tvars{'file'} };
161 0 $RENDER->binary($tvars{'writer'});
162 0 $published = 1;
163 0 return;
164 }
165  
166 0 ▶ my $path = $settings{'templates'} || '';
Condition0%0/2
$Labyrinth::Writer::settings{'templates'} || ''
l!l
00
MC/DC0%0/1
$Labyrinth::Writer::settings{'templates'} || ''
$Labyrinth::Writer::settings{'templates'}
Truth table0%0/2
A
A= $Labyrinth::Writer::settings{'templates'}
Aresult
11
00
167 0 my $vars = \%tvars;
168  
169 0 ▶ unless($vars->{'layout'} && -r "$path/$vars->{'layout'}") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vars->{"layout"} and -r qq[$path/$vars->{"layout"}]) 0 0 0
Condition0%0/3
$vars->{"layout"} and -r qq[$path/$vars->{"layout"}]
!ll&&!rl&&r
000
MC/DC0%0/2
$vars->{"layout"} and -r qq[$path/$vars->{"layout"}]
$vars->{"layout"} -r qq[$path/$vars->{"layout"}]
Truth table0%0/3
A and B
A= $vars->{"layout"}B= -r qq[$path/$vars->{"layout"}]
ABresult
0X0
100
111
170 0 $vars->{'badlayout'} = $vars->{'layout'};
171 0 $vars->{'layout'} = $codes{BADLAYOUT};
172 }
173 0 ▶ unless($vars->{'content'} && -r "$path/$vars->{'content'}") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vars->{"content"} and -r qq[$path/$vars->{"content"}]) 0 0 0
Condition0%0/3
$vars->{"content"} and -r qq[$path/$vars->{"content"}]
!ll&&!rl&&r
000
MC/DC0%0/2
$vars->{"content"} and -r qq[$path/$vars->{"content"}]
$vars->{"content"} -r qq[$path/$vars->{"content"}]
Truth table0%0/3
A and B
A= $vars->{"content"}B= -r qq[$path/$vars->{"content"}]
ABresult
0X0
100
111
174 0 $vars->{'badcontent'} = $vars->{'content'};
175 0 $vars->{'content'} = $codes{BADPAGE};
176 }
177 0 my $layout = $vars->{'layout'};
178 0 my $content = $vars->{'content'};
179  
180 # LogDebug( "layout=[$layout]" );
181 # LogDebug( "content=[$content]" );
182 # LogDebug( "cookie=[$vars->{cookie}]" ) if($vars->{cookie});
183 # use Data::Dumper;
184 # LogDebug( "vars=".Dumper($vars) );
185  
186  
187 0 ▶ $vars->{evalperl} = ($content eq $codes{BADPAGE} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$content eq $codes{'BADPAGE'} ? : 0 0 0
188  
189 #LogDebug("<!-- $layout : $content -->");
190  
191 0 my $output;
192 0 eval { $output = $PARSER->parser($layout,$vars) };
193 0 ▶ if($@ || !$output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $output) 0 0 0
Condition0%0/3
$@ or not $output
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $output
$@ not $output
Truth table0%0/3
A or not(B)
A= $@B= not $output
ABresult
1X1
011
000
194 0 LogDebug( "template error=$@" );
195 0 $$output = $@;
196 }
197  
198 0 my ($ext) = $layout =~ m/\.(\w+)$/;
199 0 ▶ $ext ||= 'html';
Condition0%0/2
$ext ||= "html"
l!l
00
MC/DC0%0/1
$ext ||= "html"
$ext
Truth table0%0/2
A
A= $ext
Aresult
11
00
200  
201 # split HTML and process etc
202 0 ▶ if($ext =~ /htm/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext =~ /htm/) 0 0 0
203 0 ▶ if(defined $settings{parsehtml} && $settings{parsehtml}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Labyrinth::Writer::settings{"parsehtml"} and $Labyrinth::Writer::settings{"parsehtml"}) 0 0 0
Condition0%0/3
defined $Labyrinth::Writer::settings{"parsehtml"} and $Labyrinth::Writer::settings{"parsehtml"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Labyrinth::Writer::settings{"parsehtml"} and $Labyrinth::Writer::settings{"parsehtml"}
defined $Labyrinth::Writer::settings{"parsehtml"} $Labyrinth::Writer::settings{"parsehtml"}
Truth table0%0/3
A and B
A= defined $Labyrinth::Writer::settings{"parsehtml"}B= $Labyrinth::Writer::settings{"parsehtml"}
ABresult
0X0
100
111
204 0 my ($top,$body,$tail) = ($$output =~ m!^(.*?<body[^>]*>)(.*?)(</body>.*)$!si);
205 # LogDebug( "parsehtml=[$settings{parsehtml}]" );
206 # LogDebug( "html=[$html]" );
207 # LogDebug( "top=[$top]" );
208 # LogDebug( "tail=[$tail]" );
209 # LogDebug( "body=[$body]" );
210 0 my $html = $top . process_html($body,0,1) . $tail;
211  
212 0 ▶ if($settings{parsetest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Writer::settings{"parsetest"}) 0 0 0
213 0 DumpToFile($settings{parsetest},"=== ORIGINAL ===",$$output,"=== PROCESSED ===",$html,"=== END ===");
214 }
215 0 $output = \$html;
216 }
217 }
218  
219 0 ▶ $tvars{headers}{type} = $knowntypes{$ext} || do {
Condition0%0/3
$knowntypes{$ext} || do { my $types = 'MIME::Types'->new; my $mime = $types->mimeTypeOf($ext); $mime->type or 'text/html' }
l!l&&r!l&&!r
000
MC/DC0%0/2
$knowntypes{$ext} || do { my $types = 'MIME::Types'->new; my $mime = $types->mimeTypeOf($ext); $mime->type or 'text/html' }
$knowntypes{$ext} do { my $types = 'MIME::Types'->new; my $mime = $types->mimeTypeOf($ext); $mime->type or 'text/html' }
Truth table0%0/3
A || B
A= $knowntypes{$ext}B= do { my $types = 'MIME::Types'->new; my $mime = $types->mimeTypeOf($ext); $mime->type or 'text/html' }
ABresult
1X1
011
000
220 0 my $types = MIME::Types->new;
221 0 my $mime = $types->mimeTypeOf($ext);
222 0 ▶ $mime->type || 'text/html';
Condition0%0/2
$mime->type || 'text/html'
l!l
00
MC/DC0%0/1
$mime->type || 'text/html'
$mime->type
Truth table0%0/2
A
A= $mime->type
Aresult
11
00
223 };
224  
225 0 $tvars{headers}{'charset'} = 'utf-8';
226 0 ▶ $tvars{headers}{'status'} = '404 Page Not Found' if($content eq $codes{BADPAGE} || $content eq $codes{BADCMD});
Branch: 1 branch
BranchTrueFalseTotal
if $content eq $codes{"BADPAGE"} or $content eq $codes{"BADCMD"} 0 0 0
Condition0%0/3
$content eq $codes{"BADPAGE"} or $content eq $codes{"BADCMD"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$content eq $codes{"BADPAGE"} or $content eq $codes{"BADCMD"}
$content eq $codes{"BADPAGE"} $content eq $codes{"BADCMD"}
Truth table0%0/3
A or B
A= $content eq $codes{"BADPAGE"}B= $content eq $codes{"BADCMD"}
ABresult
1X1
011
000
227 0 ▶ $tvars{headers}{'cookie'} = $tvars{cookie} if($tvars{cookie});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Writer::tvars{"cookie"} 0 0 0
228 0 ▶ $tvars{headers}{'attachment'} = basename($content) if($layout =~ /\.ics$/);
Branch: 1 branch
BranchTrueFalseTotal
if $layout =~ /\.ics$/ 0 0 0
229  
230 0 $published = 1;
231  
232 0 return $RENDER->publish($tvars{headers}, $output);
233 }
234  
235 0 ▶sub PublishCode {
Subroutine PublishCode: not called CC=1 SCAR=6.9
Pod documented
236 0 $tvars{'content'} = $codes{$_[0]};
237 0 return Publish();
238 }
239  
240 2 ▶sub UnPublish {
Subroutine UnPublish: called CC=1 SCAR=0.0
Pod documented
241 2 $published = 0;
242 }
243  
244 0 ▶sub Transform {
Subroutine Transform: not called CC=5 SCAR=34.0
Pod documented
245 0 my ($template,$vars,$file,$binary) = @_;
246  
247 0 my $path = $settings{'templates'};
248 0 my $layout = "$path/$template";
249  
250 0 ▶ die "Missing template [$layout]\n" unless(-e $layout);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $layout 0 0 0
251  
252 0 ▶ Config() unless($PARSER && $RENDER);
Branch: 1 branch
BranchTrueFalseTotal
unless $PARSER and $RENDER 0 0 0
Condition0%0/3
$PARSER and $RENDER
!ll&&!rl&&r
000
MC/DC0%0/2
$PARSER and $RENDER
$PARSER $RENDER
Truth table0%0/3
A and B
A= $PARSERB= $RENDER
ABresult
0X0
100
111
253  
254 0 ▶ if($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 0 0
255 0 $PARSER->parse_to_file($layout,$vars,$file,$binary);
256 0 return;
257 }
258  
259 0 my $output = $PARSER->parser($layout,$vars);
260 0 return $$output;
261 }
262  
263 =item Croak
264  
265 A shorthand call to publish and record errors.
266  
267 =cut
268  
269 0 ▶sub Croak {
Subroutine Croak: not called CC=1 SCAR=6.9
Pod documented
270 0 my $errmess = join(" ",@_);
271 0 LogError($errmess);
272 0 print STDERR "$errmess\n";
273 0 PublishCode('MESSAGE');
274 0 exit;
275 }
276  
277 12 1;
278  
279 __END__
« Labyrinth/Globals.pm ↑ Summary Labyrinth.pm »