blib/lib/HTML/DWT.pm untested

filter: Statementstmt 0.0% 0 / 233 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 363 CC 38 SCAR 45.8
CC
38
Cov
0%
new
306.0
export
42.0
_lbiInclude
42.0
CRAP 97.1
1 package HTML::DWT;
2 #############################################################
3 # Version 2.08
4 #
5 # Copyright (c) 2002 by S.D. Campbell <whytwolf@spots.ab.ca>
6 #
7 # Created 03 March 2000; Revised 04 March 2002 by SDC
8 #
9 # A perl module designed to parse a simple HTML template file
10 # generated by Macromedia Dreamweaver and replace fields in the
11 # template with values from a CGI script.
12 #
13 #############################################################
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #############################################################
28
29 use Exporter;
30 use Carp;
31 use File::Find;
32 use File::Basename;
33 use XML::Simple;
34
35 @ISA = qw(Exporter);
36 @EXPORT = qw(fillTemplate fill export);
37 @EXPORT_OK = qw(output param query clear_params);
38 %EXPORT_TAGS = (
39 Template => [qw(output param query clear_params)],
40 );
41
42 use strict;
43 use vars qw($errmsg $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS
44 $NOTICE %DWT_FIELDS %DWT_VALUES $fname $filepath @search);
45
46 $VERSION = '2.08';
47
48 $NOTICE = "\n<!-- Generated using HTML::DWT version " . $VERSION . " -->\t\t\n";
49 $NOTICE .= "<!-- HTML::DWT Copyright (c) 2001,2002 Sean Campbell -->\t\n";
50 $NOTICE .= "<!-- HTML::DWT is licenced under the GNU General Public License -->\t\n";
51 $NOTICE .= "<!-- You can find HTML::DWT at http://www.spots.ab.ca/~whytwolf -->\t\n";
52 $NOTICE .= "<!-- or by going to http://www.cpan.org -->\n";
53
54 %DWT_FIELDS = ();
55 %DWT_VALUES = ();
56
57 $errmsg = "";
58 $fname = '';
59 $filepath = '';
60 @search = ();
61
62 #############################################################
63 # new
64 #
65 # The constructor for the class. Requires a HTML Template
66 # filename. Returns a reference to the new object or undef
67 # on error. Errors can be retrieved from $HTML:DWT:errmsg.
68
69 sub new {
70 my $class = shift;
71 my %params = @_;
72
73
74 my $self = {};
75
76 if (!$params{filename}){
77 $params{filename} = $_[0];
78 }
79
80 $$self{filename} = $params{filename};
81 $$self{option} = $params{option};
82 $$self{template} = '';
83 $$self{filter} = $params{filter};
84
85 if (exists($params{case_sensitive})){
86 $$self{case_sensitive} = $params{case_sensitive};
87 } else {
88 $$self{case_sensitive} = 0;
89 }
90
91 if (exists($params{no_includes})){
92 $$self{no_includes} = $params{no_includes};
93 } else {
94 $$self{no_includes} = 0;
95 }
96
97 if (exists($params{associate})){
98 if (ref($params{associate}) ne 'ARRAY') {
99 $$self{associate} = [ $params{associate} ];
100 }
101 $$self{associate} = $params{associate};
102 } else {
103 $$self{associate} = undef;
104 }
105
106 if (exists($params{xml})){
107 $$self{xml} = $params{xml};
108 } else {
109 $$self{xml} = undef;
110 }
111
112
113 if (exists($params{path})){
114 if (ref($params{path}) ne 'ARRAY') {
115 $$self{path} = [ $params{path} ];
116 }
117 $$self{path} = $params{path};
118 } else {
119 $$self{path} = './';
120 }
121
122 foreach my $path ($$self{path}){
123 push @search, $path;
124 }
125
126 if ($ENV{'HTML_TEMPLATE_ROOT'}) {
127 my $temproot = $ENV{'HTML_TEMPLATE_ROOT'};
128 push @search, $temproot;
129 }
130
131 if ($ENV{'DOCUMENT_ROOT'}) {
132 my $docroot = $ENV{'DOCUMENT_ROOT'};
133 push @search, $docroot;
134 }
135
136
137 if (substr($$self{filename}, 0, 1) ne '/') {
138 $fname = $$self{filename};
139 foreach my $dir (@search){
140 find(\&_wanted, $dir);
141 }
142 if (!$filepath) {
143 $filepath = $$self{filename};
144 }
145 } elsif (substr($$self{filename}, 0, 10) eq '/Templates') {
146 my ($name, $path, $suffix) = fileparse($$self{filename}, '\.dwt');
147 $fname = $name . $suffix;
148 foreach my $dir (@search){
149 find(\&_wanted, $dir);
150 }
151 if (!$filepath) {
152 $filepath = $$self{filename};
153 }
154 } else {
155 $filepath = $$self{filename};
156 }
157
158 unless(open(TEMPLATE_FILE, $filepath)){
159 $errmsg = "HTML::DWT--Template File $filepath not opened: $!\n";
160 return undef;
161 }
162
163 while(<TEMPLATE_FILE>){
164 $$self{template} .= $_;
165 }
166
167 $$self{html} = $$self{template};
168 $$self{html} =~ s/<html>/_beginTemplate($$self{filename})/ie;
169 $$self{html} =~ s/<\/html>/_endTemplate()/ie;
170 $$self{html} =~ s/<!--\s*#BeginEditable\s*\"(\w*)\"\s*-->?/_quoteReplace($1)/ieg;
171
172 bless $self, $class;
173
174 if (exists($$self{associate}) || exists($$self{xml})){
175 $self->_load();
176 }
177
178 return $self;
179 }
180
181 #############################################################
182 # clear_params
183 #
184 # A subroutine which clears the values of all template
185 # parameters.
186
187 sub clear_params {
188 my $self = shift;
189 foreach my $key (keys %DWT_VALUES){
190 $DWT_VALUES{$key} = undef;
191 }
192 }
193
194
195 #############################################################
196 # fill
197 #
198 # A subroutine for parsing and replacing key values in an
199 # HTML Template. Takes a reference to a hash containing the
200 # key/value pairs. Returns the parsed HTML. Calls param()
201 # for actual substitution as of version 2.05.
202
203 sub fill {
204 my $self = shift;
205 my $cont = shift;
206
207 $self->param(%$cont);
208 return $self->output();
209 }
210
211 #############################################################
212 # fillTemplate
213 #
214 # Calls fill() for backwards compatibility with earlier versions.
215
216 sub fillTemplate {
217
218 my $self = shift;
219 my $cont = shift;
220
221 $self->fill($cont);
222
223 }
224
225 #############################################################
226 # output
227 #
228 # Returns the substituted HTML as generated by fill() or
229 # param(). For compatibility with HTML::Template.
230
231 sub output {
232 my $self = shift;
233 my %params = @_;
234
235 if ($$self{case_sensitive} == 1){
236 foreach my $key (keys %DWT_VALUES) {
237 $$self{html}=~s/<!--\s*#BeginEditable\s*($key)\s*-->?(.*?)<?!--\s*#EndEditable\s*-->/_keyReplace($DWT_VALUES{$key},$1)/egs;
238 }
239 } else {
240 foreach my $key (keys %DWT_VALUES) {
241 $$self{html}=~s/<!--\s*#BeginEditable\s*($key)\s*-->?(.*?)<?!--\s*#EndEditable\s*-->/_keyReplace($DWT_VALUES{$key},$1)/iegs;
242 }
243 }
244
245 if($$self{no_includes} == 0) {
246 $$self{html} =~ s/<!--\s*#BeginLibraryItem\s*\"(.*?)\"\s*-->?/_lbiquoteReplace($1)/ieg;
247 $$self{html} =~ s/<!--\s*#BeginLibraryItem\s*(.*?)\s*-->?(.*?)<?!--\s*#EndLibraryItem\s*-->/_lbiInclude($1)/iegs;
248 }
249
250 if ($params{'print_to'}){
251 my $print_to = $params{'print_to'};
252 print $print_to $$self{html};
253 return undef;
254 } else {
255 return $$self{html};
256 }
257 }
258
259 #############################################################
260 # param
261 #
262 # Take a hash of one or more key/value pairs and substitutes
263 # the HTML value in the key's spot in the template. For
264 # compatibility with HTML::Template.
265
266 sub param {
267
268 my $self = shift;
269
270 if (scalar(@_) == 0) {
271 return keys %DWT_FIELDS;
272 } elsif (scalar(@_) == 1){
273 my $field = shift;
274 return $DWT_VALUES{$field};
275 } else {
276 my %params = @_;
277 foreach my $key (keys %params) {
278 if ($key eq 'doctitle' && !($params{$key}=~/^\s*<title>(.*?)<\/title>/is)){
279 $DWT_VALUES{'doctitle'} = "<title>" . $params{$key} . "</title>";
280 } else {
281 $DWT_VALUES{$key} = $params{$key};
282 }
283 }
284 }
285
286 }
287
288 #############################################################
289 # query
290 #
291 # Allows for querying of template parameters. For
292 # compatibility with HTML::Template.
293
294 sub query {
295
296 my $self = shift;
297
298 if (scalar(@_) == 0) {
299 return keys %DWT_FIELDS;
300 } elsif (scalar(@_) == 1){
301 my $field = shift;
302 return $DWT_FIELDS{$field};
303 } else {
304 my %params = @_;
305 my $cmd = shift;
306 my $field = shift;
307
308 if ($cmd eq 'name') {
309 return $DWT_FIELDS{$field};
310 } else {
311 return undef;
312 }
313 }
314
315 }
316
317 #############################################################
318 # export
319 #
320 # Allows for export of field values to Dreamweaver XML format
321 # or another standardized XML format (see Dreamweaver 4
322 # documents for more details).
323
324 sub export {
325
326 my $self = shift;
327 my %params = @_;
328 my $type = $params{'type'};
329 my $output = $params{'output'};
330 my $print_to = $params{'print_to'};
331 my $xmlcont = '';
332 my $filename = '';
333
334 $self->output();
335
336 if (!$output) {
337 $output = 'xml';
338 }
339
340 if ($type eq 'er') {
341 $xmlcont = _xmler($$self{filename});
342 } else {
343 $xmlcont = _xmldw($$self{filename});
344 }
345
346 if ($output eq 'file') {
347 my $filename = $params{'filename'};
348 unless(open(XML_FILE,">$filename")) {
349 $errmsg = "HTML::DWT--XML File $filename not opened: $!\n";
350 return undef;
351 }
352 print XML_FILE $xmlcont;
353 close(XML_FILE);
354 return $filename;
355 } elsif ($output eq 'FH') {
356 print $print_to "Content-type: text/xml\n\n" . $xmlcont;
357 return undef;
358 } else {
359 return $xmlcont;
360 }
361
362 }
363
364 #############################################################
365 # _keyReplace
366 #
367 # An internal subroutine that does the actual key/value
368 # replacement. Takes the contents scalar and returns a
369 # HTML string.
370
371 sub _keyReplace {
372 my $cont = shift;
373 my $key = shift;
374
375 return "<!-- \#BeginEditable \"$key\" -->\n" . $cont . "\n<!-- \#EndEditable -->\n";
376 }
377
378 #############################################################
379 # _beginTemplate
380 #
381 # Returns the begin template string and file name back into
382 # the parsed HTML.
383
384 sub _beginTemplate {
385 my $filename = shift;
386 return "<html>\n<!-- \#BeginTemplate \"$filename\" -->\n" . $NOTICE;
387 }
388
389 #############################################################
390 # _endTemplate
391 #
392 # Returns the end template string back into the parsed HTML.
393
394 sub _endTemplate {
395 return "<!-- \#EndTemplate -->\n</html>";
396 }
397
398 #############################################################
399 # _quoteReplace
400 #
401 # An internal subroutine that removes quotes from around
402 # the editable region name (fixes recursive loop bug).
403 # As of version 2.06 also builds %DWT_FIELDS and %DWT_VALUES
404
405 sub _quoteReplace {
406 my $key = shift;
407 $DWT_FIELDS{$key} = 'VAR';
408 $DWT_VALUES{$key} = undef;
409
410 return "<!-- \#BeginEditable $key -->";
411 }
412
413 #############################################################
414 # _lbiquoteReplace
415 #
416 # An internal subroutine that removes quotes from around
417 # the library file name
418
419 sub _lbiquoteReplace {
420 my $key = shift;
421
422 return "<!-- \#BeginLibraryItem $key -->";
423 }
424
425 #############################################################
426 # _lbiInclude
427 #
428 # An internal subroutine that opens a Dreamweaver .lbi file
429 # and returns its contents.
430
431 sub _lbiInclude {
432 my $file = shift;
433
434 if (substr($file, 0, 1) ne '/') {
435 $fname = $file;
436 foreach my $dir (@search){
437 find(\&_wanted, $dir);
438 }
439 if (!$filepath) {
440 $filepath = $file;
441 }
442 } elsif (substr($file, 0, 8) eq '/Library') {
443 my ($name, $path, $suffix) = fileparse($file, '\.lbi');
444 $fname = $name . $suffix;
445 foreach my $dir (@search){
446 find(\&_wanted, $dir);
447 }
448 if (!$filepath) {
449 $filepath = $file;
450 }
451 } else {
452 $filepath = $file;
453 }
454
455
456 my $lbi = "<!-- #BeginLibraryItem \"$file\" -->\n";
457
458 unless(open(LBI_FILE, $filepath)){
459 $errmsg = "HTML::DWT--Included Library File $filepath not opened: $!\n";
460 return $errmsg;
461 }
462
463 while(<LBI_FILE>){
464 $lbi .= $_;
465 }
466
467 $lbi .= "\n<!-- #EndLibraryItem -->";
468
469 return $lbi;
470 }
471
472
473 #############################################################
474 # _xmldw
475 #
476 # An internal subroutine that generates a Dreamweaver XML
477 # document for export.
478
479 sub _xmldw {
480 my $filename = shift;
481 my $xmlcont = "<?xml version=\"1.0\"?>\n<templateItems template=\"$filename\">\n";
482
483 foreach my $key (sort keys %DWT_FIELDS){
484 $xmlcont .= "<item name=\"$key\"><![CDATA[$DWT_VALUES{$key}]]></item>\n";
485 }
486
487 $xmlcont .= "</templateItems>\n";
488
489 return $xmlcont;
490 }
491
492 #############################################################
493 # _xmler
494 #
495 # An internal subroutine that generates a XML export document
496
497 sub _xmler {
498 my $filename = shift;
499 my ($name, $path, $suffix) = fileparse($filename, '\.dwt');
500
501 my $xmlcont = "<?xml version=\"1.0\"?>\n<$name template=\"$filename\">\n";
502
503 foreach my $key (keys %DWT_VALUES){
504 $xmlcont .= "<$key><![CDATA[$DWT_VALUES{$key}]]></$key>";
505 }
506
507 $xmlcont .= "</$name>";
508
509 return $xmlcont;
510 }
511
512 #############################################################
513 # _wanted
514 #
515 # Finds the path to a file if it exists.
516
517 sub _wanted {
518
519 /$fname$/ or return;
520 $filepath = $File::Find::name;
521
522 }
523
524 #############################################################
525 # _load
526 #
527 # Loads the parameters from external sources
528
529 sub _load {
530
531 my $self = shift;
532
533 if ($$self{associate}){
534 foreach my $query ($$self{associate}){
535 foreach my $param ($query->param) {
536 $self->param($param => $query->param($param));
537 }
538 }
539 }
540
541 if ($$self{xml}) {
542 my $xs = new XML::Simple(keeproot => 1);
543 my $ref = $xs->XMLin($$self{xml});
544 my $items = $ref->{templateItems}->{item};
545 foreach my $item (keys %$items){
546 $self->param($item => $ref->{templateItems}->{item}->{$item}->{content});
547 }
548
549 }
550
551 }
552
553
554 1;
555 __END__