lib/App/Followme/Template.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 82.1% 23 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod 33.3% 2 / 6 totaltotal 90.2% 130 / 144 CC 16 SCAR 15.0
CC
16
Cov
93%
parse_code
10.1
parse_command
4.0
compile
3.0
CRAP 4.5
« lib/App/Followme/ConfiguredObject.pm ↑ Summary lib/App/Followme/FormatPage.pm »
1 package App::Followme::Template;
2  
3 12 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use App::Followme::Web;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use base qw(App::Followme::ConfiguredObject);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 our $VERSION = "2.03";
17  
18 12 ▶use constant COMMAND_START => '<!-- ';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use constant COMMAND_END => '-->';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #----------------------------------------------------------------------
22 # Compile a template into a subroutine which when called fills itself
23  
24 29 ▶sub compile {
Subroutine compile: called CC=3 SCAR=11.0
Pod documented
25 29 my ($pkg, $template) = @_;
26  
27 29 ▶ my $self = ref $pkg ? $pkg : $pkg->new();
Branch: 1 branch
BranchTrueFalseTotal
ref $pkg ? : 27 2 29
28 29 my @lines = split(/\n/, $template);
29  
30 29 my $start = <<'EOQ';
31 sub {
32 my ($meta, $item, $loop) = @_;
33 my @text;
34 my @loop;
35 @loop = @$loop if defined $loop;
36 EOQ
37  
38 29 my @mid = $self->parse_code(\@lines);
39  
40 29 my $end .= <<'EOQ';
41 return join('', @text);
42 }
43 EOQ
44  
45 29 my $code = join("\n", $start, @mid, $end);
46 29 my $sub = eval ($code);
47 29 ▶ croak $@ unless $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 29 29
48  
49 29 return $sub;
50 }
51  
52 #----------------------------------------------------------------------
53 # Replace variable references with hashlist fetches
54  
55 740 ▶sub encode_expression {
Subroutine encode_expression: called CC=2 SCAR=7.1
Pod undocumented
56 740 my ($self, $value) = @_;
57  
58 740 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 740 0 740
59 740 my $pre = '{$meta->build(\'';
60 740 my $post = '\', $item, \@loop)}';
61 740 $value =~ s/(?<!\\)([\$\@])(\w+)/$1$pre$1$2$post/g;
62  
63 } else {
64 0 $value = '';
65 }
66  
67 740 return $value;
68 }
69  
70 #----------------------------------------------------------------------
71 # Get the translation of a template command
72  
73 191 ▶sub get_command {
Subroutine get_command: called CC=1 SCAR=0.0
Pod undocumented
74 191 my ($self, $cmd) = @_;
75  
76 191 my $commands = {
77 do => '%%;',
78 for => 'if (%%) { foreach my $item (my @loop = (%%)) {',
79 endfor => '}}',
80 if => 'if (%%) { do {',
81 elsif => '}} elsif (%%) { do {',
82 else => '}} else { do {',
83 endif => '}}',
84 };
85  
86 191 return $commands->{$cmd};
87 }
88  
89 #----------------------------------------------------------------------
90 # Parse the templace source
91  
92 50 ▶sub parse_code {
Subroutine parse_code: called CC=10 SCAR=23.2
Pod undocumented
93 50 my ($self, $lines, $command) = @_;
94  
95 50 my @code;
96 my @stash;
97  
98 50 ▶ while (defined (my $line = shift @$lines)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = shift @$lines)) 740 29 769
99 740 my ($cmd, $cmdline) = $self->parse_command($line);
100  
101 740 ▶ if (defined $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd) { } 45 695 740
102 45 ▶ if (@stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stash) 42 3 45
103 42 push(@code, 'push @text, <<"EOQ";', @stash, 'EOQ');
104 42 @stash = ();
105 }
106 45 push(@code, $cmdline);
107  
108 45 ▶ if (substr($cmd, 0, 3) eq 'end') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($cmd, 0, 3) eq 'end') { } 21 24 45
elsif ($self->get_command("end$cmd")) { } 21 3 24
109 21 my $startcmd = substr($cmd, 3);
110 21 ▶ die "Mismatched block end ($command/$cmd)"
Branch: 1 branch
BranchTrueFalseTotal
if defined $startcmd and $startcmd ne $command 0 21 21
Condition33%1/3
defined $startcmd and $startcmd ne $command
!ll&&!rl&&r
0210
MC/DC0%0/2
defined $startcmd and $startcmd ne $command
defined $startcmd $startcmd ne $command
Truth table33%1/3
A and B
A= defined $startcmdB= $startcmd ne $command
ABresult
0X0
100
111
111 if defined $startcmd && $startcmd ne $command;
112 21 return @code;
113  
114 } elsif ($self->get_command("end$cmd")) {
115 21 push(@code, $self->parse_code($lines, $cmd));
116 }
117  
118 } else {
119 695 push(@stash, $self->encode_expression($line));
120 }
121 }
122  
123 29 ▶ die "Missing end (end$command)" if $command;
Branch: 1 branch
BranchTrueFalseTotal
if $command 0 29 29
124 29 ▶ push(@code, 'push @text, <<"EOQ";', @stash, 'EOQ') if @stash;
Branch: 1 branch
BranchTrueFalseTotal
if @stash 27 2 29
125  
126 29 return @code;
127 }
128  
129 #----------------------------------------------------------------------
130 # Parse a command and its argument
131  
132 740 ▶sub parse_command {
Subroutine parse_command: called CC=4 SCAR=13.9
Pod undocumented
133 740 my ($self, $line) = @_;
134  
135 740 my $command_start_pattern = COMMAND_START;
136 740 ▶ return unless $line =~ s/$command_start_pattern//;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ s/$command_start_pattern// 573 167 740
137  
138 167 my $command_end_pattern = COMMAND_END;
139 167 $line =~ s/$command_end_pattern//;
140  
141 167 my ($cmd, $arg) = split(' ', $line, 2);
142 167 ▶ $arg = '' unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 167 167
143  
144 167 my $cmdline = $self->get_command($cmd);
145 167 ▶ return unless $cmdline;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmdline 122 45 167
146  
147 45 $arg = $self->encode_expression($arg);
148 45 $cmdline =~ s/%%/$arg/g;
149  
150 45 return ($cmd, $cmdline);
151 }
152  
153 #----------------------------------------------------------------------
154 # Set the regular expression patterns used to match a command
155  
156 46 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
157 46 my ($self) = @_;
158  
159 46 $self->{command_start_pattern} = '^\s*' . quotemeta(COMMAND_START);
160 46 $self->{command_end_pattern} = '\s*' . quotemeta(COMMAND_END) . '\s*$';
161  
162 46 return;
163 }
164  
165 12 1;
166  
167 =pod
168  
169 =encoding utf-8
170  
171 =head1 NAME
172  
173 App::Followme::Template - Handle templates and prototype files
174  
175 =head1 SYNOPSIS
176  
177 use App::Followme::Template;
178 my $template = App::Followme::Template->new;
179 my $render = $template->compile($template_file);
180 my $output = $render->($hash);
181  
182 =head1 DESCRIPTION
183  
184 This module contains the methods that perform template handling. A Template is a
185 file containing commands and variables for making a web page. First, the
186 template is compiled into a subroutine and then the subroutine is called with a
187 hash as an argument to fill in the variables and produce a web
188 page.
189  
190 =head1 METHODS
191  
192 This module has one public method:
193  
194 =over 4
195  
196 =item $sub = $self->compile($template_file);
197  
198 Compile a template and return the compiled subroutine. A template is a file
199 containing commands and variables that describe how data is to be represented.
200 The method returns a subroutine reference, which when called with a metadata
201 object, returns a web page containing the fields from the metadata substituted
202 into variables in the template. Variables in the template are preceded by Perl
203 sigils, so that a link would look like:
204  
205 <li><a href="$url">$title</a></li>
206  
207 =back
208  
209 =head1 TEMPLATE SYNTAX
210  
211 Templates support the basic control structures in Perl: "for" loops and
212 "if-else" blocks. Creating output is a two step process. First you generate a
213 subroutine from one or more templates, then you call the subroutine with your
214 data to generate the output.
215  
216 The template format is line oriented. Commands are enclosed in html comments
217 (<!-- -->). A command may be preceded by white space. If a command is a block
218 command, it is terminated by the word "end" followed by the command name. For
219 example, the "for" command is terminated by an "endfor" command and the "if"
220 command by an "endif" command.
221  
222 All lines may contain variables. As in Perl, variables are a sigil character
223 ('$' or '@') followed by one or more word characters. For example, C<$name> or
224 C<@names>. To indicate a literal character instead of a variable, precede the
225 sigil with a backslash. When you run the subroutine that this module generates,
226 you pass it a metadata object. The subroutine replaces variables in the template
227 with the value in the field built by the metadata object.
228  
229 If the first non-white characters on a line are the command start string, the
230 line is interpreted as a command. The command name continues up to the first
231 white space character. The text following the initial span of white space is the
232 command argument. The argument continues up to the command end string.
233  
234 Variables in the template have the same format as ordinary Perl variables,
235 a string of word characters starting with a sigil character. for example,
236  
237 $body @files
238  
239 are examples of variables. The following commands are supported in templates:
240  
241 =over 4
242  
243 =item do
244  
245 The remainder of the line is interpreted as Perl code.
246  
247 =item for
248  
249 Expand the text between the "for" and "endfor" commands several times. The
250 argument to the "for" command should be an expression evaluating to a list. The
251 code will expand the text in the for block once for each element in the list.
252  
253 <ul>
254 <!-- for @files -->
255 <li><a href="$url">$title</a></li>
256 <!-- endfor -->
257 </ul>
258  
259 =item if
260  
261 The text until the matching C<endif> is included only if the expression in the
262 "if" command is true. If false, the text is skipped.
263  
264 <div class="column">
265 <!-- for @files -->
266 <!-- if $count % 20 == 0 -->
267 </div>
268 <div class="column">
269 <!-- endif -->
270 $title<br />
271 <!-- endfor -->
272 </div>
273  
274 =item else
275  
276 The "if" and "for" commands can contain an C<else>. The text before the "else"
277 is included if the expression in the enclosing command is true and the
278 text after the "else" is included if the "if" command is false or the "for"
279 command does not execute. You can also place an "elsif" command inside a block,
280 which includes the following text if its expression is true.
281  
282 =back
283  
284 =head1 ERRORS
285  
286 What to check when this module throws an error
287  
288 =over 4
289  
290 =item Couldn't read template
291  
292 The template is in a file and the file could not be opened. Check the filename
293 and permissions on the file. Relative filenames can cause problems and the web
294 server is probably running another account than yours.
295  
296 =item Unknown command
297  
298 Either a command was spelled incorrectly or a line that is not a command
299 begins with the command start string.
300  
301 =item Missing end
302  
303 The template contains a command for the start of a block, but
304 not the command for the end of the block. For example an "if" command
305 is missing an "endif" command.
306  
307 =item Mismatched block end
308  
309 The parser found a different end command than the begin command for the block
310 it was parsing. Either an end command is missing, or block commands are nested
311 incorrectly.
312  
313 =item Syntax error
314  
315 The expression used in a command is not valid Perl.
316  
317 =back
318  
319 =head1 LICENSE
320  
321 Copyright (C) Bernie Simon.
322  
323 This library is free software; you can redistribute it and/or modify
324 it under the same terms as Perl itself.
325  
326 =head1 AUTHOR
327  
328 Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
329  
330 =cut
« lib/App/Followme/ConfiguredObject.pm ↑ Summary lib/App/Followme/FormatPage.pm »