lib/Templer/Site/Page.pm

filter: Statementstmt 86.8% 53 / 61 Branchbran 62.5% 15 / 24 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 8 / 8 totaltotal 77.5% 90 / 116 CC 18 SCAR 21.1
CC
18
Cov
76%
content
19.6
_parse_page
8.1
new
4.1
CRAP 8.2
« lib/Templer/Site/New.pm ↑ Summary blib/lib/Templer/Timer.pm »
1  
2 =head1 NAME
3  
4 Templer::Site::Page - An interface to a site page.
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 use strict;
11 use warnings;
12  
13 use Templer::Site::Page;
14  
15 my $page = Templer::Site::Page->new( file => "./input/foo.wgn" );
16  
17 =cut
18  
19 =head1 DESCRIPTION
20  
21 A page is any non-directory beneath the input-directory which matches the
22 pattern specified by the user (defaults to "*.skx").
23  
24 Pages are processed via the L<HTML::Template> module to create the suitable
25 output.
26  
27 In C<templer> the page objects are created by the L<Templer::Site> module.
28  
29 =cut
30  
31 =head1 LICENSE
32  
33 This module is free software; you can redistribute it and/or modify it
34 under the terms of either:
35  
36 a) the GNU General Public License as published by the Free Software
37 Foundation; either version 2, or (at your option) any later version,
38 or
39  
40 b) the Perl "Artistic License".
41  
42 =cut
43  
44 =head1 AUTHOR
45  
46 Steve Kemp <steve@steve.org.uk>
47  
48 =cut
49  
50 =head1 COPYRIGHT AND LICENSE
51  
52 Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>.
53  
54 This library is free software. You can modify and or distribute it under
55 the same terms as Perl itself.
56  
57 =cut
58  
59 =head1 METHODS
60  
61 =cut
62  
63  
64 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67  
68 package Templer::Site::Page;
69  
70  
71  
72 =head2 new
73  
74 The constructor.
75  
76 The single appropriate argument is the hash-key "file", pointing to the
77 page-file on-disk.
78  
79 =cut
80  
81 sub new
82 9 ▶{
Subroutine new: called CC=4 SCAR=14.0
Pod documented
83 9 my ( $proto, %supplied ) = (@_);
84 9 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
85  
86 9 my $self = {};
87  
88 #
89 # Allow user supplied values to override our defaults
90 #
91 9 foreach my $key ( keys %supplied )
92 {
93 12 $self->{ lc $key } = $supplied{ $key };
94 }
95  
96 9 bless( $self, $class );
97 9 ▶ $self->_parse_page( $self->{ 'file' } ) if ( $self->{ 'file' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 8 1 9
98 9 return $self;
99 }
100  
101  
102 =head2 _parse_page
103  
104 Read the file, and parse the header/content.
105  
106 This is an internal method.
107  
108 =cut
109  
110 sub _parse_page
111 8 ▶{
Subroutine _parse_page: called CC=8 SCAR=20.9
112 8 my ( $self, $filename ) = (@_);
113  
114 8 ▶ open( my $handle, "<:utf8", $filename ) or
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:utf8", $filename or die "Failed to read '${filename}' - $!" 8 0 8
115 die "Failed to read '$filename' - $!";
116 8 binmode( $handle, ":utf8" );
117  
118 8 my $header = 1;
119  
120 8 ▶ while ( my $line = <$handle> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 44 8 52
121 {
122  
123 # strip trailing newline.
124 44 $line =~ s/[\r\n]*//g;
125  
126 44 ▶ if ($header)
Branch: 1 branch
BranchTrueFalseTotal
if ($header) { } 28 16 44
127 {
128 28 ▶ if ( $line =~ /^([^:]+):(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:]+):(.*)$/) 20 8 28
129 {
130 20 my $key = $1;
131 20 my $val = $2;
132 20 $key = lc($key);
133 20 $key =~ s/^\s+|\s+$//g;
134 20 $val =~ s/^\s+|\s+$//g;
135  
136 20 $self->{ $key } = $val;
137 print "Templer::Site::Page set: $key => $val\n"
138 20 ▶ if ( $self->{ 'debug' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 20 20
139 }
140 28 ▶ if ( $line =~ /^----[\r\n]*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^----[\r\n]*$/) 8 20 28
141 {
142 8 $header = undef;
143 }
144 }
145 else
146 {
147 16 $self->{ 'content' } .= $line . "\n";
148 }
149 }
150  
151 #
152 # If we're still in the header at the end of the file
153 # then something has gone wrong.
154 #
155 8 ▶ if ($header)
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 8 8
156 {
157 0 print "WARNING: No header found in $filename\n";
158 }
159  
160 8 close($handle);
161 }
162  
163  
164  
165  
166 =head2 content
167  
168 Return the body of the page.
169  
170 Here we perform the textile/markdown expansion if possible via the use
171 plugins loaded by L<Templer::Plugin::Factory>.
172  
173 =cut
174  
175 sub content
176 3 ▶{
Subroutine content: called CC=7 SCAR=29.8
Pod documented
177 3 my ( $self, $data ) = (@_);
178  
179 #
180 # The content we read from the page.
181 #
182 3 my $content = $self->{ 'content' };
183 3 ▶ my $format = $self->{ 'format' } || $data->{ 'format' } || undef;
Condition40%2/5
$self->{'format'} || $data->{'format'} || undef
l!l
03
$self->{"format"} or $data->{"format"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->{'format'} || $data->{'format'} || undef
$self->{"format"} $data->{"format"}
Truth table33%1/3
A or B
A= $self->{"format"}B= $data->{"format"}
ABresult
1X1
011
000
184  
185 #
186 # Do we have a formatter plugin for this type?
187 #
188 # Many formatters might be specified
189 #
190 3 ▶ if ($format)
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 0 3 3
191 {
192  
193 #
194 # The plugin-factory.
195 #
196 0 my $factory = Templer::Plugin::Factory->new();
197  
198 #
199 # For each formatter.
200 #
201 0 foreach my $fmt ( split( /,/, $format ) )
202 {
203 0 $fmt =~ s/^\s+|\s+$//g;
204 0 ▶ next unless ($fmt);
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt 0 0 0
205  
206 0 my $helper = $factory->formatter($fmt);
207 0 ▶ $content = $helper->format( $content, $data ) if ($helper);
Branch: 1 branch
BranchTrueFalseTotal
if $helper 0 0 0
208 }
209 }
210 3 return $content;
211 }
212  
213  
214 =head2 field
215  
216 Retrieve a field from the header of the page.
217  
218 In the following example file "foo", "bar" and "title" are fields:
219  
220 Foo: Testing ..
221 Bar: file_glob( "*.gif" )
222 Title: This is my page title.
223 -----
224 <p>This is my page content ..</p>
225  
226 =cut
227  
228 sub field
229 25 ▶{
Subroutine field: called CC=1 SCAR=0.0
Pod documented
230 25 my ( $self, $field ) = (@_);
231 25 return ( $self->{ $field } );
232 }
233  
234  
235  
236 =head2 fields
237  
238 Return all known fields/values from the page.
239  
240 =cut
241  
242 sub fields
243 8 ▶{
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
244 8 my ($self) = (@_);
245  
246 8 return (%$self);
247 }
248  
249  
250 =head2 source
251  
252 Return the filename we were built from. This is the value passed
253 in the constructor.
254  
255 =cut
256  
257 sub source
258 15 ▶{
Subroutine source: called CC=1 SCAR=0.0
Pod documented
259 15 my ($self) = (@_);
260 15 $self->field("file");
261 }
262  
263  
264  
265 =head2 layout
266  
267 Return the layout-template to use for this page, if one has been set.
268  
269 =cut
270  
271 sub layout
272 3 ▶{
Subroutine layout: called CC=1 SCAR=0.0
Pod documented
273 3 my ($self) = (@_);
274 3 $self->field("layout");
275 }
276  
277  
278 =head2 dependencies
279  
280 Return the dependencies of the current page.
281  
282 =cut
283  
284 sub dependencies
285 1 ▶{
Subroutine dependencies: called CC=2 SCAR=8.1
Pod documented
286 1 my ($self) = (@_);
287  
288 0 ▶ $self->{ 'dependencies' } ? @{ $self->{ 'dependencies' } } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dependencies'} ? : 0 1 1
289 }
290  
291  
292 =head2 add_dependency
293  
294 Add a dependency to the current page. This is used so that the file-inclusion
295 plugin can add such a thing.
296  
297 =cut
298  
299 sub add_dependency
300 9 ▶{
Subroutine add_dependency: called CC=1 SCAR=0.0
Pod documented
301 9 my ( $self, $file ) = (@_);
302 9 push( @{ $self->{ 'dependencies' } }, $file );
303 }
304  
305  
306 11 1;
« lib/Templer/Site/New.pm ↑ Summary blib/lib/Templer/Timer.pm »