| 1 | package Toader::Templates; | ||
| 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 Toader::isaToaderDir; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Cwd 'abs_path'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use base 'Error::Helper'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Text::Template; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Toader::Templates::Defaults; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Toader::Templates - This handles fetching Toader templates. | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 1.0.0 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '1.0.0'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | For information on the storage and rendering of entries, | ||
| 26 | please see 'Documentation/Templates.pod'. | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head2 new | ||
| 31 | |||
| 32 | =head3 args hash ref | ||
| 33 | |||
| 34 | =head4 dir | ||
| 35 | |||
| 36 | This is the directory to intiate in. | ||
| 37 | |||
| 38 | =head4 toader | ||
| 39 | |||
| 40 | This is a L<Toader> object. | ||
| 41 | |||
| 42 | my $foo = Toader::Templates->new( \%args ); | ||
| 43 | if($foo->error){ | ||
| 44 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 45 | } | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub new{ | ||
| 50 | my %args; | ||
| 51 | if(defined($_[1])){ | ||
| 52 | %args= %{$_[1]}; | ||
| 53 | }; | ||
| 54 | |||
| 55 | my $self={ | ||
| 56 | error=>undef, | ||
| 57 | errorString=>'', | ||
| 58 | perror=>undef, | ||
| 59 | isatd=>Toader::isaToaderDir->new(), | ||
| 60 | dir=>undef, | ||
| 61 | defaults=>Toader::Templates::Defaults->new, | ||
| 62 | errorExtra=>{ | ||
| 63 | flags=>{ | ||
| 64 | 1=>'noDirSpecified', | ||
| 65 | 2=>'isaToaderDirErrored', | ||
| 66 | 3=>'notAtoaderDir', | ||
| 67 | 4=>'invalidTemplateName', | ||
| 68 | 5=>'openTemplateFailed', | ||
| 69 | 6=>'noDefaultTemplate', | ||
| 70 | 7=>'templateFillErrored', | ||
| 71 | 8=>'templateStringUndef', | ||
| 72 | 9=>'templateNameUndef', | ||
| 73 | 10=>'notAtoaderObj', | ||
| 74 | 11=>'getVCSerrored', | ||
| 75 | 12=>'VCSusableErrored', | ||
| 76 | 13=>'noTemplateSpecified', | ||
| 77 | 14=>'underVCSerrored', | ||
| 78 | 15=>'VCSaddErrored', | ||
| 79 | 16=>'VCSdeleteErrored', | ||
| 80 | 17=>'unlinkFailed', | ||
| 81 | 18=>'notInDir', | ||
| 82 | 19=>'noToaderObj', | ||
| 83 | }, | ||
| 84 | }, | ||
| 85 | VCSusable=>0, | ||
| 86 | }; | ||
| 87 | bless $self; | ||
| 88 | |||
| 89 | if ( defined( $args{dir} ) ){ | ||
| 90 | if ( ! $self->{isatd}->isaToaderDir( $args{dir} ) ){ | ||
| 91 | $self->{perror}=1; | ||
| 92 | $self->{error}=1, | ||
| 93 | $self->{errorString}='The specified directory is not a Toader directory'; | ||
| 94 | $self->warn; | ||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | $self->{dir}=$args{dir}; | ||
| 98 | } | ||
| 99 | |||
| 100 | #if we have a Toader object, reel it in | ||
| 101 | if ( ! defined( $args{toader} ) ){ | ||
| 102 | $self->{error}=19; | ||
| 103 | $self->{perror}=1; | ||
| 104 | $self->{errorString}='No $args{toader} specified'; | ||
| 105 | $self->warn; | ||
| 106 | return $self; | ||
| 107 | } | ||
| 108 | if ( ref( $args{toader} ) ne "Toader" ){ | ||
| 109 | $self->{perror}=1; | ||
| 110 | $self->{error}=10; | ||
| 111 | $self->{errorString}='The object specified is a "'.ref($args{toader}).'"'; | ||
| 112 | $self->warn; | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | $self->{toader}=$args{toader}; | ||
| 116 | |||
| 117 | #gets the Toader::VCS object | ||
| 118 | $self->{vcs}=$self->{toader}->getVCS; | ||
| 119 | if ( $self->{toader}->error ){ | ||
| 120 | $self->{perror}=1; | ||
| 121 | $self->{error}=11; | ||
| 122 | $self->{errorString}='Toader->getVCS errored. error="'. | ||
| 123 | $self->{toader}->error.'" errorString="'.$self->{toader}->errorString.'"'; | ||
| 124 | $self->warn; | ||
| 125 | return $self; | ||
| 126 | } | ||
| 127 | |||
| 128 | #checks if VCS is usable | ||
| 129 | $self->{VCSusable}=$self->{vcs}->usable; | ||
| 130 | if ( $self->{vcs}->error ){ | ||
| 131 | $self->{perror}=1; | ||
| 132 | $self->{error}=12; | ||
| 133 | $self->{errorString}='Toader::VCS->usable errored. error="'. | ||
| 134 | $self->{toader}->error.'" errorString="'.$self->{toader}->errorString.'"'; | ||
| 135 | $self->warn; | ||
| 136 | return $self; | ||
| 137 | } | ||
| 138 | |||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 dirGet | ||
| 143 | |||
| 144 | This gets L<Toader> directory this entry is associated with. | ||
| 145 | |||
| 146 | This will only error if a permanent error is set. | ||
| 147 | |||
| 148 | my $dir=$foo->dirGet; | ||
| 149 | if($foo->error){ | ||
| 150 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 151 | } | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub dirGet{ | ||
| 156 | my $self=$_[0]; | ||
| 157 | |||
| 158 | if (!$self->errorblank){ | ||
| 159 | return undef; | ||
| 160 | } | ||
| 161 | |||
| 162 | return $self->{dir}; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head2 dirSet | ||
| 166 | |||
| 167 | This sets L<Toader> directory this entry is associated with. | ||
| 168 | |||
| 169 | One argument is taken and it is the L<Toader> directory to set it to. | ||
| 170 | |||
| 171 | $foo->dirSet($toaderDirectory); | ||
| 172 | if($foo->error){ | ||
| 173 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 174 | } | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub dirSet{ | ||
| 179 | my $self=$_[0]; | ||
| 180 | my $dir=$_[1]; | ||
| 181 | |||
| 182 | if (!$self->errorblank){ | ||
| 183 | return undef; | ||
| 184 | } | ||
| 185 | |||
| 186 | #checks if the directory is Toader directory or not | ||
| 187 | my $returned=$self->{isatd}->isaToaderDir($dir); | ||
| 188 | if (! $returned ) { | ||
| 189 | $self->{error}=1; | ||
| 190 | $self->{errorString}='"'.$dir.'" is not a Toader directory'; | ||
| 191 | $self->warn; | ||
| 192 | return undef; | ||
| 193 | } | ||
| 194 | |||
| 195 | $self->{dir}=$dir; | ||
| 196 | |||
| 197 | return 1; | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 fill_in | ||
| 201 | |||
| 202 | This fills in a template that has been passed to it. | ||
| 203 | |||
| 204 | Two arguments are taken. The first is the template name. | ||
| 205 | The second is a hash reference. | ||
| 206 | |||
| 207 | The returned string is the filled out template. | ||
| 208 | |||
| 209 | my $rendered=$foo->fill_in( $templateName, \%hash ); | ||
| 210 | if ( $foo->error ){ | ||
| 211 | warn( 'Error:'.$foo->error.': '.$foo->errorString ); | ||
| 212 | } | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | sub fill_in{ | ||
| 217 | my $self=$_[0]; | ||
| 218 | my $name=$_[1]; | ||
| 219 | my $hash=$_[2]; | ||
| 220 | |||
| 221 | if( ! $self->errorblank ){ | ||
| 222 | return undef; | ||
| 223 | } | ||
| 224 | |||
| 225 | #make sure a template name is specified | ||
| 226 | if ( ! defined( $name ) ){ | ||
| 227 | $self->{error}=9; | ||
| 228 | $self->{errorString}='No template name specified'; | ||
| 229 | $self->warn; | ||
| 230 | return undef; | ||
| 231 | } | ||
| 232 | |||
| 233 | #gets the template | ||
| 234 | my $template=$self->getTemplate( $name ); | ||
| 235 | if ( $self->error ){ | ||
| 236 | return undef; | ||
| 237 | } | ||
| 238 | |||
| 239 | return $self->fill_in_string( $template, $hash ); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 fill_in_string | ||
| 243 | |||
| 244 | This fills in a template that has been passed to it. | ||
| 245 | |||
| 246 | Two arguments are required and the first is the template string to use and | ||
| 247 | second it is the hash to pass to it. | ||
| 248 | |||
| 249 | The returned string is the filled out template. | ||
| 250 | |||
| 251 | my $rendered=$foo->fill_in_string( $templateString, \%hash ); | ||
| 252 | if ( $foo->error ){ | ||
| 253 | warn( 'Error:'.$foo->error.': '.$foo->errorString ); | ||
| 254 | } | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub fill_in_string{ | ||
| 259 | my $self=$_[0]; | ||
| 260 | my $string=$_[1]; | ||
| 261 | my $hash=$_[2]; | ||
| 262 | |||
| 263 | if( ! $self->errorblank ){ | ||
| 264 | return undef; | ||
| 265 | } | ||
| 266 | |||
| 267 | if ( ! defined( $string ) ){ | ||
| 268 | $self->{error}=8; | ||
| 269 | $self->{errorString}='No template string specified'; | ||
| 270 | $self->warn; | ||
| 271 | return undef; | ||
| 272 | } | ||
| 273 | |||
| 274 | my $template = Text::Template->new( | ||
| 275 | TYPE => 'STRING', | ||
| 276 | SOURCE => $string, | ||
| 277 | DELIMITERS=>[ '[==', '==]' ], | ||
| 278 | ); | ||
| 279 | |||
| 280 | my $rendered=$template->fill_in( | ||
| 281 | HASH=>$hash, | ||
| 282 | ); | ||
| 283 | |||
| 284 | if ( ! defined ( $rendered ) ){ | ||
| 285 | $self->{error}=7; | ||
| 286 | $self->{errorString}='Error encountered filling in the template'; | ||
| 287 | $self->warn; | ||
| 288 | return undef; | ||
| 289 | } | ||
| 290 | |||
| 291 | return $rendered; | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 findTemplate | ||
| 295 | |||
| 296 | This finds a specified template. | ||
| 297 | |||
| 298 | One arguement is taken and it is the name of the template. | ||
| 299 | |||
| 300 | A return of undef can mean either a error or it was not found. | ||
| 301 | If there was an error, the method error will return true. | ||
| 302 | |||
| 303 | my $templateFile=$foo->findTemplate($templateName); | ||
| 304 | if( !defined( $templateFile ) ){ | ||
| 305 | if($foo->error){ | ||
| 306 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 307 | }else{ | ||
| 308 | print("Not found\n"); | ||
| 309 | } | ||
| 310 | }else{ | ||
| 311 | print $templateFile."\n"; | ||
| 312 | } | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | sub findTemplate{ | ||
| 317 | my $self=$_[0]; | ||
| 318 | my $name=$_[1]; | ||
| 319 | |||
| 320 | if (!$self->errorblank){ | ||
| 321 | return undef; | ||
| 322 | } | ||
| 323 | |||
| 324 | #make sure a directory has been set | ||
| 325 | if (!defined( $self->{dir} )) { | ||
| 326 | $self->{error}=2; | ||
| 327 | $self->{errorString}='No directory has been set yet'; | ||
| 328 | $self->warn; | ||
| 329 | return undef; | ||
| 330 | } | ||
| 331 | |||
| 332 | #checks if the name is valid | ||
| 333 | my $returned=$self->templateNameCheck($name); | ||
| 334 | if (! $returned ) { | ||
| 335 | $self->{error}=4; | ||
| 336 | $self->{errorString}='"'.$name.'" is not a valid template name'; | ||
| 337 | $self->warn; | ||
| 338 | return undef; | ||
| 339 | } | ||
| 340 | |||
| 341 | #checks if the directory is Toader directory or not | ||
| 342 | $returned=$self->{isatd}->isaToaderDir( $self->{dir} ); | ||
| 343 | if (! $returned ) { | ||
| 344 | $self->{error}=3; | ||
| 345 | $self->{errorString}='"'.$self->{dir}.'" is no longer a Toader directory'; | ||
| 346 | $self->warn; | ||
| 347 | return undef; | ||
| 348 | } | ||
| 349 | |||
| 350 | #initial stuff to check | ||
| 351 | my $dir=$self->{dir}; | ||
| 352 | my $template=$dir.'/.toader/templates/'.$name; | ||
| 353 | |||
| 354 | #checks if the template exists | ||
| 355 | if (-f $template) { | ||
| 356 | return $template; | ||
| 357 | } | ||
| 358 | |||
| 359 | #recurse down trying to find the last one | ||
| 360 | $dir=abs_path($dir.'/..'); | ||
| 361 | #we will always find something below so it is just set to 1 | ||
| 362 | while (1) { | ||
| 363 | #we hit the FS root... | ||
| 364 | #if he hit this, something is definitely wrong | ||
| 365 | if ($dir eq '/') { | ||
| 366 | return undef; | ||
| 367 | } | ||
| 368 | |||
| 369 | #make sure | ||
| 370 | $returned=$self->{isatd}->isaToaderDir($dir); | ||
| 371 | if (!$returned) { | ||
| 372 | return undef; | ||
| 373 | } | ||
| 374 | |||
| 375 | #check if it exists | ||
| 376 | $template=$dir.'/.toader/templates/'.$name; | ||
| 377 | if (-f $template) { | ||
| 378 | return $template; | ||
| 379 | } | ||
| 380 | |||
| 381 | #check the next directory | ||
| 382 | $dir=abs_path($dir.'/..'); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | =head2 getTemplate | ||
| 387 | |||
| 388 | This finds a template and then returns it. | ||
| 389 | |||
| 390 | The method findTemplate will be used and if that fails the default | ||
| 391 | template will be returned. | ||
| 392 | |||
| 393 | One arguement is required and it is the template name. | ||
| 394 | |||
| 395 | my $template=$foo->getTemplate($templateName); | ||
| 396 | if($foo->error){ | ||
| 397 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 398 | } | ||
| 399 | |||
| 400 | =cut | ||
| 401 | |||
| 402 | sub getTemplate{ | ||
| 403 | my $self=$_[0]; | ||
| 404 | my $name=$_[1]; | ||
| 405 | |||
| 406 | if (!$self->errorblank){ | ||
| 407 | return undef; | ||
| 408 | } | ||
| 409 | |||
| 410 | #make sure a template name is specified | ||
| 411 | if ( ! defined( $name ) ){ | ||
| 412 | $self->{error}=9; | ||
| 413 | $self->{errorString}='No template name specified'; | ||
| 414 | $self->warn; | ||
| 415 | return undef; | ||
| 416 | } | ||
| 417 | |||
| 418 | #try to find it as a file | ||
| 419 | #also allow this to do the error checking as it is the same | ||
| 420 | my $file=$self->findTemplate($name); | ||
| 421 | if ($self->error) { | ||
| 422 | $self->warnString('findTemplate errored'); | ||
| 423 | return undef; | ||
| 424 | } | ||
| 425 | |||
| 426 | #the contents of the template to be returned | ||
| 427 | my $template; | ||
| 428 | |||
| 429 | #if we found a file, read it and return it | ||
| 430 | if (defined($file)) { | ||
| 431 | my $fh; | ||
| 432 | if ( ! open( $fh, '<', $file ) ) { | ||
| 433 | $self->{error}=5; | ||
| 434 | $self->{errorString}="Unable to open '".$file."'"; | ||
| 435 | $self->warn; | ||
| 436 | return undef; | ||
| 437 | } | ||
| 438 | $template=join('',<$fh>); | ||
| 439 | close $fh; | ||
| 440 | return $template; | ||
| 441 | } | ||
| 442 | |||
| 443 | #tries to fetch the default template | ||
| 444 | $template=$self->{defaults}->getTemplate($name); | ||
| 445 | if ( ! defined( $template ) ) { | ||
| 446 | $self->{error}=6; | ||
| 447 | $self->{errorString}='No default template'; | ||
| 448 | $self->warn; | ||
| 449 | return undef; | ||
| 450 | } | ||
| 451 | |||
| 452 | return $template; | ||
| 453 | } | ||
| 454 | |||
| 455 | =head2 getTemplateDefault | ||
| 456 | |||
| 457 | This finds a default template and then returns it. | ||
| 458 | |||
| 459 | One arguement is required and it is the template name. | ||
| 460 | |||
| 461 | my $template=$foo->getTemplate($templateName); | ||
| 462 | if($foo->error){ | ||
| 463 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 464 | } | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub getTemplateDefault{ | ||
| 469 | my $self=$_[0]; | ||
| 470 | my $name=$_[1]; | ||
| 471 | |||
| 472 | if (!$self->errorblank){ | ||
| 473 | return undef; | ||
| 474 | } | ||
| 475 | |||
| 476 | #make sure a template name is specified | ||
| 477 | if ( ! defined( $name ) ){ | ||
| 478 | $self->{error}=9; | ||
| 479 | $self->{errorString}='No template name specified'; | ||
| 480 | $self->warn; | ||
| 481 | return undef; | ||
| 482 | } | ||
| 483 | |||
| 484 | #tries to fetch the default template | ||
| 485 | my $template=$self->{defaults}->getTemplate($name); | ||
| 486 | if ( ! defined( $template ) ) { | ||
| 487 | $self->{error}=6; | ||
| 488 | $self->{errorString}='No default template'; | ||
| 489 | $self->warn; | ||
| 490 | return undef; | ||
| 491 | } | ||
| 492 | |||
| 493 | return $template; | ||
| 494 | } | ||
| 495 | |||
| 496 | =head2 listTemplates | ||
| 497 | |||
| 498 | This lists the various templates in the directory. | ||
| 499 | |||
| 500 | my @templates=$foo->listTemplates; | ||
| 501 | if($foo->error){ | ||
| 502 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 503 | } | ||
| 504 | |||
| 505 | =cut | ||
| 506 | |||
| 507 | sub listTemplates{ | ||
| 508 | my $self=$_[0]; | ||
| 509 | |||
| 510 | if (!$self->errorblank){ | ||
| 511 | return undef; | ||
| 512 | } | ||
| 513 | |||
| 514 | #make sure a directory has been set | ||
| 515 | if (!defined( $self->{dir} )) { | ||
| 516 | $self->{error}=2; | ||
| 517 | $self->{errorString}='No directory has been set yet'; | ||
| 518 | $self->warn; | ||
| 519 | return undef; | ||
| 520 | } | ||
| 521 | |||
| 522 | #checks if the directory is Toader directory or not | ||
| 523 | my $returned=$self->{isatd}->isaToaderDir( $self->{dir} ); | ||
| 524 | if (! $returned ) { | ||
| 525 | $self->{error}=3; | ||
| 526 | $self->{errorString}='"'.$self->{dir}.'" is no longer a Toader directory'; | ||
| 527 | $self->warn; | ||
| 528 | return undef; | ||
| 529 | } | ||
| 530 | |||
| 531 | #the directory to list | ||
| 532 | my $dir=$self->{dir}.'/.toader/templates/'; | ||
| 533 | |||
| 534 | #makes sure the template exists and if it does not, return only having the default | ||
| 535 | if (! -d $dir) { | ||
| 536 | return; | ||
| 537 | } | ||
| 538 | |||
| 539 | #lists each theme | ||
| 540 | my $dh; | ||
| 541 | if (opendir($dh, $dir )) { | ||
| 542 | $self->{error}='4'; | ||
| 543 | $self->{errorString}='Failed to open the directory "'.$dir.'"'; | ||
| 544 | $self->warn; | ||
| 545 | return undef; | ||
| 546 | } | ||
| 547 | my @templates=grep( { -f $dir.'/'.$_ } readdir($dh) ); | ||
| 548 | close($dh); | ||
| 549 | |||
| 550 | return \@templates; | ||
| 551 | } | ||
| 552 | |||
| 553 | =head2 listDefaultTemplates | ||
| 554 | |||
| 555 | This lists the various templates in the directory. | ||
| 556 | |||
| 557 | my @templates=$foo->listTemplates; | ||
| 558 | if($foo->error){ | ||
| 559 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 560 | } | ||
| 561 | |||
| 562 | =cut | ||
| 563 | |||
| 564 | sub listDefaultTemplates{ | ||
| 565 | my $self=$_[0]; | ||
| 566 | |||
| 567 | if (!$self->errorblank){ | ||
| 568 | return undef; | ||
| 569 | } | ||
| 570 | |||
| 571 | return $self->{defaults}->listTemplates; | ||
| 572 | } | ||
| 573 | |||
| 574 | =head2 remove | ||
| 575 | |||
| 576 | This removes a template from the current directory. | ||
| 577 | |||
| 578 | One argument is required and that is the name of the template. | ||
| 579 | |||
| 580 | $foo->remove( $name ); | ||
| 581 | if($foo->error){ | ||
| 582 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 583 | } | ||
| 584 | |||
| 585 | =cut | ||
| 586 | |||
| 587 | sub remove{ | ||
| 588 | my $self=$_[0]; | ||
| 589 | my $name=$_[1]; | ||
| 590 | |||
| 591 | if (!$self->errorblank){ | ||
| 592 | return undef; | ||
| 593 | } | ||
| 594 | |||
| 595 | #make sure a directory has been set | ||
| 596 | if (!defined( $self->{dir} )) { | ||
| 597 | $self->{error}=2; | ||
| 598 | $self->{errorString}='No directory has been set yet'; | ||
| 599 | $self->warn; | ||
| 600 | return undef; | ||
| 601 | } | ||
| 602 | |||
| 603 | #make sure a template name is specified | ||
| 604 | if ( ! defined( $name ) ){ | ||
| 605 | $self->{error}=9; | ||
| 606 | $self->{errorString}='No template name specified'; | ||
| 607 | $self->warn; | ||
| 608 | return undef; | ||
| 609 | } | ||
| 610 | |||
| 611 | #checks if the name is valid | ||
| 612 | my $returned=$self->templateNameCheck($name); | ||
| 613 | if (! $returned ) { | ||
| 614 | $self->{error}=4; | ||
| 615 | $self->{errorString}='"'.$name.'" is not a valid template name'; | ||
| 616 | $self->warn; | ||
| 617 | return undef; | ||
| 618 | } | ||
| 619 | |||
| 620 | #make sure the template is in this directory | ||
| 621 | if ( ! $self->templateInDir( $name ) ){ | ||
| 622 | $self->{error}=18; | ||
| 623 | $self->{errorString}='Template "'.$name.'" is not in "'. | ||
| 624 | $self->{dir}.'"'; | ||
| 625 | $self->warn; | ||
| 626 | return undef; | ||
| 627 | } | ||
| 628 | |||
| 629 | #the file in question | ||
| 630 | my $file=$self->{dir}.'/.toader/templates/'.$name; | ||
| 631 | |||
| 632 | #try to unlink the file | ||
| 633 | if ( ! unlink( $file ) ) { | ||
| 634 | $self->{error}=17; | ||
| 635 | $self->{errorString}='Unlink of "'.$file.'" failed'; | ||
| 636 | $self->warn; | ||
| 637 | return undef; | ||
| 638 | } | ||
| 639 | |||
| 640 | #if VCS is not usable, return here | ||
| 641 | if ( ! $self->{VCSusable} ){ | ||
| 642 | return 1; | ||
| 643 | } | ||
| 644 | |||
| 645 | #if it is not under VCS, we have nothing to do | ||
| 646 | my $underVCS=$self->{vcs}->underVCS($file); | ||
| 647 | if ( $self->{vcs}->error ){ | ||
| 648 | $self->{error}=14; | ||
| 649 | $self->{errorString}='Toader::VCS->underVCS errored. error="'. | ||
| 650 | $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"'; | ||
| 651 | $self->warn; | ||
| 652 | return undef; | ||
| 653 | } | ||
| 654 | if ( $underVCS ){ | ||
| 655 | return 1; | ||
| 656 | } | ||
| 657 | |||
| 658 | #delete it as if we reach here it is not under VCS and VCS is being used | ||
| 659 | $self->{vcs}->delete( $file ); | ||
| 660 | if ( $self->{vcs}->error ){ | ||
| 661 | $self->{error}=16; | ||
| 662 | $self->{errorString}='Toader::VCS->delete errored. error="'. | ||
| 663 | $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"'; | ||
| 664 | $self->warn; | ||
| 665 | return undef; | ||
| 666 | } | ||
| 667 | |||
| 668 | |||
| 669 | return 1; | ||
| 670 | } | ||
| 671 | |||
| 672 | =head2 set | ||
| 673 | |||
| 674 | This sets a template in the current directory. | ||
| 675 | |||
| 676 | Two arguments are required and those in order are the name | ||
| 677 | template and the template. | ||
| 678 | |||
| 679 | $foo->set( $name, $template ); | ||
| 680 | if($foo->error){ | ||
| 681 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 682 | } | ||
| 683 | |||
| 684 | =cut | ||
| 685 | |||
| 686 | sub set{ | ||
| 687 | my $self=$_[0]; | ||
| 688 | my $name=$_[1]; | ||
| 689 | my $template=$_[2]; | ||
| 690 | |||
| 691 | if (!$self->errorblank){ | ||
| 692 | return undef; | ||
| 693 | } | ||
| 694 | |||
| 695 | #make sure a directory has been set | ||
| 696 | if (!defined( $self->{dir} )) { | ||
| 697 | $self->{error}=2; | ||
| 698 | $self->{errorString}='No directory has been set yet'; | ||
| 699 | $self->warn; | ||
| 700 | return undef; | ||
| 701 | } | ||
| 702 | |||
| 703 | #make sure a template name is specified | ||
| 704 | if ( ! defined( $name ) ){ | ||
| 705 | $self->{error}=9; | ||
| 706 | $self->{errorString}='No template name specified'; | ||
| 707 | $self->warn; | ||
| 708 | return undef; | ||
| 709 | } | ||
| 710 | |||
| 711 | #checks if the name is valid | ||
| 712 | my $returned=$self->templateNameCheck($name); | ||
| 713 | if (! $returned ) { | ||
| 714 | $self->{error}=4; | ||
| 715 | $self->{errorString}='"'.$name.'" is not a valid template name'; | ||
| 716 | $self->warn; | ||
| 717 | return undef; | ||
| 718 | } | ||
| 719 | |||
| 720 | #make sure a template name is specified | ||
| 721 | if ( ! defined( $template ) ){ | ||
| 722 | $self->{error}=13; | ||
| 723 | $self->{errorString}='No template specified'; | ||
| 724 | $self->warn; | ||
| 725 | return undef; | ||
| 726 | } | ||
| 727 | |||
| 728 | #the file in question | ||
| 729 | my $file=$self->{dir}.'/.toader/templates/'.$name; | ||
| 730 | |||
| 731 | #write the template out | ||
| 732 | my $fh; | ||
| 733 | if ( ! open( $fh, '>', $file ) ){ | ||
| 734 | $self->{error}=5; | ||
| 735 | $self->{errorString}='Unable to open "'.$file.'" for writing'; | ||
| 736 | $self->warn; | ||
| 737 | return undef; | ||
| 738 | } | ||
| 739 | print $fh $template; | ||
| 740 | close $fh; | ||
| 741 | |||
| 742 | #if VCS is not usable, return here | ||
| 743 | if ( ! $self->{VCSusable} ){ | ||
| 744 | return 1; | ||
| 745 | } | ||
| 746 | |||
| 747 | #if it is not under VCS, we have nothing to do | ||
| 748 | my $underVCS=$self->{vcs}->underVCS($file); | ||
| 749 | if ( $self->{vcs}->error ){ | ||
| 750 | $self->{error}=14; | ||
| 751 | $self->{errorString}='Toader::VCS->underVCS errored. error="'. | ||
| 752 | $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"'; | ||
| 753 | $self->warn; | ||
| 754 | return undef; | ||
| 755 | } | ||
| 756 | if ( $underVCS ){ | ||
| 757 | return 1; | ||
| 758 | } | ||
| 759 | |||
| 760 | #add it as if we reach here it is not under VCS and VCS is being used | ||
| 761 | $self->{vcs}->add( $file ); | ||
| 762 | if ( $self->{vcs}->error ){ | ||
| 763 | $self->{error}=15; | ||
| 764 | $self->{errorString}='Toader::VCS->add errored. error="'. | ||
| 765 | $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"'; | ||
| 766 | $self->warn; | ||
| 767 | return undef; | ||
| 768 | } | ||
| 769 | |||
| 770 | return 1; | ||
| 771 | } | ||
| 772 | |||
| 773 | =head2 templateInDir | ||
| 774 | |||
| 775 | This checks if the template is in the current directory. | ||
| 776 | |||
| 777 | One argument is required and that is the name of the template. | ||
| 778 | |||
| 779 | $foo->templateInDir( $name ); | ||
| 780 | if($foo->error){ | ||
| 781 | warn('Error:'.$foo->error.': '.$foo->errorString); | ||
| 782 | } | ||
| 783 | |||
| 784 | =cut | ||
| 785 | |||
| 786 | sub templateInDir{ | ||
| 787 | my $self=$_[0]; | ||
| 788 | my $name=$_[1]; | ||
| 789 | |||
| 790 | if (!$self->errorblank){ | ||
| 791 | return undef; | ||
| 792 | } | ||
| 793 | |||
| 794 | #make sure a directory has been set | ||
| 795 | if (!defined( $self->{dir} )) { | ||
| 796 | $self->{error}=2; | ||
| 797 | $self->{errorString}='No directory has been set yet'; | ||
| 798 | $self->warn; | ||
| 799 | return undef; | ||
| 800 | } | ||
| 801 | |||
| 802 | #make sure a template name is specified | ||
| 803 | if ( ! defined( $name ) ){ | ||
| 804 | $self->{error}=9; | ||
| 805 | $self->{errorString}='No template name specified'; | ||
| 806 | $self->warn; | ||
| 807 | return undef; | ||
| 808 | } | ||
| 809 | |||
| 810 | #checks if the name is valid | ||
| 811 | my $returned=$self->templateNameCheck($name); | ||
| 812 | if (! $returned ) { | ||
| 813 | $self->{error}=4; | ||
| 814 | $self->{errorString}='"'.$name.'" is not a valid template name'; | ||
| 815 | $self->warn; | ||
| 816 | return undef; | ||
| 817 | } | ||
| 818 | |||
| 819 | #the file in question | ||
| 820 | my $file=$self->{dir}.'/.toader/templates/'.$name; | ||
| 821 | |||
| 822 | #checks if it exists | ||
| 823 | if ( -f $file ){ | ||
| 824 | return 1; | ||
| 825 | } | ||
| 826 | |||
| 827 | return 0; | ||
| 828 | } | ||
| 829 | |||
| 830 | =head2 templateNameCheck | ||
| 831 | |||
| 832 | This makes sure checks to make sure a template name is valid. | ||
| 833 | |||
| 834 | my $returned=$foo->templateNameCheck($name); | ||
| 835 | if ($returned){ | ||
| 836 | print "Valid\n"; | ||
| 837 | } | ||
| 838 | |||
| 839 | =cut | ||
| 840 | |||
| 841 | sub templateNameCheck{ | ||
| 842 | my $self=$_[0]; | ||
| 843 | my $name=$_[1]; | ||
| 844 | |||
| 845 | if (!$self->errorblank){ | ||
| 846 | return undef; | ||
| 847 | } | ||
| 848 | |||
| 849 | if (!defined($name)) { | ||
| 850 | return 0; | ||
| 851 | } | ||
| 852 | if ($name =~ /^ /) { | ||
| 853 | return 0; | ||
| 854 | } | ||
| 855 | if ($name =~ /\t/) { | ||
| 856 | return 0; | ||
| 857 | } | ||
| 858 | if ($name =~ /\n/) { | ||
| 859 | return 0; | ||
| 860 | } | ||
| 861 | if ($name =~ / $/) { | ||
| 862 | return 0; | ||
| 863 | } | ||
| 864 | |||
| 865 | return 1; | ||
| 866 | } | ||
| 867 | |||
| 868 | =head1 ERROR CODES | ||
| 869 | |||
| 870 | =head2 1, noDirSpecified | ||
| 871 | |||
| 872 | The specified directory is not a L<Toader> directory. | ||
| 873 | |||
| 874 | =head2 2, isaToaderDirErrored | ||
| 875 | |||
| 876 | No directory has been specified yet. | ||
| 877 | |||
| 878 | =head2 3, notAtoaderDir | ||
| 879 | |||
| 880 | The directory in question is no longer a toader directory. | ||
| 881 | |||
| 882 | =head2 4, invalidTemplateName | ||
| 883 | |||
| 884 | Not a valid template name. | ||
| 885 | |||
| 886 | =head2 5, openTemplateFailed | ||
| 887 | |||
| 888 | Unable to open the template file. | ||
| 889 | |||
| 890 | =head2 6, noDefaultTemplate | ||
| 891 | |||
| 892 | Unable to fetch the default template. It does not exist. | ||
| 893 | |||
| 894 | =head2 7, templateFillErrored | ||
| 895 | |||
| 896 | Errored filling out the template string. | ||
| 897 | |||
| 898 | =head2 8, templateStringUndef | ||
| 899 | |||
| 900 | Nothing specified for the template string. | ||
| 901 | |||
| 902 | =head2 9, templateNameUndef | ||
| 903 | |||
| 904 | Template name is not defined. | ||
| 905 | |||
| 906 | =head2 10, notAtoaderObj | ||
| 907 | |||
| 908 | The object in question is not a Toader object. | ||
| 909 | |||
| 910 | =head2 11, getVCSerrored | ||
| 911 | |||
| 912 | L<Toader>->getVCS errored. | ||
| 913 | |||
| 914 | =head2 12, VCSusableErrored | ||
| 915 | |||
| 916 | L<Toader::VCS>->usable errored. | ||
| 917 | |||
| 918 | =head2 13, noTemplateSpecified | ||
| 919 | |||
| 920 | Nothing specified for the data for a template. | ||
| 921 | |||
| 922 | =head2 14, underVCSerrored | ||
| 923 | |||
| 924 | L<Toader::VCS>->underVCS errored. | ||
| 925 | |||
| 926 | =head2 15, VCSaddErrored | ||
| 927 | |||
| 928 | L<Toader::VCS>->add errored. | ||
| 929 | |||
| 930 | =head2 16, VCSdeleteErrored | ||
| 931 | |||
| 932 | L<Toader::VCS>->delete errored. | ||
| 933 | |||
| 934 | =head2 17, unlinkFailed | ||
| 935 | |||
| 936 | Failed to unlink the template. | ||
| 937 | |||
| 938 | =head2 18, notInDir | ||
| 939 | |||
| 940 | The requested template is not in this Toader dir. | ||
| 941 | |||
| 942 | =head2 19, noToaderObj | ||
| 943 | |||
| 944 | No L<Toader> object is given. | ||
| 945 | |||
| 946 | =head1 AUTHOR | ||
| 947 | |||
| 948 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||
| 949 | |||
| 950 | =head1 BUGS | ||
| 951 | |||
| 952 | Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through | ||
| 953 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll | ||
| 954 | automatically be notified of progress on your bug as I make changes. | ||
| 955 | |||
| 956 | |||
| 957 | |||
| 958 | |||
| 959 | =head1 SUPPORT | ||
| 960 | |||
| 961 | You can find documentation for this module with the perldoc command. | ||
| 962 | |||
| 963 | perldoc Toader::Templates | ||
| 964 | |||
| 965 | |||
| 966 | You can also look for information at: | ||
| 967 | |||
| 968 | =over 4 | ||
| 969 | |||
| 970 | =item * RT: CPAN's request tracker | ||
| 971 | |||
| 972 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader> | ||
| 973 | |||
| 974 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 975 | |||
| 976 | L<http://annocpan.org/dist/Toader> | ||
| 977 | |||
| 978 | =item * CPAN Ratings | ||
| 979 | |||
| 980 | L<http://cpanratings.perl.org/d/Toader> | ||
| 981 | |||
| 982 | =item * Search CPAN | ||
| 983 | |||
| 984 | L<http://search.cpan.org/dist/Toader/> | ||
| 985 | |||
| 986 | =back | ||
| 987 | |||
| 988 | |||
| 989 | =head1 ACKNOWLEDGEMENTS | ||
| 990 | |||
| 991 | |||
| 992 | =head1 LICENSE AND COPYRIGHT | ||
| 993 | |||
| 994 | Copyright 2013 Zane C. Bowers-Hadley. | ||
| 995 | |||
| 996 | This program is free software; you can redistribute it and/or modify it | ||
| 997 | under the terms of either: the GNU General Public License as published | ||
| 998 | by the Free Software Foundation; or the Artistic License. | ||
| 999 | |||
| 1000 | See http://dev.perl.org/licenses/ for more information. | ||
| 1001 | |||
| 1002 | |||
| 1003 | =cut | ||
| 1004 | |||
| 1005 | 1; # End of Toader::Templates | ||