forgot to account for fullstop in pattern
[koha.git] / installer / html-template-to-template-toolkit.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Carp;
6 use Data::Dumper;
7
8 use Getopt::Long;
9 use File::Basename;
10 use File::Copy;
11
12 my $help_msg = <<EOH;
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.
17
18 Options:
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.
25 EOH
26
27 my $tmpl_in_dir      = 'koha-tmpl';
28 my $tmpl_out_dir     = 'koha-tt';
29
30 # Arguments:
31 my $KOHA_ROOT;
32 my $tmpl_extn_match  = "tmpl|inc|xsl"; # Type match defaults to *.tmpl plus *.inc if not specified
33 my $copy_other_files = 0;
34 my @template_files;
35 my @files_w_tmpl_loops;
36 my $verbose          = 0;
37 GetOptions (
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
43 ) or die $help_msg;
44
45 if ( ! $KOHA_ROOT || ! -d $KOHA_ROOT ) {
46     croak "Koha root not passed or is not correct.";
47 }
48 if ( ! -d "$KOHA_ROOT/$tmpl_in_dir" ) {
49     croak "Cannot find template dir ($tmpl_in_dir)";
50 }
51
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: $!";
56 }
57
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");
61 }
62 foreach my $file (@template_files) {
63     (my $new_path = $file) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
64     $new_path =~ s/\.tmpl/.tt/;
65     $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
66
67     open my $ITMPL, '<', $file or croak "Can't open $file for input: $!";
68     open my $OTT, '>', $new_path or croak "Can't open $new_path for output: $!";
69
70     # Slurp in input file..
71 ##    my $input_tmpl = do { local $/; <$ITMPL> };
72 ##    close $ITMPL;
73
74     # allows 'proper' handling of for loop scope
75     my @cur_scope = ("");
76     my @cur_scope_count = (0);
77
78     for my $input_tmpl(<$ITMPL>){
79     my $for_loop_found = 0;
80
81     # handle poorly names variable such as f1!, f1+, f1-, f1| and mod
82     $input_tmpl =~ s/"(\w+)\|"/"$1pipe"/ig;
83     $input_tmpl =~ s/"(\w+)\+"/"$1plus"/ig;
84     $input_tmpl =~ s/"(\w+)\-"/"$1minus"/ig;
85     $input_tmpl =~ s/"(\w+)!"/"$1exclamation"/ig;
86     $input_tmpl =~ s/"(\w+),(\w+)"/"$1comma$2"/ig;
87     $input_tmpl =~ s/NAME="mod"/NAME="modname"/ig;
88     # handle 'naked' TMPL_VAR "parameter" by turning them into what they should be, TMPL_VAR NAME="parameter"
89     $input_tmpl =~ s/TMPL_VAR\s+"(\w+)"/TMPL_VAR NAME="$1"/ig;
90     # make an end (ESCAPE NAME DEFAULT) into a ned (NAME ESCAPE DEFAULT)
91     $input_tmpl =~ s/ESCAPE="(\w+?)"\s+NAME=['"](\w+?)['"]\s+DEFAULT=['"](.+?)['"]/NAME="$2" ESCAPE="$1" DEFAULT="$3"/ig;
92
93     # Process..
94     # NB: if you think you're seeing double, you probably are, *some* (read:most) patterns appear twice: once with quotations marks, once without.
95     #     trying to combine them into a single pattern proved troublesome as a regex like ['"]?(.*?)['"]? was causing problems and fixing the problem caused (alot) more complex regex
96
97     # variables
98     $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s+ESCAPE=['"](\w*?)['"]\s+DEFAULT=['"]?(.*?)['"]?\s*-*>/[% DEFAULT $cur_scope[-1]$1="$3" |$2 %]/ig;
99     $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $cur_scope[-1]$1 |$2 %]/ig;
100     $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?(\w*?)\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $cur_scope[-1]$1 |$2 %]/ig;
101     $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+ESCAPE=['"]?(\w*?)['"]?\s+NAME\s?=\s?['"]?([\w-]*?)['"]?\s*-*>/[% $cur_scope[-1]$2 |$1 %]/ig;
102     $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?(\w*?)['"]?\s+DEFAULT=['"](.*?)['"]\s*-*>/[% DEFAULT $cur_scope[-1]$1="$2" %]/ig; # if a value being assigned is wrapped in quotes, keep them intact
103     $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s+DEFAULT=(.*?)\s*-*>/[% DEFAULT $cur_scope[-1]$1=$2 %]/ig;
104     $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s*-*>/[% $cur_scope[-1]$1 %]/ig;
105     $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% $1 %]/ig;     # TMPL_VAR NAME and TMPL_VAR EXPR are logically equiv, see http://search.cpan.org/~samtregar/HTML-Template-Expr-0.07/Expr.pm
106     $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?(.*?)\s*-*>/[% $1 %]/ig;
107
108     # if, elseif and unless blocks
109     $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% IF ( $1 ) %]/ig;
110     $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?(.*?)\s*-*>/[% IF ( $1 ) %]/ig;
111     $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
112     $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?(\w*?)\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
113     $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+['"](.*?)['"]\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
114     $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+([\w\s]*?)\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
115
116     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% ELSIF ( $1 ) %]/ig;
117     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+EXPR\s?=\s?(.*?)\s*-*>/[% ELSIF ( $1 ) %]/ig;
118     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+NAME\s?=\s?['"](\w*?)['"]\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
119     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+NAME\s?=\s?(\w*?)\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
120     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+['"](\w*?)['"]\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
121     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+(\w*?)\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
122
123     $input_tmpl =~ s/<[!-]*\s*TMPL_ELSE\s*-*>/[% ELSE %]/ig;
124     $input_tmpl =~ s/<[!-]*\s*\/TMPL_IF\s*-*>/[% END %]/ig;
125
126     $input_tmpl =~ s/<[!-]*\s*TMPL_UNLESS\s+NAME\s?=\s?['"]?(\w*?)['"]?\s*-*>/[% UNLESS ( $cur_scope[-1]$1 ) %]/ig;
127     $input_tmpl =~ s/<[!-]*\s*\/TMPL_UNLESS\s*-*>/[% END %]/ig;
128     # includes
129     $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?\.inc)"\s*-*>/[% INCLUDE '$1' %]/ig;
130     $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?)"\s*-*>/[% INCLUDE $1 %]/ig;
131
132     if ($input_tmpl =~ m/<[!-]*\s*TMPL_LOOP/i ){
133         $for_loop_found = 1;
134     }
135
136     $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?['"](?<SCOPE>.*?)['"]\s*-*>/"[% FOREACH " . substr($+{SCOPE}, 0, -1) . " IN $cur_scope[-1]$1 %]"/ieg;
137     $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?(?<SCOPE>.*?)\s*-*>/"[% FOREACH " . substr($+{SCOPE}, 0, -1) . " IN $cur_scope[-1]$1 %]"/ieg;
138
139     # handle for loop scope
140     if($for_loop_found){
141         my $scope = substr($+{SCOPE}, 0, -1) . ".";
142         push(@cur_scope, $scope);
143         $for_loop_found = 0;
144     }
145
146     # handle loops
147     if ( $input_tmpl =~ m/<!--[\s\/]*TMPL_LOOP\s*-->/i ) {
148         push(@files_w_tmpl_loops, $new_path);
149         pop(@cur_scope);
150     }
151
152     $input_tmpl =~ s/<[!-]*\s*\/TMPL_LOOP\s*-*>/[% END %]/ig;
153
154     # misc 'patches'
155     $input_tmpl =~ s/\seq\s/ == /ig;
156     $input_tmpl =~ s/HTML/html/ig;
157     $input_tmpl =~ s/URL/url/ig;
158     $input_tmpl =~ s/\w*\.__first__/loop.first/ig;
159     $input_tmpl =~ s/\w*\.__last__/loop.last/ig;
160     $input_tmpl =~ s/\w*\.__odd__/loop.odd/ig;
161     $input_tmpl =~ s/\w*\.__even__/loop.even/ig;
162     $input_tmpl =~ s/\w*\.__counter__/loop.count/ig; #loop.count gives the range (0..max) whereas loop.index gives the range (1..max+1), __counter__ is unknown
163
164     #hack to get around lack of javascript filter
165     $input_tmpl =~ s/\|JS/|replace("'", "\\'") |replace('"', '\\"') |replace('\\n', '\\\\n') |replace('\\r', '\\\\r')/ig;
166     
167     # Write out..
168     print $OTT $input_tmpl;
169     }
170     close $OTT;
171 }
172
173 if ( scalar(@files_w_tmpl_loops) && $verbose ) {
174     print "\nThese files contain TMPL_LOOPs that need double checking:\n";
175     foreach my $file (@files_w_tmpl_loops) {
176         print "$file\n";
177     }
178 }
179
180 ## SUB-ROUTINES ##
181
182 # Create new directory structure and return list of template files
183 sub mirror_template_dir_structure_return_files {
184     my($dir, $type) = @_;
185
186     my @files = ();
187     if ( opendir(DIR, $dir) ) {
188         my @dirent = readdir DIR;   # because DIR is shared when recursing
189         closedir DIR;
190         for my $dirent (@dirent) {
191             my $path = "$dir/$dirent";
192             if ( $dirent =~ /^\./ ) {
193               ;
194             }
195             elsif ( -f $path ) {
196                 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
197                 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
198                 if ( !defined $type || $dirent =~ /\.(?:$type)$/) {
199                     push(@files, $path);
200                 }
201                 elsif ( $copy_other_files ) {
202                     copy($path, $new_path)
203                       or croak "Failed to copy $path to $new_path: $!";
204                 }
205             }
206             elsif ( -d $path ) {
207                 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
208                 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
209                 if ( ! -d $new_path ) {
210                     mkdir($new_path) #, '0755'
211                       or croak "Failed to create " . $new_path ." directory: $!";
212                 }
213                 my @sub_files = mirror_template_dir_structure_return_files($path, $type);
214                 push(@files, @sub_files) if ( scalar(@sub_files) );
215             }
216         }
217     } else {
218         warn("Cannot open $dir: $! ... skipping");
219     }
220
221     return @files;
222 }