From 161e7d9a0c48d4aaedf1a9450eb63b4a35555fba Mon Sep 17 00:00:00 2001 From: Will Stokes Date: Mon, 23 Aug 2010 16:04:28 +1200 Subject: [PATCH] Draft script to 'convert' HTML:Toolkit syntax in templates to Template::Toolkit. Will need human to go through most if not all templates (basically those with a loop) and fix up. Makes no attempt to fix horrendous formatting. Branch: html-template-to-template-toolkit Signed-off-by: Chris Cormack --- .../html-template-to-template-toolkit.pl | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100755 installer/html-template-to-template-toolkit.pl diff --git a/installer/html-template-to-template-toolkit.pl b/installer/html-template-to-template-toolkit.pl new file mode 100755 index 0000000000..3efdb6a07b --- /dev/null +++ b/installer/html-template-to-template-toolkit.pl @@ -0,0 +1,147 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Carp; +use Data::Dumper; + +use Getopt::Long; +use File::Basename; +use File::Copy; + +my $help_msg = < \$KOHA_ROOT, + "type|t=s" => \$tmpl_extn_match, + "copyall|c" => \$copy_other_files, + "file|f=s" => \@template_files, # array of filenames + "verbose+" => \$verbose, # incremental flag +) or die $help_msg; + +if ( ! $KOHA_ROOT || ! -d $KOHA_ROOT ) { + croak "Koha root not passed or is not correct."; +} +if ( ! -d "$KOHA_ROOT/$tmpl_in_dir" ) { + croak "Cannot find template dir ($tmpl_in_dir)"; +} + +# Attempt to create koha-tt dir.. +if ( ! -d "$KOHA_ROOT/$tmpl_out_dir" ) { + mkdir("$KOHA_ROOT/$tmpl_out_dir") #, '0755' + or croak "Cannot create $tmpl_out_dir directory in $KOHA_ROOT: $!"; +} + +# Obtain list of files to process - go recursively through tmpl_in_dir and subdirectories.. +unless ( scalar(@template_files) ) { + @template_files = mirror_template_dir_structure_return_files("$KOHA_ROOT/$tmpl_in_dir", "$tmpl_extn_match"); +} +foreach my $file (@template_files) { + (my $new_path = $file) =~ s/$tmpl_in_dir/$tmpl_out_dir/; + $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ ); + + open my $ITMPL, '<', $file or croak "Can't open $file for input: $!"; + open my $OTT, '>', $new_path or croak "Can't open $new_path for output: $!"; + + # Slurp in input file.. + my $input_tmpl = do { local $/; <$ITMPL> }; + close $ITMPL; + + # Process.. + # variables + $input_tmpl =~ s//[% $1 %]/ig; + # if and unless blocks + $input_tmpl =~ s//[% IF ( $1 ) %]/ig; + $input_tmpl =~ s//[% IF ( $1 ) %]/ig; + $input_tmpl =~ s//[% ELSE %]/ig; + $input_tmpl =~ s//[% END %]/ig; + $input_tmpl =~ s//[% END %]/ig; + # includes + $input_tmpl =~ s//[% INCLUDE '$1' %]/ig; + $input_tmpl =~ s//[% INCLUDE $1 %]/ig; + + if ( $input_tmpl =~ m//i ) { + push(@files_w_tmpl_loops, $new_path); + } + + $input_tmpl =~ s//"[% FOREACH ".substr($1, 0 , -1)." IN (".$1.") %]"/ieg; + $input_tmpl =~ s//[% END %]/ig; + + # Write out.. + print $OTT $input_tmpl; + close $OTT; +} + +if ( scalar(@files_w_tmpl_loops) && $verbose ) { + print "\nThese files contain TMPL_LOOPs that need double checking:\n"; + foreach my $file (@files_w_tmpl_loops) { + print "$file\n"; + } +} + +## SUB-ROUTINES ## + +# Create new directory structure and return list of template files +sub mirror_template_dir_structure_return_files { + my($dir, $type) = @_; + + my @files = (); + if ( opendir(DIR, $dir) ) { + my @dirent = readdir DIR; # because DIR is shared when recursing + closedir DIR; + for my $dirent (@dirent) { + my $path = "$dir/$dirent"; + if ( $dirent =~ /^\./ ) { + ; + } + elsif ( -f $path ) { + (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/; + $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ ); + if ( !defined $type || $dirent =~ /\.(?:$type)$/) { + push(@files, $path); + } + elsif ( $copy_other_files ) { + copy($path, $new_path) + or croak "Failed to copy $path to $new_path: $!"; + } + } + elsif ( -d $path ) { + (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/; + $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ ); + if ( ! -d $new_path ) { + mkdir($new_path) #, '0755' + or croak "Failed to create " . $new_path ." directory: $!"; + } + my @sub_files = mirror_template_dir_structure_return_files($path, $type); + push(@files, @sub_files) if ( scalar(@sub_files) ); + } + } + } else { + warn("Cannot open $dir: $! ... skipping"); + } + + return @files; +} -- 2.39.2