Skip to content

Commit

Permalink
Add force option to prevent overwrite template
Browse files Browse the repository at this point in the history
  • Loading branch information
keedi committed Dec 7, 2011
1 parent 24b1331 commit 0b77a89
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions lib/OpenDocument/Template/Util.pm
Expand Up @@ -21,7 +21,8 @@ sub update_template {


my %params = ( my %params = (
output_dir => $ot->template_dir, output_dir => $ot->template_dir,
prefix => qr/(xxx|yyy|zzz)\./, prefix => q{},
force => 0,
@_, @_,
); );


Expand Down Expand Up @@ -50,32 +51,69 @@ sub update_template {
$tidy->write; $tidy->write;


my $text = read_file($file); my $text = read_file($file);
my $regexp = $params{prefix}; if ( $params{prefix} ) {
$text =~ s/${regexp}\w+/[% $& | xml %]/g; my $regexp = qr/$params{prefix}/;
write_file( catfile($output_dir, $file), $text ); $text =~ s/${regexp}\w+/[% $& | xml %]/g;
}

my $dest = catfile($output_dir, $file);
if ( -f $dest ) {
if ($params{force}) {
write_file( $dest, $text ) if $params{force};
}
else {
warn "file is already exists. use --force option: $dest\n";
}
}
else {
write_file( $dest, $text );
}
} }
} }

return 1;
} }


1; 1;
__END__ __END__
=pod =pod
=method update_template =method update_template( $ot, %params )
update template. update template.
=over
=item $ot
OpenDocument::Template object
=item prefix
match rule to convert for Template Toolkit variable
=item output_dir
directory for generated(updated) file
=item force
force overwrite if file is already existed
=back
Example:
my $ot = OpenDocument::Template->new( my $ot = OpenDocument::Template->new(
config => 'dcf.yml', config => 'addressbook.yml',
template_dir => 'templates', template_dir => 'template',
src => 'dcf-template.odt', src => 'addressbook.odt',
dest => 'dcf.odt', dest => 'addressbook-template.odt',
); );
OpenDocument::Template::Util->update_template( OpenDocument::Template::Util->update_template(
$ot, $ot,
prefix => qr/(xxx|yyy)\./, prefix => qr/(meta|person)\./,
output_dir => 'result', ) or "failed to update template\n";
);
=cut =cut

0 comments on commit 0b77a89

Please sign in to comment.