blib/lib/HTML/DWT/Simple.pm

filter: Statementstmt 25.0% 21 / 84 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 27.2% 3 / 11 Podpod 100% 3 / 3 totaltotal 21.6% 27 / 125 CC 17 SCAR 33.5
CC
17
Cov
19%
new
56.0
param
42.0
_load
20.0
CRAP 28.6
↑ Summary
1 package HTML::DWT::Simple;
2 #############################################################
3 # HTML::DWT::Simple
4 # Whyte.Wolf DreamWeaver HTML Template Module (Simple)
5 # Version 1.02
6 #
7 # Copyright (c) 2002 by S.D. Campbell <whytwolf@spots.ab.ca>
8 #
9 # Created 13 March 2002, Modified 05 April 2002
10 #
11 # A perl module designed to parse a simple HTML template file
12 # generated by Macromedia Dreamweaver and replace fields in the
13 # template with values from a CGI script.
14 #
15 #############################################################
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #############################################################
30
31 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
32
33 1 @ISA = qw(Exporter);
34 1 @EXPORT_OK = qw(output param);
35
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use vars qw($errmsg $VERSION @ISA @EXPORT_OK
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 $NOTICE %DWT_FIELDS %DWT_VALUES);
39
40 1 $VERSION = '1.02';
41
42 1 $NOTICE = "\n<!-- Generated using HTML::DWT::Simple version " . $VERSION . " -->\t\t\n";
43 1 $NOTICE .= "<!-- HTML::DWT::Simple Copyright (c) 2002 Sean Campbell -->\t\n";
44 1 $NOTICE .= "<!-- HTML::DWT::Simple is licenced under the GNU General Public License -->\t\n";
45 1 $NOTICE .= "<!-- You can find HTML::DWT::Simple at http://www.spots.ab.ca/~whytwolf -->\t\n";
46 1 $NOTICE .= "<!-- or by going to http://www.cpan.org -->\n";
47
48 1 %DWT_FIELDS = ();
49 1 %DWT_VALUES = ();
50
51 1 $errmsg = "";
52
53 #############################################################
54 # new
55 #
56 # The constructor for the class. Requires a HTML Template
57 # filename. Returns a reference to the new object or undef
58 # on error. Errors can be retrieved from
59 # $HTML::DWT::Simple::errmsg.
60
61 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
62 0 my $class = shift;
63 0 my %params = @_;
64
65
66 0 my $self = {};
67
68 0 ▶ if (!$params{filename}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"filename"}) 0 0 0
69 0 $params{filename} = $_[0];
70 }
71
72 0 ▶ if (exists($params{associate})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'associate'}) { } 0 0 0
73 0 ▶ if (ref($params{associate}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"associate"} ne "ARRAY") 0 0 0
74 0 $$self{associate} = [ $params{associate} ];
75 }
76 0 $$self{associate} = $params{associate};
77 } else {
78 0 $$self{associate} = undef;
79 }
80
81
82 0 $$self{filename} = $params{filename};
83 0 $$self{template} = '';
84
85
86 0 ▶ unless(open(TEMPLATE_FILE, $$self{filename})){
Branch: 1 branch
BranchTrueFalseTotal
unless (open TEMPLATE_FILE, $self->{"filename"}) 0 0 0
87 0 $errmsg = "HTML::DWT::Simple--Template File $$self{filename} not opened: $!\n";
88 0 return undef;
89 }
90
91 0 ▶ while(<TEMPLATE_FILE>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline TEMPLATE_FILE)) 0 0 0
92 0 $$self{template} .= $_;
93 }
94
95 0 $$self{html} = $$self{template};
96 0 $$self{html} =~ s/<html>/_beginTemplate($$self{filename})/ie;
97 0 $$self{html} =~ s/<\/html>/_endTemplate()/ie;
98 0 $$self{html} =~ s/<!--\s*#BeginEditable\s*\"(\w*)\"\s*-->?/_quoteReplace($1)/ieg;
99
100 0 bless $self, $class;
101
102 0 ▶ if (exists($$self{associate})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"associate"}) 0 0 0
103 0 $self->_load();
104 }
105
106 0 return $self;
107 }
108
109
110 #############################################################
111 # output
112 #
113 # Returns the substituted HTML as generated by fill() or
114 # param(). For compatibility with HTML::Template.
115
116 0 ▶sub output {
Subroutine output: not called CC=3 SCAR=24.8
Pod documented
117 0 my $self = shift;
118 0 my %params = @_;
119
120
121 0 foreach my $key (keys %DWT_VALUES) {
122 0 $$self{html}=~s/<!--\s*#BeginEditable\s*($key)\s*-->?(.*?)<?!--\s*#EndEditable\s*-->/_keyReplace($DWT_VALUES{$key},$1)/iegs;
123 }
124
125
126 0 ▶ if ($params{'print_to'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'print_to'}) { } 0 0 0
127 0 my $print_to = $params{'print_to'};
128 0 print $print_to $$self{html};
129 0 return undef;
130 } else {
131 0 return $$self{html};
132 }
133 }
134
135 #############################################################
136 # param
137 #
138 # Take a hash of one or more key/value pairs and substitutes
139 # the HTML value in the key's spot in the template. For
140 # compatibility with HTML::Template.
141
142 0 ▶sub param {
Subroutine param: not called CC=6 SCAR=37.4
Pod documented
143
144 0 my $self = shift;
145
146 0 ▶ if (scalar(@_) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 0 0
elsif (scalar @_ == 1) { } 0 0 0
147 0 return keys %DWT_FIELDS;
148 } elsif (scalar(@_) == 1){
149 0 my $field = shift;
150 0 return $DWT_VALUES{$field};
151 } else {
152 0 my %params = @_;
153 0 foreach my $key (keys %params) {
154 0 ▶ if ($key eq 'doctitle' && !($params{$key}=~/<title>(.*?)<\/title>/i)){
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'doctitle' and not $params{$key} =~ m[<title>(.*?)</title>]i) { } 0 0 0
Condition0%0/3
$key eq "doctitle" and not $params{$key} =~ m[<title>(.*?)</title>]i
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "doctitle" and not $params{$key} =~ m[<title>(.*?)</title>]i
$key eq "doctitle" not $params{$key} =~ m[<title>(.*?)</title>]i
Truth table0%0/3
A and not(B)
A= $key eq "doctitle"B= not $params{$key} =~ m[<title>(.*?)</title>]i
ABresult
0X0
100
111
155 0 $DWT_VALUES{'doctitle'} = "<title>" . $params{$key} . "</title>";
156 } else {
157 0 $DWT_VALUES{$key} = $params{$key};
158 }
159 }
160 }
161
162 }
163
164
165 #############################################################
166 # _keyReplace
167 #
168 # An internal subroutine that does the actual key/value
169 # replacement. Takes the contents scalar and returns a
170 # HTML string.
171
172 0 ▶sub _keyReplace {
Subroutine _keyReplace: not called CC=1 SCAR=6.9
173 0 my $cont = shift;
174 0 my $key = shift;
175
176 0 return "<!-- \#BeginEditable \"$key\" -->\n" . $cont . "\n<!-- \#EndEditable -->\n";
177 }
178
179 #############################################################
180 # _beginTemplate
181 #
182 # Returns the begin template string and file name back into
183 # the parsed HTML.
184
185 0 ▶sub _beginTemplate {
Subroutine _beginTemplate: not called CC=1 SCAR=6.9
186 0 my $filename = shift;
187 0 return "<html>\n<!-- \#BeginTemplate \"$filename\" -->\n" . $NOTICE;
188 }
189
190 #############################################################
191 # _endTemplate
192 #
193 # Returns the end template string back into the parsed HTML.
194
195 0 ▶sub _endTemplate {
Subroutine _endTemplate: not called CC=1 SCAR=6.9
196 0 return "<!-- \#EndTemplate -->\n</html>";
197 }
198
199 #############################################################
200 # _quoteReplace
201 #
202 # An internal subroutine that removes quotes from around
203 # the editable region name (fixes recursive loop bug).
204 # As of version 2.06 also builds %DWT_FIELDS and %DWT_VALUES
205
206 0 ▶sub _quoteReplace {
Subroutine _quoteReplace: not called CC=1 SCAR=6.9
207 0 my $key = shift;
208 0 $DWT_FIELDS{$key} = 'VAR';
209 0 $DWT_VALUES{$key} = undef;
210
211 0 return "<!-- \#BeginEditable $key -->";
212 }
213
214 #############################################################
215 # _load
216 #
217 # Loads the parameters from external sources
218
219 0 ▶sub _load {
Subroutine _load: not called CC=4 SCAR=30.0
220
221 0 my $self = shift;
222
223 0 ▶ if ($$self{associate}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"associate"}) 0 0 0
224 0 foreach my $query ($$self{associate}){
225 0 foreach my $param ($query->param) {
226 0 $self->param($param => $query->param($param));
227 }
228 }
229 }
230
231 }
232
233 1 1;
234 __END__
↑ Summary