13 This script does a first-cut conversion of koha HTML::Template template files (.tmpl).
14 It creates a mirror of koha-tmpl called koha-tt where converted files will be placed.
15 By default all files will be converted: use the --file (-f) argument to specify
16 individual files to process.
19 --koharoot (-r): Root directory of koha installation.
20 --type (-t): template file extenstions to match
21 (defaults to tmpl|inc|xsl).
22 --copyall (-c): Also copy across all files in template directory
23 --file (-f): specify individual files to process
24 --debug (-d): output more information.
27 my $tmpl_in_dir = 'koha-tmpl';
28 my $tmpl_out_dir = 'koha-tt';
32 my $tmpl_extn_match = "tmpl|inc|xsl"; # Type match defaults to *.tmpl plus *.inc if not specified
33 my $copy_other_files = 0;
35 my @files_w_tmpl_loops;
38 "koharoot=s" => \$KOHA_ROOT,
39 "type|t=s" => \$tmpl_extn_match,
40 "copyall|c" => \$copy_other_files,
41 "file|f=s" => \@template_files, # array of filenames
42 "verbose+" => \$verbose, # incremental flag
45 if ( ! $KOHA_ROOT || ! -d $KOHA_ROOT ) {
46 croak "Koha root not passed or is not correct.";
48 if ( ! -d "$KOHA_ROOT/$tmpl_in_dir" ) {
49 croak "Cannot find template dir ($tmpl_in_dir)";
52 # Attempt to create koha-tt dir..
53 if ( ! -d "$KOHA_ROOT/$tmpl_out_dir" ) {
54 mkdir("$KOHA_ROOT/$tmpl_out_dir") #, '0755'
55 or croak "Cannot create $tmpl_out_dir directory in $KOHA_ROOT: $!";
58 # Obtain list of files to process - go recursively through tmpl_in_dir and subdirectories..
59 unless ( scalar(@template_files) ) {
60 @template_files = mirror_template_dir_structure_return_files("$KOHA_ROOT/$tmpl_in_dir", "$tmpl_extn_match");
62 foreach my $file (@template_files) {
63 (my $new_path = $file) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
64 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
66 open my $ITMPL, '<', $file or croak "Can't open $file for input: $!";
67 open my $OTT, '>', $new_path or croak "Can't open $new_path for output: $!";
69 # Slurp in input file..
70 my $input_tmpl = do { local $/; <$ITMPL> };
75 $input_tmpl =~ s/<!--\s*TMPL_VAR\s+NAME="(.*?)"\s*-->/[% $1 %]/ig;
76 # if and unless blocks
77 $input_tmpl =~ s/<!--\s*TMPL_IF\s+NAME="(.*?)"\s*-->/[% IF ( $1 ) %]/ig;
78 $input_tmpl =~ s/<!--\s*TMPL_UNLESS\s+NAME="(.*?)"\s*-->/[% IF ( $1 ) %]/ig;
79 $input_tmpl =~ s/<!--\s*TMPL_ELSE\s*-->/[% ELSE %]/ig;
80 $input_tmpl =~ s/<!--\s*\/TMPL_IF\s*-->/[% END %]/ig;
81 $input_tmpl =~ s/<!--\s*\/TMPL_UNLESS\s*-->/[% END %]/ig;
83 $input_tmpl =~ s/<!--\s*TMPL_INCLUDE\s+NAME="(.*?\.inc)"\s*-->/[% INCLUDE '$1' %]/ig;
84 $input_tmpl =~ s/<!--\s*TMPL_INCLUDE\s+NAME="(.*?)"\s*-->/[% INCLUDE $1 %]/ig;
86 if ( $input_tmpl =~ m/<!--[\s\/]*TMPL_LOOP\s*-->/i ) {
87 push(@files_w_tmpl_loops, $new_path);
90 $input_tmpl =~ s/<!--\s*TMPL_LOOP\s+NAME="(.*?)"\s*-->/"[% FOREACH ".substr($1, 0 , -1)." IN (".$1.") %]"/ieg;
91 $input_tmpl =~ s/<!--\s*\/TMPL_LOOP\s*-->/[% END %]/ig;
94 print $OTT $input_tmpl;
98 if ( scalar(@files_w_tmpl_loops) && $verbose ) {
99 print "\nThese files contain TMPL_LOOPs that need double checking:\n";
100 foreach my $file (@files_w_tmpl_loops) {
107 # Create new directory structure and return list of template files
108 sub mirror_template_dir_structure_return_files {
109 my($dir, $type) = @_;
112 if ( opendir(DIR, $dir) ) {
113 my @dirent = readdir DIR; # because DIR is shared when recursing
115 for my $dirent (@dirent) {
116 my $path = "$dir/$dirent";
117 if ( $dirent =~ /^\./ ) {
121 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
122 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
123 if ( !defined $type || $dirent =~ /\.(?:$type)$/) {
126 elsif ( $copy_other_files ) {
127 copy($path, $new_path)
128 or croak "Failed to copy $path to $new_path: $!";
132 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
133 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
134 if ( ! -d $new_path ) {
135 mkdir($new_path) #, '0755'
136 or croak "Failed to create " . $new_path ." directory: $!";
138 my @sub_files = mirror_template_dir_structure_return_files($path, $type);
139 push(@files, @sub_files) if ( scalar(@sub_files) );
143 warn("Cannot open $dir: $! ... skipping");