| 1 | package Toader::Render::AutoDoc; | ||
| 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::Render::AutoDoc::Cleanup; | ||
| 10 | use Toader::pathHelper; | ||
| 11 | use File::Path qw(make_path); | ||
| 12 | use Pod::Simple::HTML; | ||
| 13 | use File::Copy; | ||
| 14 | use Script::isAperlScript; | ||
| 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 | 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 | 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=>'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 | bless $self; | ||
| 91 | |||
| 92 | if ( defined( $args{toDir} ) ){ | ||
| 93 | $self->{toDir}=$args{toDir}; | ||
| 94 | } | ||
| 95 | |||
| 96 | #make sure we have a Toader::AutoDoc object. | ||
| 97 | if ( ! defined( $args{obj} ) ){ | ||
| 98 | $self->{perror}=1; | ||
| 99 | $self->{error}=1; | ||
| 100 | $self->{errorString}='Nothing defined for the Toader::AutoDoc object'; | ||
| 101 | $self->warn; | ||
| 102 | return $self; | ||
| 103 | } | ||
| 104 | if ( ref( $args{obj} ) ne 'Toader::AutoDoc' ){ | ||
| 105 | $self->{perror}=1; | ||
| 106 | $self->{error}=1; | ||
| 107 | $self->{errorString}='The specified object is not a Toader::AutoDoc object, but a "'. | ||
| 108 | ref( $args{obj} ).'"'; | ||
| 109 | $self->warn; | ||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | $self->{obj}=$args{obj}; | ||
| 113 | |||
| 114 | #make sure the object does not have a permanent error set | ||
| 115 | if( ! $self->{obj}->errorblank ){ | ||
| 116 | $self->{perror}=1; | ||
| 117 | $self->{error}=3; | ||
| 118 | $self->{errorString}='The Toader::AutoDoc object has a permanent error set'; | ||
| 119 | $self->warn; | ||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | #make sure a Toader object is given | ||
| 124 | if ( ! defined( $args{toader} ) ){ | ||
| 125 | $self->{perror}=1; | ||
| 126 | $self->{error}=2; | ||
| 127 | $self->{errorString}='Nothing defined for the Toader object'; | ||
| 128 | $self->warn; | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | if ( ref( $args{toader} ) ne 'Toader' ){ | ||
| 132 | $self->{perror}=1; | ||
| 133 | $self->{error}=2; | ||
| 134 | $self->{errorString}='The specified object is not a Toader object, but a "'. | ||
| 135 | ref( $args{toader} ).'"'; | ||
| 136 | $self->warn; | ||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | $self->{toader}=$args{toader}; | ||
| 140 | |||
| 141 | #make sure the object does not have a permanent error set | ||
| 142 | if( ! $self->{toader}->errorblank ){ | ||
| 143 | $self->{perror}=1; | ||
| 144 | $self->{error}=4; | ||
| 145 | $self->{errorString}='The Toader object has a permanent error set'; | ||
| 146 | $self->warn; | ||
| 147 | return $self; | ||
| 148 | } | ||
| 149 | |||
| 150 | #make sure a directory is set | ||
| 151 | if( ! defined( $self->{obj}->dirGet ) ){ | ||
| 152 | $self->{perror}=1; | ||
| 153 | $self->{error}=5; | ||
| 154 | $self->{errorString}='The Toader::AutoDoc object does not have a directory set'; | ||
| 155 | $self->warn; | ||
| 156 | return $self; | ||
| 157 | } | ||
| 158 | $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 | }); | ||
| 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 | ); | ||
| 175 | if ( $self->{g}->error ){ | ||
| 176 | $self->{perror}=''; | ||
| 177 | } | ||
| 178 | |||
| 179 | #initialize the Toader::pathHelper | ||
| 180 | $self->{ph}=Toader::pathHelper->new( $self->{dir} ); | ||
| 181 | if ( $self->{ph}->error ){ | ||
| 182 | $self->{perror}=1; | ||
| 183 | $self->{error}=6; | ||
| 184 | $self->{errorString}='Failed to initiate pathHelper. error="'. | ||
| 185 | $self->{ph}->error.'" errorString="'.$self->{ph}->errorString.'"'; | ||
| 186 | $self->warn; | ||
| 187 | return $self; | ||
| 188 | } | ||
| 189 | |||
| 190 | #gets the r2r for the object | ||
| 191 | $self->{r2r}=$self->{ph}->relative2root( $self->{dir} ); | ||
| 192 | if ( $self->{ph}->error ){ | ||
| 193 | $self->{perror}=1; | ||
| 194 | $self->{error}=19; | ||
| 195 | $self->{errorString}='pathHelper failed to find the relative2root path for "'. | ||
| 196 | $self->{odir}.'"'; | ||
| 197 | $self->warn; | ||
| 198 | return $self; | ||
| 199 | } | ||
| 200 | |||
| 201 | 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 | sub content{ | ||
| 213 | my $self=$_[0]; | ||
| 214 | |||
| 215 | if ( ! $self->errorblank ){ | ||
| 216 | 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 | }); | ||
| 227 | if ( $self->{t}->error ){ | ||
| 228 | $self->{error}=8; | ||
| 229 | $self->{errorString}='Filling in the template failed. error="'. | ||
| 230 | $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"'; | ||
| 231 | $self->warn; | ||
| 232 | return undef; | ||
| 233 | } | ||
| 234 | |||
| 235 | return $content; | ||
| 236 | } | ||
| 237 | |||
| 238 | =head2 render | ||
| 239 | |||
| 240 | This renders the object. | ||
| 241 | |||
| 242 | No arguments are taken. | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub render{ | ||
| 247 | my $self=$_[0]; | ||
| 248 | |||
| 249 | if ( ! $self->errorblank ){ | ||
| 250 | 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 | my $output=$self->{toader}->getOutputDir; | ||
| 257 | if ( ! defined( $output ) ){ | ||
| 258 | $self->{error}=11; | ||
| 259 | $self->{errorString}='No output directory has been set for the Toader object'; | ||
| 260 | $self->warn; | ||
| 261 | return undef; | ||
| 262 | } | ||
| 263 | |||
| 264 | #makes sure the output directory exists | ||
| 265 | if ( ! -d $output ){ | ||
| 266 | $self->{error}=12; | ||
| 267 | $self->{errorString}='The output directory, "'.$output.'", does not exist'; | ||
| 268 | $self->warn; | ||
| 269 | return undef; | ||
| 270 | } | ||
| 271 | |||
| 272 | #clean up the old page | ||
| 273 | my $cleanup=Toader::Render::AutoDoc::Cleanup->new( $self->{toader} ); | ||
| 274 | if ( $cleanup->error ){ | ||
| 275 | $self->{error}=6; | ||
| 276 | $self->{errorString}='Initialing the cleanup module failed. error="'. | ||
| 277 | $cleanup->error.'" errorString="'.$cleanup->errorString.'"'; | ||
| 278 | $self->warn; | ||
| 279 | return undef; | ||
| 280 | } | ||
| 281 | $cleanup->cleanup( $self->{obj} ); | ||
| 282 | if ( $cleanup->error ){ | ||
| 283 | $self->{error}=7; | ||
| 284 | $self->{errorString}='Cleanup failed. error="'.$cleanup->error. | ||
| 285 | '" errorString="'.$cleanup->errorString.'"'; | ||
| 286 | $self->warn; | ||
| 287 | return undef; | ||
| 288 | } | ||
| 289 | |||
| 290 | #this renders the content of it | ||
| 291 | my $content=$self->content; | ||
| 292 | if ( $self->error ){ | ||
| 293 | $self->warnString('Failed to render the content to include'); | ||
| 294 | 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 | content=>$content, | ||
| 305 | }); | ||
| 306 | if ( $self->{t}->error ){ | ||
| 307 | $self->{error}=8; | ||
| 308 | $self->{errorString}='Filling in the template failed. error="'. | ||
| 309 | $self->{t}->error.'" errorString="'.$self->{t}->errorString.'"'; | ||
| 310 | $self->warn; | ||
| 311 | return undef; | ||
| 312 | } | ||
| 313 | |||
| 314 | #put together some paths for later use | ||
| 315 | my $autodocDir=$output.'/'.$self->{r2r}.'/.autodoc/'; | ||
| 316 | my $index=$autodocDir.'/index.html'; | ||
| 317 | my $filesDir=$autodocDir.'/.files'; | ||
| 318 | |||
| 319 | #make sure the files and entry directory exist | ||
| 320 | if ( ! -d $autodocDir ){ | ||
| 321 | if ( ! make_path( $autodocDir ) ){ | ||
| 322 | $self->{error}=13; | ||
| 323 | $self->{errorString}='The output entry directry, "'.$autodocDir.'", could not be created'; | ||
| 324 | $self->warn; | ||
| 325 | return undef; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | if ( ! -d $filesDir ){ | ||
| 329 | if ( ! make_path( $filesDir ) ){ | ||
| 330 | $self->{error}=14; | ||
| 331 | $self->{errorString}='The output file directry, "'.$filesDir.'", could not be created'; | ||
| 332 | $self->warn; | ||
| 333 | return undef; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | #write the index out | ||
| 338 | my $fh; | ||
| 339 | if ( ! open( $fh, '>', $index ) ){ | ||
| 340 | $self->{error}=15; | ||
| 341 | $self->{errorString}='Failed to open the index, "'.$index.'", for writing'; | ||
| 342 | $self->warn; | ||
| 343 | return undef; | ||
| 344 | } | ||
| 345 | print $fh $page; | ||
| 346 | close( $fh ); | ||
| 347 | |||
| 348 | #gets a list of documents | ||
| 349 | my @files=$self->{obj}->findDocs; | ||
| 350 | if ( $self->{obj}->error ){ | ||
| 351 | $self->{error}=8; | ||
| 352 | $self->{errorString}='Failed to get a list of the documents'; | ||
| 353 | $self->warn; | ||
| 354 | return undef; | ||
| 355 | } | ||
| 356 | |||
| 357 | #copy them all over | ||
| 358 | my $int=0; | ||
| 359 | while ( defined( $files[$int] ) ){ | ||
| 360 | my $copyFrom=$self->{dir}.'/'.$files[$int]; | ||
| 361 | my $copyTo=$filesDir.'/'.$files[$int]; | ||
| 362 | |||
| 363 | #append .html for POD docs | ||
| 364 | my $ispod=0; | ||
| 365 | my $checker=Script::isAperlScript->new({ | ||
| 366 | any=>1, | ||
| 367 | env=>1, | ||
| 368 | }); | ||
| 369 | if ( $copyTo =~ /\.[Pp][Oo][Dd]$/ ){ | ||
| 370 | $ispod='1'; | ||
| 371 | } | ||
| 372 | if ( $copyTo =~ /\.[Pp][Mm]$/ ){ | ||
| 373 | $ispod='1'; | ||
| 374 | } | ||
| 375 | if ( $checker->isAperlScript( $copyFrom ) ){ | ||
| 376 | $ispod='1'; | ||
| 377 | } | ||
| 378 | |||
| 379 | #make sure the directory exists | ||
| 380 | my $outputdir=$copyTo; | ||
| 381 | my $outputdirTest=$copyTo; | ||
| 382 | $outputdirTest=~s/.*\///; | ||
| 383 | $outputdir=~s/$outputdirTest$//; | ||
| 384 | if ( ! -d $outputdir ){ | ||
| 385 | if ( ! make_path( $outputdir ) ){ | ||
| 386 | $self->{error}=15; | ||
| 387 | $self->{errorString}='make_path for "'.$outputdir.'" failed'; | ||
| 388 | $self->warn; | ||
| 389 | return undef; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | if ( $ispod ){ | ||
| 394 | #handles it if it is a pod | ||
| 395 | $copyTo=$copyTo.'.html'; | ||
| 396 | |||
| 397 | #convert to HTML from POD | ||
| 398 | my $p2h=Pod::Simple::HTML->new; | ||
| 399 | $p2h->index(1); | ||
| 400 | my $html; | ||
| 401 | $p2h->output_string(\$html); | ||
| 402 | $p2h->parse_file($copyFrom); | ||
| 403 | |||
| 404 | #open the $copyTo and write it out | ||
| 405 | my $fh; | ||
| 406 | if ( ! open( $fh, '>', $copyTo ) ){ | ||
| 407 | $self->{error}=16; | ||
| 408 | $self->{errorString}='Failed to open "'.$copyTo.'"'; | ||
| 409 | $self->warn; | ||
| 410 | return undef; | ||
| 411 | } | ||
| 412 | print $fh $html; | ||
| 413 | close( $fh ); | ||
| 414 | |||
| 415 | }else{ | ||
| 416 | |||
| 417 | #copy it | ||
| 418 | if ( ! copy( $copyFrom, $copyTo ) ){ | ||
| 419 | $self->{error}=16; | ||
| 420 | $self->{errorString}='Failed to copy "'.$copyFrom.'" to "'.$copyTo.'"'; | ||
| 421 | $self->warn; | ||
| 422 | return undef; | ||
| 423 | } | ||
| 424 | |||
| 425 | } | ||
| 426 | |||
| 427 | $int++; | ||
| 428 | } | ||
| 429 | |||
| 430 | |||
| 431 | 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; # End of Toader::Render::AutoDoc |