5 use List::Util qw(reduce);
 
   6 use List::MoreUtils qw(zip);
 
   8 use constant DEBUG => 0;
 
  10 unless ( caller(0) ) {
 
  11   pod2usage(2) unless @ARGV;
 
  16   my $file = shift or return;
 
  18   my $contents = do { local( @ARGV, $/ ) = $file ; <> }
 
  19     or die "cannot read file";
 
  22     "<translate>"  => "[% '",
 
  23     "</translate>" => "' | \$T8 %]",
 
  29   my $inline_counter = 0;
 
  31   # now replace <translate> with [% '
 
  32   # and </translate> with ' | $T8 %]
 
  33   while ($contents =~ m# ( < /? translate> | \[% | %\] ) #xg) {
 
  35     my $pos    = pos $contents;
 
  39       DEBUG && warn "entering [% block %] at pos $pos";
 
  44       DEBUG && warn "leaving [% block %] at pos $pos";
 
  49       $inline_counter++ if $match eq '<translate>';
 
  53     DEBUG && warn "found token $match at pos $pos";
 
  55     my $sub_by = $substitutions{$match};
 
  58       DEBUG && warn "found token $& but got no substitute";
 
  62     die "unbalanced tokens - two times '$match' in file $file"
 
  63       if $last_match eq $match;
 
  68     # alter string. substr is faster than s/// for strings of this size.
 
  69     substr $contents, $-[0], $+[0] - $-[0], $sub_by;
 
  71     # set match pos for m//g matching on the altered string.
 
  72     pos $contents = $-[0] + length $sub_by;
 
  75   warn "found $inline_counter occurances of inline translates in file $file $/"
 
  78   exit 0 unless $num_matches;
 
  80   die "unbalanced tokens in file $file" if $num_matches % 2;
 
  82   if ($contents !~ m/\[%-? USE T8 %\]/) {
 
  83     $contents = "[%- USE T8 %]$/" . $contents;
 
  89     open my $fh, ">$file" or die "can't write $file $!";
 
 100 migrate_template_to_t8.pl - helper script to migrate templates to T8 module
 
 105   scripts/migrate_template_to_t8.pl <file>
 
 108   for file in `find templates | grep master\.html`;
 
 109     do scripts/migrate_template_to_t8.pl $file;
 
 113   require "scripts/migrate_template_to_t8.pl";
 
 118 This script will do the following actions in a template file
 
 124 Change every occurance of C<<< <translate>Text</translate> >>> to C<<< [%
 
 129 Add [%- USE T8 %] at the top if something needs to be translated
 
 133 Note! This script is written to help with the process of migrating old
 
 134 templates. It is assumed that anyone working on Lx-Office is working with a
 
 135 version control system. This script will change your files. You have been
 
 138 Due to the nature of the previous locale system, it is not easily possible to
 
 139 migrate translates in other template blocks. As of this writing this is used in
 
 140 about 20 occurances throughout the code. If such a construct is found, a
 
 141 warning will be generated. lib uses of this will have to trap the warning.
 
 145 =head2 found I<NUM> occurances of inline translates in file I<FILE>
 
 147 If a processed file has <translate> blocks in template blocks, these will be
 
 148 ignored.  This warning is thrown at the end of processing.
 
 150 =head2 unbalanced tokens in file I<FILE>
 
 152 The script could not resolve pairs of <translate> </translate>s. The file will
 
 153 not be changed in this case.
 
 157 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>