Toader/Render/AutoDoc.pm

filter: Statementstmt 16.5% 38 / 229 Branchbran 0.0% 0 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod 100% 3 / 3 totaltotal 16.6% 53 / 319 CC 37 SCAR 56.9
CC
37
Cov
13%
render
552.0
new
182.0
content
12.0
CRAP 296.3
« Toader/Render/Page/Cleanup.pm ↑ Summary Toader/Render/Directory.pm »
1 package Toader::Render::AutoDoc;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Toader::Render::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Toader::Templates;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Toader::Render::AutoDoc::Cleanup;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Toader::pathHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Pod::Simple::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Script::isAperlScript;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Toader::Render::AutoDoc - This renders a Toader::AutoDoc object.
19  
20 =head1 VERSION
21  
22 Version 0.2.1
23  
24 =cut
25  
26 1 our $VERSION = '0.2.1';
27  
28 =head1 SYNOPSIS
29  
30 =head1 METHODS
31  
32 =head2 new
33  
34 This initiates the object.
35  
36 =head3 args hash ref
37  
38 =head4 obj
39  
40 This is the L<Toader::AutoDoc> object to render.
41  
42 =head4 toader
43  
44 This is the L<Toader> object to use.
45  
46 =head2 toDir
47  
48 This is the value used to get from the directory it is being
49 rendered in back to directory storing stuff for that directory. By
50 default this is '../'.
51  
52 my $foo=Toader::Render::AutoDoc->new(\%args);
53 if($foo->error){
54 warn('error: '.$foo->error.":".$foo->errorString);
55 }
56  
57 =cut
58  
59 0 ▶sub new{
Subroutine new: not called CC=13 SCAR=52.0
Pod documented
60 0 my %args;
61 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
62 0 %args= %{$_[1]};
63 };
64  
65 0 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=>'cleanupNewErrored',
78 7=>'cleanupErrored',
79 8=>'fileListFailed',
80 9=>'pathhelperNewErrored',
81 11=>'noOutputDirSet',
82 12=>'outputDirDoesNotExist',
83 13=>'outputPageDirCreationFailed',
84 14=>'outputFileDirCreationFailed',
85 15=>'makePathFailed',
86 16=>'copyFailed',
87 },
88 },
89 };
90 0 bless $self;
91  
92 0 ▶ if ( defined( $args{toDir} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"toDir"}) 0 0 0
93 0 $self->{toDir}=$args{toDir};
94 }
95  
96 #make sure we have a Toader::AutoDoc object.
97 0 ▶ if ( ! defined( $args{obj} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"obj"}) 0 0 0
98 0 $self->{perror}=1;
99 0 $self->{error}=1;
100 0 $self->{errorString}='Nothing defined for the Toader::AutoDoc object';
101 0 $self->warn;
102 0 return $self;
103 }
104 0 ▶ if ( ref( $args{obj} ) ne 'Toader::AutoDoc' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"obj"} ne "Toader::AutoDoc") 0 0 0
105 0 $self->{perror}=1;
106 0 $self->{error}=1;
107 $self->{errorString}='The specified object is not a Toader::AutoDoc object, but a "'.
108 0 ref( $args{obj} ).'"';
109 0 $self->warn;
110 0 return $self;
111 }
112 0 $self->{obj}=$args{obj};
113  
114 #make sure the object does not have a permanent error set
115 0 ▶ if( ! $self->{obj}->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"obj"}->errorblank) 0 0 0
116 0 $self->{perror}=1;
117 0 $self->{error}=3;
118 0 $self->{errorString}='The Toader::AutoDoc object has a permanent error set';
119 0 $self->warn;
120 0 return $self;
121 }
122  
123 #make sure a Toader object is given
124 0 ▶ if ( ! defined( $args{toader} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"toader"}) 0 0 0
125 0 $self->{perror}=1;
126 0 $self->{error}=2;
127 0 $self->{errorString}='Nothing defined for the Toader object';
128 0 $self->warn;
129 0 return $self;
130 }
131 0 ▶ if ( ref( $args{toader} ) ne 'Toader' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"toader"} ne "Toader") 0 0 0
132 0 $self->{perror}=1;
133 0 $self->{error}=2;
134 $self->{errorString}='The specified object is not a Toader object, but a "'.
135 0 ref( $args{toader} ).'"';
136 0 $self->warn;
137 0 return $self;
138 }
139 0 $self->{toader}=$args{toader};
140  
141 #make sure the object does not have a permanent error set
142 0 ▶ if( ! $self->{toader}->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"toader"}->errorblank) 0 0 0
143 0 $self->{perror}=1;
144 0 $self->{error}=4;
145 0 $self->{errorString}='The Toader object has a permanent error set';
146 0 $self->warn;
147 0 return $self;
148 }
149  
150 #make sure a directory is set
151 0 ▶ if( ! defined( $self->{obj}->dirGet ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"obj"}->dirGet) 0 0 0
152 0 $self->{perror}=1;
153 0 $self->{error}=5;
154 0 $self->{errorString}='The Toader::AutoDoc object does not have a directory set';
155 0 $self->warn;
156 0 return $self;
157 }
158 0 $self->{dir}=$self->{obj}->dirGet;
159  
160 #initialize this here for simplicity
161 $self->{t}=Toader::Templates->new({
162 dir=>$self->{obj}->dirGet,
163 toader=>$args{toader},
164 0 });
165  
166 #initialize the general object here for simplicity
167 $self->{g}=Toader::Render::General->new(
168 {
169 toader=>$self->{toader},
170 self=>\$self,
171 obj=>$self->{obj},
172 toDir=>$self->{toDir},
173 }
174 0 );
175 0 ▶ if ( $self->{g}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"g"}->error) 0 0 0
176 0 $self->{perror}='';
177 }
178  
179 #initialize the Toader::pathHelper
180 0 $self->{ph}=Toader::pathHelper->new( $self->{dir} );
181 0 ▶ if ( $self->{ph}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ph"}->error) 0 0 0
182 0 $self->{perror}=1;
183 0 $self->{error}=6;
184 $self->{errorString}='Failed to initiate pathHelper. error="'.
185 0 $self->{ph}->error.'" errorString="'.$self->{ph}->errorString.'"';
186 0 $self->warn;
187 0 return $self;
188 }
189  
190 #gets the r2r for the object
191 0 $self->{r2r}=$self->{ph}->relative2root( $self->{dir} );
192 0 ▶ if ( $self->{ph}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ph"}->error) 0 0 0
193 0 $self->{perror}=1;
194 0 $self->{error}=19;
195 $self->{errorString}='pathHelper failed to find the relative2root path for "'.
196 0 $self->{odir}.'"';
197 0 $self->warn;
198 0 return $self;
199 }
200  
201 0 return $self;
202 }
203  
204 =head2 content
205  
206 This renders the content to be included in a static page.
207  
208 my $content=$foo->content;
209  
210 =cut
211  
212 0 ▶sub content{
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
213 0 my $self=$_[0];
214  
215 0 ▶ if ( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
216 0 return undef;
217 }
218  
219 my $content=$self->{t}->fill_in( 'autodocContent',
220 {
221 g=>\$self->{g},
222 toader=>\$self->{toader},
223 self=>\$self,
224 obj=>\$self->{obj},
225 c=>\$self->{toader}->getConfig,
226 0 });
227 0 ▶ if ( $self->{t}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"t"}->error) 0 0 0
228 0 $self->{error}=8;
229 $self->{errorString}='Filling in the template failed. error="'.
230 0 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
231 0 $self->warn;
232 0 return undef;
233 }
234  
235 0 return $content;
236 }
237  
238 =head2 render
239  
240 This renders the object.
241  
242 No arguments are taken.
243  
244 =cut
245  
246 0 ▶sub render{
Subroutine render: not called CC=23 SCAR=63.1
Pod documented
247 0 my $self=$_[0];
248
249 0 ▶ if ( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
250 0 return undef;
251 }
252  
253 #makes sure we have a output directory set...
254 #while we don't care about this for rendering the content, we do need to
255 #know this for actually fully rendering it
256 0 my $output=$self->{toader}->getOutputDir;
257 0 ▶ if ( ! defined( $output ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 0 0
258 0 $self->{error}=11;
259 0 $self->{errorString}='No output directory has been set for the Toader object';
260 0 $self->warn;
261 0 return undef;
262 }
263  
264 #makes sure the output directory exists
265 0 ▶ if ( ! -d $output ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $output) 0 0 0
266 0 $self->{error}=12;
267 0 $self->{errorString}='The output directory, "'.$output.'", does not exist';
268 0 $self->warn;
269 0 return undef;
270 }
271  
272 #clean up the old page
273 0 my $cleanup=Toader::Render::AutoDoc::Cleanup->new( $self->{toader} );
274 0 ▶ if ( $cleanup->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cleanup->error) 0 0 0
275 0 $self->{error}=6;
276 0 $self->{errorString}='Initialing the cleanup module failed. error="'.
277 $cleanup->error.'" errorString="'.$cleanup->errorString.'"';
278 0 $self->warn;
279 0 return undef;
280 }
281 0 $cleanup->cleanup( $self->{obj} );
282 0 ▶ if ( $cleanup->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cleanup->error) 0 0 0
283 0 $self->{error}=7;
284 0 $self->{errorString}='Cleanup failed. error="'.$cleanup->error.
285 '" errorString="'.$cleanup->errorString.'"';
286 0 $self->warn;
287 0 return undef;
288 }
289  
290 #this renders the content of it
291 0 my $content=$self->content;
292 0 ▶ if ( $self->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) 0 0 0
293 0 $self->warnString('Failed to render the content to include');
294 0 return undef;
295 }
296  
297 my $page=$self->{t}->fill_in( 'page',
298 {
299 toader=>\$self->{toader},
300 g=>\$self->{g},
301 self=>\$self,
302 obj=>\$self->{obj},
303 c=>\$self->{toader}->getConfig,
304 0 content=>$content,
305 });
306 0 ▶ if ( $self->{t}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"t"}->error) 0 0 0
307 0 $self->{error}=8;
308 $self->{errorString}='Filling in the template failed. error="'.
309 0 $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"';
310 0 $self->warn;
311 0 return undef;
312 }
313  
314 #put together some paths for later use
315 0 my $autodocDir=$output.'/'.$self->{r2r}.'/.autodoc/';
316 0 my $index=$autodocDir.'/index.html';
317 0 my $filesDir=$autodocDir.'/.files';
318  
319 #make sure the files and entry directory exist
320 0 ▶ if ( ! -d $autodocDir ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $autodocDir) 0 0 0
321 0 ▶ if ( ! make_path( $autodocDir ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (make_path($autodocDir)) 0 0 0
322 0 $self->{error}=13;
323 0 $self->{errorString}='The output entry directry, "'.$autodocDir.'", could not be created';
324 0 $self->warn;
325 0 return undef;
326 }
327 }
328 0 ▶ if ( ! -d $filesDir ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $filesDir) 0 0 0
329 0 ▶ if ( ! make_path( $filesDir ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (make_path($filesDir)) 0 0 0
330 0 $self->{error}=14;
331 0 $self->{errorString}='The output file directry, "'.$filesDir.'", could not be created';
332 0 $self->warn;
333 0 return undef;
334 }
335 }
336  
337 #write the index out
338 0 my $fh;
339 0 ▶ if ( ! open( $fh, '>', $index ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">", $index) 0 0 0
340 0 $self->{error}=15;
341 0 $self->{errorString}='Failed to open the index, "'.$index.'", for writing';
342 0 $self->warn;
343 0 return undef;
344 }
345 0 print $fh $page;
346 0 close( $fh );
347  
348 #gets a list of documents
349 0 my @files=$self->{obj}->findDocs;
350 0 ▶ if ( $self->{obj}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"obj"}->error) 0 0 0
351 0 $self->{error}=8;
352 0 $self->{errorString}='Failed to get a list of the documents';
353 0 $self->warn;
354 0 return undef;
355 }
356  
357 #copy them all over
358 0 my $int=0;
359 0 ▶ while ( defined( $files[$int] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) 0 0 0
360 0 my $copyFrom=$self->{dir}.'/'.$files[$int];
361 0 my $copyTo=$filesDir.'/'.$files[$int];
362  
363 #append .html for POD docs
364 0 my $ispod=0;
365 0 my $checker=Script::isAperlScript->new({
366 any=>1,
367 env=>1,
368 });
369 0 ▶ if ( $copyTo =~ /\.[Pp][Oo][Dd]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($copyTo =~ /\.[Pp][Oo][Dd]$/) 0 0 0
370 0 $ispod='1';
371 }
372 0 ▶ if ( $copyTo =~ /\.[Pp][Mm]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($copyTo =~ /\.[Pp][Mm]$/) 0 0 0
373 0 $ispod='1';
374 }
375 0 ▶ if ( $checker->isAperlScript( $copyFrom ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($checker->isAperlScript($copyFrom)) 0 0 0
376 0 $ispod='1';
377 }
378  
379 #make sure the directory exists
380 0 my $outputdir=$copyTo;
381 0 my $outputdirTest=$copyTo;
382 0 $outputdirTest=~s/.*\///;
383 0 $outputdir=~s/$outputdirTest$//;
384 0 ▶ if ( ! -d $outputdir ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $outputdir) 0 0 0
385 0 ▶ if ( ! make_path( $outputdir ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (make_path($outputdir)) 0 0 0
386 0 $self->{error}=15;
387 0 $self->{errorString}='make_path for "'.$outputdir.'" failed';
388 0 $self->warn;
389 0 return undef;
390 }
391 }
392  
393 0 ▶ if ( $ispod ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ispod) { } 0 0 0
394 #handles it if it is a pod
395 0 $copyTo=$copyTo.'.html';
396
397 #convert to HTML from POD
398 0 my $p2h=Pod::Simple::HTML->new;
399 0 $p2h->index(1);
400 0 my $html;
401 0 $p2h->output_string(\$html);
402 0 $p2h->parse_file($copyFrom);
403  
404 #open the $copyTo and write it out
405 0 my $fh;
406 0 ▶ if ( ! open( $fh, '>', $copyTo ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">", $copyTo) 0 0 0
407 0 $self->{error}=16;
408 0 $self->{errorString}='Failed to open "'.$copyTo.'"';
409 0 $self->warn;
410 0 return undef;
411 }
412 0 print $fh $html;
413 0 close( $fh );
414  
415 }else{
416  
417 #copy it
418 0 ▶ if ( ! copy( $copyFrom, $copyTo ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (copy($copyFrom, $copyTo)) 0 0 0
419 0 $self->{error}=16;
420 0 $self->{errorString}='Failed to copy "'.$copyFrom.'" to "'.$copyTo.'"';
421 0 $self->warn;
422 0 return undef;
423 }
424  
425 }
426  
427 0 $int++;
428 }
429
430  
431 0 return 1;
432 }
433  
434 =head1 ERROR CODES
435  
436 =head2 1, noObj
437  
438 No L<Toader::AutoDoc> object specified.
439  
440 =head2 2, noToaderObj
441  
442 No L<Toader> object specified.
443  
444 =head2 3, objPerror
445  
446 The L<Toader::AutoDoc> object has a permanent error set.
447  
448 =head2 4, toaderObjPerror
449  
450 The L<Toader> object has a permanent error set.
451  
452 =head2 5, noDirSet
453  
454 The L<Toader::AutoDoc> object does not have a directory set
455  
456 =head2 6, cleanupNewErrored
457  
458 Failed to initialize the cleanup module.
459  
460 =head2 7, cleanupErrored
461  
462 Failed to cleanup.
463  
464 =head2 8, fileListFailed
465  
466 Failed to get a list of files.
467  
468 =head2 9, pathhelperNewErrored
469  
470 Failed to initialize the path helper.
471  
472 =head2 11, noOutputDirSet
473  
474 The L<Toader> object does not have a output directory set.
475  
476 =head2 12, outputDirDoesNotExist
477  
478 The output directory does not exist.
479  
480 =head2 13, outputPageDirCreationFailed
481  
482 The output page directory could not be created.
483  
484 =head2 14, outputFileDirCreationFailed
485  
486 The output file directory could not be created.
487  
488 =head2 15, makePathFailed
489  
490 make_path failed.
491  
492 =head2 16, copyFailed
493  
494 Failed to copy the HTML produced from the POD to where it belongs.
495  
496 =head1 AUTHOR
497  
498 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
499  
500 =head1 BUGS
501  
502 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
503 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll
504 automatically be notified of progress on your bug as I make changes.
505  
506  
507  
508  
509 =head1 SUPPORT
510  
511 You can find documentation for this module with the perldoc command.
512  
513 perldoc Toader::Render::AutoDoc
514  
515 You can also look for information at:
516  
517 =over 4
518  
519 =item * RT: CPAN's request tracker
520  
521 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
522  
523 =item * AnnoCPAN: Annotated CPAN documentation
524  
525 L<http://annocpan.org/dist/Toader>
526  
527 =item * CPAN Ratings
528  
529 L<http://cpanratings.perl.org/d/Toader>
530  
531 =item * Search CPAN
532  
533 L<http://search.cpan.org/dist/Toader/>
534  
535 =back
536  
537  
538 =head1 ACKNOWLEDGEMENTS
539  
540  
541 =head1 LICENSE AND COPYRIGHT
542  
543 Copyright 2013. Zane C. Bowers-Hadley.
544  
545 This program is free software; you can redistribute it and/or modify it
546 under the terms of either: the GNU General Public License as published
547 by the Free Software Foundation; or the Artistic License.
548  
549 See http://dev.perl.org/licenses/ for more information.
550  
551  
552 =cut
553  
554 1 1; # End of Toader::Render::AutoDoc
« Toader/Render/Page/Cleanup.pm ↑ Summary Toader/Render/Directory.pm »