1 # Copyright 2002-2007 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: substitute_file.coretag,v 1.4 2007-03-30 23:40:54 pajamian Exp $
10 UserTag substitute_file Order file
11 UserTag substitute_file addAttr
12 UserTag substitute_file hasEndTag
13 UserTag substitute_file Version $Revision: 1.4 $
14 UserTag substitute_file Routine <<EOR
15 ## This is a stupid thing to make 5.6.1 and File::Copy
16 ## compatible with Safe
19 require File::Basename;
20 import File::Basename 'basename';
21 package Vend::Interpolate;
23 my ($file, $opt, $replace) = @_;
26 $::Scratch->{ui_failure} = errmsg(@args);
30 return $die->("substitute_file - %s: file does not exist", $file)
32 return $die->("substitute_file - %s: file not writeable", $file)
36 $opt->{begin} = '<!--+\s*begin\s+content\s*--+>';
37 $opt->{end} = '<!--+\s*end\s+content\s*--+>';
38 $opt->{newline} = 1 if ! defined $opt->{newline};
42 $opt->{begin} = '\[(?:tmp|seti?)\s*' . $opt->{scratch} . '\]';
43 $opt->{end} = '\[/(?:tmp|seti?)\]';
44 $opt->{greedy} = 0 if ! defined $opt->{greedy};
45 $opt->{newline} = 1 if ! defined $opt->{newline};
48 if (! length($opt->{begin}) or ! length($opt->{end})) {
49 return $die->("missing begin or end marker");
52 my $bak = POSIX::tmpnam();
53 File::Copy::copy($file, $bak)
55 "substitute_file - %s: unable to backup to %s",
58 my $data = Vend::Util::readfile($file);
59 return $die->("substitute_file - %s: file has no data", $file)
63 if(defined $opt->{greedy} and ! Vend::Util::is_yes($opt->{greedy}) ) {
64 $exist = $opt->{newline} ? '[\s\S]*?' : '.*?';
67 $exist = $opt->{newline} ? '[\s\S]*' : '.*';
70 my $begin = $opt->{begin};
71 my $end = $opt->{end};
75 my ($begin, $replace, $end) = @_;
76 return $replace if $opt->{replace};
77 return $begin . $replace . $end;
80 if($opt->{case} and $opt->{global}) {
81 $subbed = $data =~ s{($begin)$exist($end)}{$sub->($1, $replace, $2)}ge;
83 elsif($opt->{global}) {
84 $subbed = $data =~ s{($begin)$exist($end)}{$sub->($1, $replace, $2)}ige;
87 $subbed = $data =~ s{($begin)$exist($end)}{$sub->($1, $replace, $2)}e;
90 $subbed = $data =~ s{($begin)$exist($end)}{$sub->($1, $replace, $2)}ie;
94 open(SUBFILE, ">$file")
96 "substitute_file: cannot write %s, backup in %s",
101 "substitute_file: error writing %s, backup in %s",
106 "substitute_file: error closing %s, backup in %s",