Toader/Render/Entry.pm untested

filter: Statementstmt 0.0% 0 / 318 Branchbran 0.0% 0 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 447 SCAR 80.0
CC
54
Cov
0%
render
210.0
new
182.0
content
90.0
CRAP 2970.0
1 package Toader::Render::Entry;
2  
3 use warnings;
4 use strict;
5 use base 'Error::Helper';
6 use Toader::Render::General;
7 use Toader::Templates;
8 use Date::Parse;
9 use Toader::pathHelper;
10 use File::Path qw(make_path);
11 use Toader::Render::Entry::Cleanup;
12  
13 =head1 NAME
14  
15 Toader::Render::Entry - This renders a Toader::Entry object.
16  
17 =head1 VERSION
18  
19 Version 0.1.1
20  
21 =cut
22  
23 our $VERSION = '0.1.1';
24  
25 =head1 SYNOPSIS
26  
27 =head1 METHODS
28  
29 =head2 new
30  
31 This initiates the object.
32  
33 =head3 args hash ref
34  
35 =head4 obj
36  
37 This is the L<Toader::Entry> object to render.
38  
39 =head4 toader
40  
41 This is the L<Toader> object to use.
42  
43 =head2 toDir
44  
45 This is the value used to get from the directory it is being
46 rendered in back to directory storing stuff for that directory. By
47 default this is '../../' as it needs to get from '.entries/'.$entryID
48 back to the directory. Or we are creating a listing of the last
49 several in the '.entries/' directory, it should be set it to '../'.
50  
51  
52 my $foo=Toader::Render::Entry->new(\%args);
53 if($foo->error){
54 warn('error: '.$foo->error.":".$foo->errorString);
55 }
56  
57 =cut
58  
59 sub new{
60 my %args;
61 if(defined($_[1])){
62 %args= %{$_[1]};
63 };
64  
65 my $self={
66 error=>undef,
67 errorString=>'',
68 perror=>undef,
69 toDir=>'../../',
70 errorExtra=>{
71 flags=>{
72 1=>'noObj',
73 2=>'noToaderObj',
74 3=>'objPerror',
75 4=>'toaderObjPerror',
76 5=>'noDirSet',
77 6=>'cleanupInitErrored',
78 7=>'cleanupErrored',
79 8=>'backendInitErrored',
80 9=>'pathhelperInitErrored',
81 11=>'noOutputDirSet',
82 12=>'outputDirDoesNotExist',
83 13=>'outputEntryDirCreationFailed',
84 14=>'outputFileDirCreationFailed',
85 15=>'generalInitErrored',
86 },
87 },
88 };
89 bless $self;
90  
91 if ( defined( $args{toDir} ) ){
92 $self->{toDir}=$args{toDir};
93 }
94  
95 #make sure we have a Toader::Entry object.
96 if ( ! defined( $args{obj} ) ){
97 $self->{perror}=1;
98 $self->{error}=1;
99 $self->{errorString}='Nothing defined for the Toader::Entry object';
100 $self->warn;
101 return $self;
102 }
103 if ( ref( $args{obj} ) ne 'Toader::Entry' ){
104 $self->{perror}=1;
105 $self->{error}=1;
106 $self->{errorString}='The specified object is not a Toader::Entry object, but a "'.
107 ref( $args{obj} ).'"';
108 $self->warn;
109 return $self;
110 }
111 $self->{obj}=$args{obj};
112  
113 #make sure the object does not have a permanent error set
114 if( ! $self->{obj}->errorblank ){
115 $self->{perror}=1;
116 $self->{error}=3;
117 $self->{errorString}='The Toader::Entry object has a permanent error set';
118 $self->warn;
119 return $self;
120 }
121  
122 #make sure a Toader object is given
123 if ( ! defined( $args{toader} ) ){
124 $self->{perror}=1;
125 $self->{error}=2;
126 $self->{errorString}='Nothing defined for the Toader object';
127 $self->warn;
128 return $self;
129 }
130 if ( ref( $args{toader} ) ne 'Toader' ){
131 $self->{perror}=1;
132 $self->{error}=2;
133 $self->{errorString}='The specified object is not a Toader object, but a "'.
134 ref( $args{toader} ).'"';
135 $self->warn;
136 return $self;
137 }
138 $self->{toader}=$args{toader};
139  
140 #make sure the object does not have a permanent error set
141 if( ! $self->{toader}->errorblank ){
142 $self->{perror}=1;
143 $self->{error}=4;
144 $self->{errorString}='The Toader object has a permanent error set';
145 $self->warn;
146 return $self;
147 }
148  
149 #make sure a directory is set
150 if( ! defined( $self->{obj}->dirGet ) ){
151 $self->{perror}=1;
152 $self->{error}=5;
153 $self->{errorString}='The Toader::Entry object does not have a directory set';
154 $self->warn;
155 return $self;
156 }
157 $self->{dir}=$self->{obj}->dirGet;
158  
159 #initialize this here for simplicity
160 $self->{t}=Toader::Templates->new({
161 dir=>$self->{obj}->dirGet,
162 toader=>$args{toader},
163 });
164  
165 #initialize the general object here for simplicity
166 $self->{g}=Toader::Render::General->new(
167 {
168 toader=>$self->{toader},
169 self=>\$self,
170 obj=>$self->{obj},
171 toDir=>$self->{toDir},
172 }
173 );
174 if ( $self->{g}->error ){
175 $self->{perror}=1;
176 $self->{error}=15;
177 $self->{errorString}='Failed to initialize Toader::Render::General. error="'
178 .$self->{g}->error.'" errorString="'.$self->{g}->errorString.'"';
179 $self->warn;
180 return $self;
181 }
182  
183 #initialize the Toader::pathHelper
184 $self->{ph}=Toader::pathHelper->new( $self->{dir} );
185 if ( $self->{ph}->error ){
186 $self->{perror}=1;
187 $self->{error}=6;
188 $self->{errorString}='Failed to initiate pathHelper. error="'.
189 $self->{ph}->error.'" errorString="'.$self->{ph}->errorString.'"';
190 $self->warn;
191 return $self;
192 }
193  
194 #gets the r2r for the object
195 $self->{r2r}=$self->{ph}->relative2root( $self->{dir} );
196 if ( $self->{ph}->error ){
197 $self->{perror}=1;
198 $self->{error}=19;
199 $self->{errorString}='pathHelper failed to find the relative2root path for "'.
200 $self->{odir}.'"';
201 $self->warn;
202 return $self;
203 }
204  
205 return $self;
206 }
207  
208 =head2 content
209  
210 This renders the content to be included in a static
211 entry page.
212  
213 my $content=$foo->content;
214  
215 =cut
216  
217 sub content{
218 my $self=$_[0];
219  
220 if ( ! $self->errorblank ){
221 return undef;
222 }
223  
224 #puts together the date stuff
225 my $date=$self->{obj}->{mime}->header("Date");
226 my ($sec,$min,$hour,$day,$month,$year,$zone) = strptime($date);
227 $year=1900+$year;
228  
229 if( $day < 10 ){
230 $day='0'.$day;
231 }
232 $month++;
233 if( $month < 10 ){
234 $month='0'.$month;
235 }
236  
237 my $body=$self->{t}->fill_in_string( $self->{obj}->bodyGet,
238 {
239 title=>$self->{obj}->titleGet,
240 from=>$self->{obj}->fromGet,
241 date=>$self->{obj}->entryNameGet,
242 g=>\$self->{g},
243 toader=>\$self->{toader},
244 sec=>$sec,
245 min=>$min,
246 hour=>$hour,
247 day=>$day,
248 month=>$month,
249 year=>$year,
250 zone=>$zone,
251 obj=>\$self->{obj},
252 self=>\$self,
253 c=>\$self->{toader}->getConfig,
254 });
255 if ( $self->{t}->error ){
256 $self->{error}=8;
257 $self->{errorString}='Filling in the template failed. error="'.
258 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
259 $self->warn;
260 return undef;
261 }
262  
263 #this prepares to run it through the specified renderer
264 my $renderer;
265 my $torun='use Toader::Render::Entry::backends::'.$self->{obj}->rendererGet.'; '.
266 '$renderer=Toader::Render::Entry::backends::'.$self->{obj}->rendererGet.'->new({'.
267 'obj=>$self->{obj}, toader=>$self->{toader}, });';
268 eval( $torun );
269 if ( ! defined( $renderer ) ){
270 $self->{error}=8;
271 $self->{errorString}='Failed to initialize the backend. It returned undef. '.
272 'renderer="'.$self->{obj}->rendererGet.'"';
273 $self->warn;
274 return undef;
275 }
276 if ( $renderer->error ){
277 $self->{error}=8;
278 $self->{errorString}='Failed to initialize the backend. It returned with an error. '.
279 'error="'.$renderer->error.'" errorString="'.$renderer->errorString.'"';
280 $self->warn;
281 return undef;
282 }
283  
284 #render it
285 my $content=$renderer->render( $body );
286 if ( $renderer->error ){
287 $self->{error}=9;
288 $self->{errorString}='Failed to render the content. It returned an error. '.
289 'error="'.$renderer->error.'" errorString="'.$renderer->errorString.'"';
290 $self->warn;
291 return undef;
292 }
293  
294 $content=$self->{t}->fill_in( 'entryContent',
295 {
296 body=>$content,
297 title=>$self->{obj}->titleGet,
298 from=>$self->{obj}->fromGet,
299 date=>$self->{obj}->entryNameGet,
300 g=>\$self->{g},
301 toader=>\$self->{toader},
302 sec=>$sec,
303 min=>$min,
304 hour=>$hour,
305 day=>$day,
306 month=>$month,
307 year=>$year,
308 zone=>$zone,
309 self=>\$self,
310 obj=>\$self->{obj},
311 c=>\$self->{toader}->getConfig,
312 });
313 if ( $self->{t}->error ){
314 $self->{error}=8;
315 $self->{errorString}='Filling in the template failed. error="'.
316 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
317 $self->warn;
318 return undef;
319 }
320  
321 return $content;
322 }
323  
324 =head2 archive
325  
326 This renders the '.entries/archive.html' file.
327  
328 =cut
329  
330 sub archive{
331 my $self=$_[0];
332
333 if ( ! $self->errorblank ){
334 return undef;
335 }
336  
337 #makes sure we have a output directory set...
338 #while we don't care about this for rendering the content, we do need to
339 #know this for actually fully rendering it
340 my $output=$self->{toader}->getOutputDir;
341 if ( ! defined( $output ) ){
342 $self->{error}=11;
343 $self->{errorString}='No output directory has been set for the Toader object';
344 $self->warn;
345 return undef;
346 }
347  
348 #makes sure the output directory exists
349 if ( ! -d $output ){
350 $self->{error}=12;
351 $self->{errorString}='The output directory, "'.$output.'", does not exist';
352 $self->warn;
353 return undef;
354 }
355  
356 #this renders the content of it
357 my $content=$self->archiveContent;
358 if ( $self->error ){
359 $self->warnString('Failed to render the content to include');
360 return undef;
361 }
362  
363 my $page=$self->{t}->fill_in( 'page',
364 {
365 toader=>\$self->{toader},
366 g=>\$self->{g},
367 self=>\$self,
368 obj=>\$self->{obj},
369 c=>\$self->{toader}->getConfig,
370 locationID=>'Entries Archive',
371 content=>$content,
372 });
373 if ( $self->{t}->error ){
374 $self->{error}=8;
375 $self->{errorString}='Filling in the template failed. error="'.
376 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
377 $self->warn;
378 return undef;
379 }
380  
381 #the paths that will be used
382 my $dir=$output.'/'.$self->{r2r}.'/.entries/';
383 my $index=$dir.'archive.html';
384  
385 #make sure the files and entry directory exist
386 if ( ! -d $dir ){
387 if ( ! make_path( $dir ) ){
388 $self->{error}=13;
389 $self->{errorString}='The output entry directry, "'.$dir.'", could not be created';
390 $self->warn;
391 return undef;
392 }
393 }
394  
395 #write the index out
396 my $fh;
397 if ( ! open( $fh, '>', $index ) ){
398 $self->{error}=15;
399 $self->{errorString}='Failed to open the index, "'.$index.'", for writing';
400 $self->warn;
401 return undef;
402 }
403 print $fh $page;
404 close( $fh );
405  
406 return 1;
407 }
408  
409 =head2 archiveContent
410  
411 This renders the content for the index.html page for the entries.
412  
413 =cut
414  
415 sub archiveContent{
416 my $self=$_[0];
417
418 if ( ! $self->errorblank ){
419 return undef;
420 }
421  
422 my $content=$self->{t}->fill_in( 'entryArchive',
423 {
424 toader=>\$self->{toader},
425 g=>\$self->{g},
426 self=>\$self,
427 obj=>\$self->{obj},
428 c=>\$self->{toader}->getConfig,
429 });
430 if ( $self->{t}->error ){
431 $self->{error}=8;
432 $self->{errorString}='Filling in the template failed. error="'.
433 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
434 $self->warn;
435 return undef;
436 }
437  
438 return $content;
439 }
440  
441 =head2 index
442  
443 This renders the '.entries/index.html' file.
444  
445 =cut
446  
447 sub index{
448 my $self=$_[0];
449
450 if ( ! $self->errorblank ){
451 return undef;
452 }
453  
454 #makes sure we have a output directory set...
455 #while we don't care about this for rendering the content, we do need to
456 #know this for actually fully rendering it
457 my $output=$self->{toader}->getOutputDir;
458 if ( ! defined( $output ) ){
459 $self->{error}=11;
460 $self->{errorString}='No output directory has been set for the Toader object';
461 $self->warn;
462 return undef;
463 }
464  
465 #makes sure the output directory exists
466 if ( ! -d $output ){
467 $self->{error}=12;
468 $self->{errorString}='The output directory, "'.$output.'", does not exist';
469 $self->warn;
470 return undef;
471 }
472  
473 #this renders the content of it
474 my $content=$self->indexContent;
475 if ( $self->error ){
476 $self->warnString('Failed to render the content to include');
477 return undef;
478 }
479  
480 my $page=$self->{t}->fill_in( 'page',
481 {
482 toader=>\$self->{toader},
483 g=>\$self->{g},
484 self=>\$self,
485 obj=>\$self->{obj},
486 c=>\$self->{toader}->getConfig,
487 locationID=>'Latest Entries',
488 content=>$content,
489 });
490 if ( $self->{t}->error ){
491 $self->{error}=8;
492 $self->{errorString}='Filling in the template failed. error="'.
493 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
494 $self->warn;
495 return undef;
496 }
497  
498 #the paths that will be used
499 my $dir=$output.'/'.$self->{r2r}.'/.entries/';
500 my $index=$dir.'index.html';
501  
502 #make sure the files and entry directory exist
503 if ( ! -d $dir ){
504 if ( ! make_path( $dir ) ){
505 $self->{error}=13;
506 $self->{errorString}='The output entry directry, "'.$dir.'", could not be created';
507 $self->warn;
508 return undef;
509 }
510 }
511  
512 #write the index out
513 my $fh;
514 if ( ! open( $fh, '>', $index ) ){
515 $self->{error}=15;
516 $self->{errorString}='Failed to open the index, "'.$index.'", for writing';
517 $self->warn;
518 return undef;
519 }
520 print $fh $page;
521 close( $fh );
522  
523 return 1;
524 }
525  
526 =head2 indexContent
527  
528 This renders the content for the index.html page for the entries.
529  
530 =cut
531  
532 sub indexContent{
533 my $self=$_[0];
534
535 if ( ! $self->errorblank ){
536 return undef;
537 }
538  
539 my $content=$self->{t}->fill_in( 'entryIndex',
540 {
541 toader=>\$self->{toader},
542 g=>\$self->{g},
543 self=>\$self,
544 obj=>\$self->{obj},
545 c=>\$self->{toader}->getConfig,
546 });
547 if ( $self->{t}->error ){
548 $self->{error}=8;
549 $self->{errorString}='Filling in the template failed. error="'.
550 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
551 $self->warn;
552 return undef;
553 }
554  
555 return $content;
556 }
557  
558 =head2 render
559  
560 This renders the object.
561  
562 No arguments are taken.
563  
564 =cut
565  
566 sub render{
567 my $self=$_[0];
568
569 if ( ! $self->errorblank ){
570 return undef;
571 }
572  
573 #makes sure we have a output directory set...
574 #while we don't care about this for rendering the content, we do need to
575 #know this for actually fully rendering it
576 my $output=$self->{toader}->getOutputDir;
577 if ( ! defined( $output ) ){
578 $self->{error}=11;
579 $self->{errorString}='No output directory has been set for the Toader object';
580 $self->warn;
581 return undef;
582 }
583  
584 #makes sure the output directory exists
585 if ( ! -d $output ){
586 $self->{error}=12;
587 $self->{errorString}='The output directory, "'.$output.'", does not exist';
588 $self->warn;
589 return undef;
590 }
591  
592 #clean up the old entry
593 my $cleanup=Toader::Render::Entry::Cleanup->new( $self->{toader} );
594 if ( $cleanup->error ){
595 $self->{error}=6;
596 $self->{errorString}='Initialing the cleanup module failed. error="'.
597 $cleanup->error.'" errorString="'.$cleanup->errorString.'"';
598 $self->warn;
599 return undef;
600 }
601 $cleanup->cleanup( $self->{obj} );
602 if ( $cleanup->error ){
603 $self->{error}=7;
604 $self->{errorString}='Cleanup failed. error="'.$cleanup->error.
605 '" errorString="'.$cleanup->errorString.'"';
606 $self->warn;
607 return undef;
608 }
609  
610 #this renders the content of it
611 my $content=$self->content;
612 if ( $self->error ){
613 $self->warnString('Failed to render the content to include');
614 return undef;
615 }
616  
617 my $page=$self->{t}->fill_in( 'page',
618 {
619 toader=>\$self->{toader},
620 g=>\$self->{g},
621 self=>\$self,
622 obj=>\$self->{obj},
623 c=>\$self->{toader}->getConfig,
624 content=>$content,
625 });
626 if ( $self->{t}->error ){
627 $self->{error}=8;
628 $self->{errorString}='Filling in the template failed. error="'.
629 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
630 $self->warn;
631 return undef;
632 }
633  
634 #put together some paths for later use
635 my $entryDir=$output.'/'.$self->{r2r}.'/.entries/'.$self->{obj}->entryNameGet;
636 my $index=$entryDir.'/index.html';
637 my $fileDir=$entryDir.'/.files';
638  
639 #make sure the files and entry directory exist
640 if ( ! -d $entryDir ){
641 if ( ! make_path( $entryDir ) ){
642 $self->{error}=13;
643 $self->{errorString}='The output entry directry, "'.$entryDir.'", could not be created';
644 $self->warn;
645 return undef;
646 }
647 }
648 if ( ! -d $fileDir ){
649 if ( ! make_path( $fileDir ) ){
650 $self->{error}=14;
651 $self->{errorString}='The output file directry, "'.$fileDir.'", could not be created';
652 $self->warn;
653 return undef;
654 }
655 }
656  
657 #write the index out
658 my $fh;
659 if ( ! open( $fh, '>', $index ) ){
660 $self->{error}=15;
661 $self->{errorString}='Failed to open the index, "'.$index.'", for writing';
662 $self->warn;
663 return undef;
664 }
665 print $fh $page;
666 close( $fh );
667  
668 #extract the files
669 $self->{obj}->subpartsExtract( $fileDir );
670 if ( $self->{obj}->error ){
671 $self->{error}=16;
672 $self->{errorString}='Failed to extract the subparts. error="'.
673 $self->{obj}->error.'" errorString="'.$self->{obj}->errorString.'"';
674 $self->warn;
675 return undef;
676 }
677  
678 return 1;
679 }
680  
681 =head1 ERROR CODES
682  
683 =head2 1, noObj
684  
685 No L<Toader::Entry> object specified.
686  
687 =head2 2, noToaderObj
688  
689 No L<Toader> object specified.
690  
691 =head2 3, objPerror
692  
693 The L<Toader::Entry> object has a permanent error set.
694  
695 =head2 4, toaderObjPerror
696  
697 The L<Toader> object has a permanent error set.
698  
699 =head2 5, noDirSet
700  
701 The L<Toader::Entry> object does not have a directory set
702  
703 =head2 6, cleanupInitErrored
704  
705 Failed to initialize the cleanup module.
706  
707 =head2 7, cleanupErrored
708  
709 Failed to cleanup.
710  
711 =head2 8, backendInitErrored
712  
713 Failed to initialize the backend.
714  
715 =head2 9, pathhelperInitErrored
716  
717 Failed to initialize the path helper.
718  
719 =head2 11, noOutputDirSet
720  
721 The L<Toader> object does not have a output directory set.
722  
723 =head2 12, outputDirDoesNotExist
724  
725 The output directory does not exist.
726  
727 =head2 13, outputEntryDirCreationFailed
728  
729 The output entry directory could not be created.
730  
731 =head2 14, outputFileDirCreationFailed
732  
733 The output file directory could not be created.
734  
735 =head2 15, generalInitErrored
736  
737 Failed to initialize the L<Toader::Render::General> object.
738  
739 =head1 AUTHOR
740  
741 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
742  
743 =head1 BUGS
744  
745 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
746 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll
747 automatically be notified of progress on your bug as I make changes.
748  
749  
750  
751  
752 =head1 SUPPORT
753  
754 You can find documentation for this module with the perldoc command.
755  
756 perldoc Toader::Render
757  
758  
759 You can also look for information at:
760  
761 =over 4
762  
763 =item * RT: CPAN's request tracker
764  
765 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
766  
767 =item * AnnoCPAN: Annotated CPAN documentation
768  
769 L<http://annocpan.org/dist/Toader>
770  
771 =item * CPAN Ratings
772  
773 L<http://cpanratings.perl.org/d/Toader>
774  
775 =item * Search CPAN
776  
777 L<http://search.cpan.org/dist/Toader/>
778  
779 =back
780  
781  
782 =head1 ACKNOWLEDGEMENTS
783  
784  
785 =head1 LICENSE AND COPYRIGHT
786  
787 Copyright 2011. Zane C. Bowers-Hadley.
788  
789 This program is free software; you can redistribute it and/or modify it
790 under the terms of either: the GNU General Public License as published
791 by the Free Software Foundation; or the Artistic License.
792  
793 See http://dev.perl.org/licenses/ for more information.
794  
795  
796 =cut
797  
798 1; # End of Toader::Render::Entry