| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::TemplateCMD::Command::Print; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # Created on: 2008-03-26 13:43:32 | 
| 4 |  |  |  |  |  |  | # Create by:  ivanw | 
| 5 |  |  |  |  |  |  | # $Id$ | 
| 6 |  |  |  |  |  |  | # $Revision$, $HeadURL$, $Date$ | 
| 7 |  |  |  |  |  |  | # $Revision$, $Source$, $Date$ | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 1648 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 55 |  | 
| 10 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 45 |  | 
| 11 | 2 |  |  | 2 |  | 420 | use version; | 
|  | 2 |  |  |  |  | 1837 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 12 | 2 |  |  | 2 |  | 156 | use Carp qw/carp croak cluck confess longmess/; | 
|  | 2 |  |  |  |  | 15 |  | 
|  | 2 |  |  |  |  | 203 |  | 
| 13 | 2 |  |  | 2 |  | 589 | use List::MoreUtils qw/uniq/; | 
|  | 2 |  |  |  |  | 12202 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 14 | 2 |  |  | 2 |  | 2270 | use Data::Dumper qw/Dumper/; | 
|  | 2 |  |  |  |  | 6723 |  | 
|  | 2 |  |  |  |  | 122 |  | 
| 15 | 2 |  |  | 2 |  | 487 | use English qw/ -no_match_vars /; | 
|  | 2 |  |  |  |  | 1664 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 16 | 2 |  |  | 2 |  | 1268 | use Template; | 
|  | 2 |  |  |  |  | 18756 |  | 
|  | 2 |  |  |  |  | 69 |  | 
| 17 | 2 |  |  | 2 |  | 15 | use Template::Provider; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 39 |  | 
| 18 | 2 |  |  | 2 |  | 940 | use IPC::Open2; | 
|  | 2 |  |  |  |  | 9134 |  | 
|  | 2 |  |  |  |  | 125 |  | 
| 19 | 2 |  |  | 2 |  | 15 | use base qw/App::TemplateCMD::Command/; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 1192 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | our $VERSION     = version->new('0.6.10'); | 
| 22 |  |  |  |  |  |  | our @EXPORT_OK   = qw//; | 
| 23 |  |  |  |  |  |  | our %EXPORT_TAGS = (); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub process { | 
| 26 | 0 |  |  | 0 | 1 |  | my ($self, $cmd, %option) = @_; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 |  |  |  |  |  | my $template = shift @{$option{files}}; | 
|  | 0 |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my $args     = $cmd->conf_join( | 
| 30 | 0 |  | 0 |  |  |  | $cmd->conf_join( ( $cmd->config || {} ), ( $option{args} || {} ) ), | 
|  |  |  | 0 |  |  |  |  | 
| 31 |  |  |  |  |  |  | \%option | 
| 32 |  |  |  |  |  |  | ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 | 0 |  |  |  |  | confess "No template passed!\n" if !$template; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  |  | my $out = ''; | 
| 37 | 0 |  |  |  |  |  | $cmd->{template}->process( $template, $args, \$out ); | 
| 38 | 0 | 0 | 0 |  |  |  | warn  $cmd->{template}->error . "\n" if $cmd->{template}->error && $cmd->{template}->error !~ /^file error - .*: not a file$/; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 | 0 |  |  |  |  | if (!$out) { | 
| 41 | 0 |  |  |  |  |  | my @files = uniq sort map {$_->{file}} $cmd->list_templates(); | 
|  | 0 |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  |  | my @templates = grep { m{^$template [.] .+ $}xms } @files; | 
|  | 0 |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 | 0 |  |  |  |  | if (@templates) { | 
| 46 | 0 |  |  |  |  |  | $cmd->{template}->process( $templates[0], $args, \$out ); | 
| 47 | 0 | 0 | 0 |  |  |  | warn  $cmd->{template}->error . "\n" if $cmd->{template}->error && $cmd->{template}->error !~ /^file error - .*: not a file$/; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 | 0 |  |  |  |  | $out =~ s/^\0=__/__/gxms if $out; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 | 0 |  |  |  |  | if ( $option{args}{tidy} ) { | 
| 54 | 0 | 0 |  |  |  |  | if ( $option{args}{tidy} eq 'perl' ) { | 
| 55 | 0 |  |  |  |  |  | eval { require Perl::Tidy }; | 
|  | 0 |  |  |  |  |  |  | 
| 56 | 0 | 0 |  |  |  |  | if ($EVAL_ERROR) { | 
| 57 | 0 |  |  |  |  |  | warn "Perl::Tidy is not installed, carn't tidy perl code\n"; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | else { | 
| 60 | 0 |  |  |  |  |  | my $tidied; | 
| 61 | 0 |  |  |  |  |  | eval { | 
| 62 | 0 |  |  |  |  |  | local @ARGV; | 
| 63 | 0 |  |  |  |  |  | Perl::Tidy::perltidy( source => \$out, destination => \$tidied ); | 
| 64 | 0 |  |  |  |  |  | $out = $tidied; | 
| 65 |  |  |  |  |  |  | }; | 
| 66 | 0 | 0 |  |  |  |  | if ($EVAL_ERROR) { | 
| 67 | 0 |  |  |  |  |  | warn "perltidy errored with: $EVAL_ERROR\n"; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | else { | 
| 72 | 0 |  |  |  |  |  | warn "$option{args}{tidy}tidy"; | 
| 73 | 0 |  |  |  |  |  | my $pid = open2( my $fh_out, my $fh_in, "$option{args}{tidy}tidy" ); | 
| 74 | 0 |  |  |  |  |  | sleep 1; | 
| 75 | 0 |  |  |  |  |  | print {$fh_in} $out; | 
|  | 0 |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | sleep 1; | 
| 77 | 0 |  |  |  |  |  | $out = <$fh_out>; | 
| 78 | 0 |  |  |  |  |  | waitpid( $pid, 0 ); | 
| 79 | 0 |  |  |  |  |  | warn "exit status: " . $? >> 8; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 |  |  |  |  |  | return $out; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub help { | 
| 87 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | return <<"HELP"; | 
| 90 |  |  |  |  |  |  | $0 print [options] template [--out|-o file] [[-a|--args] key=value] | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | -a --args[=]str  Specify arguments to pass on to the template. The format of | 
| 93 |  |  |  |  |  |  | the arguments is key=value where key is the name of a template | 
| 94 |  |  |  |  |  |  | variable. Arguments can be specified without explicit using | 
| 95 |  |  |  |  |  |  | this option. | 
| 96 |  |  |  |  |  |  | -o --out[=]file  Specify a file to wright the out put to | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Standard arguments: | 
| 99 |  |  |  |  |  |  | tidy=command     Specify tidy program which will post process the output. | 
| 100 |  |  |  |  |  |  | If command equals perl the Perl::Tidy module is used directly | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | This command processes the template for saving or inserting/appending to | 
| 103 |  |  |  |  |  |  | another file. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | The arguments --args parameter passes parameters onto the individual templates | 
| 106 |  |  |  |  |  |  | along with any variables set in the configuration file. | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Also see: | 
| 109 |  |  |  |  |  |  | $0 help templates | 
| 110 |  |  |  |  |  |  | $0 help config | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | HELP | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | 1; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | __END__ |