Bug 23204: (RM follow-up) Use Koha::Script
[koha.git] / installer / html-template-to-template-toolkit.pl
blob60cd951dd174aa9a62c0f8955cbf5346e59fb403
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Carp;
5 use Data::Dumper;
7 use Getopt::Long;
8 use File::Basename;
9 use File::Copy;
11 my $help_msg = <<EOH;
12 This script does a first-cut conversion of koha HTML::Template template files (.tmpl).
13 It creates a mirror of koha-tmpl called koha-tt where converted files will be placed.
14 By default all files will be converted: use the --file (-f) argument to specify
15 individual files to process.
17 Options:
18 --koharoot (-r): Root directory of koha installation.
19 --type (-t): template file extenstions to match
20 (defaults to tmpl|inc|xsl).
21 --copyall (-c): Also copy across all files in template directory
22 --file (-f): specify individual files to process
23 --debug (-d): output more information.
24 EOH
26 my $tmpl_in_dir = 'koha-tmpl';
27 my $tmpl_out_dir = 'koha-tt';
29 # template toolkit variables NOT to scope, in other words, variables that need to remain global (case sensitive)
30 my @globals = ("themelang","JacketImages","OPACAmazonCoverImages","GoogleJackets","BakerTaylorEnabled",
31 "SyndeticsEnabled", "OpacRenewalAllowed", "item_level_itypes","noItemTypeImages",
32 "virtualshelves", "RequestOnOpac", "COinSinOPACResults", "OPACXSLTResultsDisplay",
33 "OPACItemsResultsDisplay", "LibraryThingForLibrariesID", "opacuserlogin", "TagsEnabled",
34 "TagsShowOnList", "TagsInputOnList","loggedinusername","opacbookbag",
35 "OPACAmazonEnabled", "SyndeticsCoverImages","using_https");
37 # Arguments:
38 my $KOHA_ROOT;
39 my $tmpl_extn_match = "tmpl|inc|xsl"; # Type match defaults to *.tmpl plus *.inc if not specified
40 my $copy_other_files = 0;
41 my @template_files;
42 my @files_w_tmpl_loops;
43 my $verbose = 0;
44 GetOptions (
45 "koharoot=s" => \$KOHA_ROOT,
46 "type|t=s" => \$tmpl_extn_match,
47 "copyall|c" => \$copy_other_files,
48 "file|f=s" => \@template_files, # array of filenames
49 "verbose+" => \$verbose, # incremental flag
50 ) or die $help_msg;
52 if ( ! $KOHA_ROOT || ! -d $KOHA_ROOT ) {
53 croak "Koha root not passed or is not correct.";
55 if ( ! -d "$KOHA_ROOT/$tmpl_in_dir" ) {
56 croak "Cannot find template dir ($tmpl_in_dir)";
59 # Attempt to create koha-tt dir..
60 if ( ! -d "$KOHA_ROOT/$tmpl_out_dir" ) {
61 mkdir("$KOHA_ROOT/$tmpl_out_dir") #, '0755'
62 or croak "Cannot create $tmpl_out_dir directory in $KOHA_ROOT: $!";
65 # Obtain list of files to process - go recursively through tmpl_in_dir and subdirectories..
66 unless ( scalar(@template_files) ) {
67 @template_files = mirror_template_dir_structure_return_files("$KOHA_ROOT/$tmpl_in_dir", "$tmpl_extn_match");
69 foreach my $file (@template_files) {
70 (my $new_path = $file) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
71 $new_path =~ s/\.tmpl/.tt/;
72 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
74 open my $ITMPL, '<', $file or croak "Can't open $file for input: $!";
75 open my $OTT, '>', $new_path or croak "Can't open $new_path for output: $!";
77 # allows 'proper' handling of for loop scope
78 # cur_scope is a stack of scopes, the last being the current
79 # when opening a for loop push scope onto end, when closing for loop pop
80 my @cur_scope = ("");
81 # flag representing if we've found a for loop this iteration
82 my $for_loop_found = 0;
84 for my $input_tmpl(<$ITMPL>){
85 my @parts = split "<", $input_tmpl;
86 for( my $i=0; $i<=$#parts; ++$i ){
87 my $input_tmpl = $i ? "<" . $parts[$i] : $parts[$i]; # add < sign back in to every part except the first
88 $for_loop_found = 0;
90 # handle poorly names variable such as f1!, f1+, f1-, f1| and mod
91 $input_tmpl =~ s/"(\w+)\|"/"$1pipe"/ig;
92 $input_tmpl =~ s/"(\w+)\+"/"$1plus"/ig;
93 $input_tmpl =~ s/"(\w+)\-"/"$1minus"/ig;
94 $input_tmpl =~ s/"(\w+)!"/"$1exclamation"/ig;
95 $input_tmpl =~ s/"(\w+),(\w+)"/"$1comma$2"/ig; #caused a problem in patron search
96 $input_tmpl =~ s/NAME="mod"/NAME="modname"/ig;
97 # handle 'naked' TMPL_VAR "parameter" by turning them into what they should be, TMPL_VAR NAME="parameter"
98 $input_tmpl =~ s/TMPL_VAR\s+"(\w+)"/TMPL_VAR NAME="$1"/ig;
99 # make an end (ESCAPE NAME DEFAULT) into a ned (NAME ESCAPE DEFAULT)
100 $input_tmpl =~ s/ESCAPE="(\w+?)"\s+NAME=['"](\w+?)['"]\s+DEFAULT=['"](.+?)['"]/NAME="$2" ESCAPE="$1" DEFAULT="$3"/ig;
102 # Process..
103 # NB: if you think you're seeing double, you probably are, *some* (read:most) patterns appear twice: once with quotations marks, once without.
104 # trying to combine them into a single pattern proved troublesome as a regex like ['"]?(.*?)['"]? was causing problems and fixing the problem caused (a lot) more complex regex
106 # variables
107 $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;
108 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $cur_scope[-1]$1 |$2 %]/ig;
109 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?(\w*?)\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $cur_scope[-1]$1 |$2 %]/ig;
110 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+ESCAPE=['"]?(\w*?)['"]?\s+NAME\s?=\s?['"]?([\w-]*?)['"]?\s*-*>/[% $cur_scope[-1]$2 |$1 %]/ig;
111 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?(\w*?)['"]?\s+DEFAULT=['"](.*?)['"]\s*-*>/[% DEFAULT $cur_scope[-1]$1="$2" %]/ig;
112 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s+DEFAULT=(.*?)\s*-*>/[% DEFAULT $cur_scope[-1]$1=$2 %]/ig;
113 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s*-*>/[% $cur_scope[-1]$1 %]/ig;
114 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% $1 %]/ig; # TMPL_VAR NAME and TMPL_VAR EXPR are logically equiv
115 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?(.*?)\s*-*>/[% $1 %]/ig;
117 # if, elseif and unless blocks
118 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% IF ( $1 ) %]/ig;
119 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?(.*?)\s*-*>/[% IF ( $1 ) %]/ig;
120 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
121 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?(\w*?)\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
122 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+['"](.*?)['"]\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
123 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+([\w\s]*?)\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
125 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% ELSIF ( $1 ) %]/ig;
126 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+EXPR\s?=\s?(.*?)\s*-*>/[% ELSIF ( $1 ) %]/ig;
127 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+NAME\s?=\s?['"](\w*?)['"]\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
128 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+NAME\s?=\s?(\w*?)\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
129 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+['"](\w*?)['"]\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
130 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+(\w*?)\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
132 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSE\s*-*>/[% ELSE %]/ig;
133 $input_tmpl =~ s/<[!-]*\s*\/TMPL_IF\s*-*>/[% END %]/ig;
135 $input_tmpl =~ s/<[!-]*\s*TMPL_UNLESS\s+NAME\s?=\s?['"]?(\w*?)['"]?\s*-*>/[% UNLESS ( $cur_scope[-1]$1 ) %]/ig;
136 $input_tmpl =~ s/<[!-]*\s*\/TMPL_UNLESS\s*-*>/[% END %]/ig;
137 # includes
138 $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?\.inc)"\s*-*>/[% INCLUDE '$1' %]/ig;
139 $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?)"\s*-*>/[% INCLUDE $1 %]/ig;
141 #reverse scoping bug fix
142 for my $tag (@globals){
143 next unless $cur_scope[-1];
144 $input_tmpl =~ s/$cur_scope[-1]$tag/$tag/g;
147 if ($input_tmpl =~ m/<[!-]*\s*TMPL_LOOP/i ){
148 $for_loop_found = 1;
151 $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?['"](?<SCOPE>.*?)['"]\s*-*>/"[% FOREACH " . substr($+{SCOPE}, 0, -1) . " IN $cur_scope[-1]$1 %]"/ieg;
152 $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?(?<SCOPE>.*?)\s*-*>/"[% FOREACH " . substr($+{SCOPE}, 0, -1) . " IN $cur_scope[-1]$1 %]"/ieg;
154 # handle new scope
155 if($for_loop_found){
156 my $scope = substr($+{SCOPE}, 0, -1) . ".";
157 push(@cur_scope, $scope);
158 $for_loop_found = 0;
161 # handle loops and old scope
162 if ( $input_tmpl =~ m/<!--[\s\/]*TMPL_LOOP\s*-->/i ) {
163 push(@files_w_tmpl_loops, $new_path);
164 pop(@cur_scope);
167 $input_tmpl =~ s/<[!-]*\s*\/TMPL_LOOP\s*-*>/[% END %]/ig;
169 # misc 'patches'
170 $input_tmpl =~ s/\seq\s/ == /ig;
171 $input_tmpl =~ s/HTML/html/g;
172 $input_tmpl =~ s/URL/url/g;
173 $input_tmpl =~ s/dhtmlcalendar_dateformat/DHTMLcalendar_dateformat/ig;
174 $input_tmpl =~ s/\w*\.__first__/loop.first/ig;
175 $input_tmpl =~ s/\w*\.__last__/loop.last/ig;
176 $input_tmpl =~ s/\w*\.__odd__/loop.odd/ig;
177 $input_tmpl =~ s/\w*\.__even__/loop.even/ig;
178 $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
180 # hack to get around lack of javascript filter
181 $input_tmpl =~ s/\|\s*JS/|replace("'", "\\'") |replace('"', '\\"') |replace('\\n', '\\\\n') |replace('\\r', '\\\\r')/ig;
183 # Write out..
184 print $OTT $input_tmpl;
187 close $ITMPL;
188 close $OTT;
191 if ( scalar(@files_w_tmpl_loops) && $verbose ) {
192 print "\nThese files contain TMPL_LOOPs that need double checking:\n";
193 foreach my $file (@files_w_tmpl_loops) {
194 print "$file\n";
198 ## SUB-ROUTINES ##
200 # Create new directory structure and return list of template files
201 sub mirror_template_dir_structure_return_files {
202 my($dir, $type) = @_;
204 my @files = ();
205 if ( opendir(DIR, $dir) ) {
206 my @dirent = readdir DIR; # because DIR is shared when recursing
207 closedir DIR;
208 for my $dirent (@dirent) {
209 my $path = "$dir/$dirent";
210 if ( $dirent =~ /^\./ ) {
213 elsif ( -f $path ) {
214 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
215 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
216 if ( !defined $type || $dirent =~ /\.(?:$type)$/) {
217 push(@files, $path);
219 elsif ( $copy_other_files ) {
220 copy($path, $new_path)
221 or croak "Failed to copy $path to $new_path: $!";
224 elsif ( -d $path ) {
225 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
226 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
227 if ( ! -d $new_path ) {
228 mkdir($new_path) #, '0755'
229 or croak "Failed to create " . $new_path ." directory: $!";
231 my @sub_files = mirror_template_dir_structure_return_files($path, $type);
232 push(@files, @sub_files) if ( scalar(@sub_files) );
235 } else {
236 warn("Cannot open $dir: $! ... skipping");
239 return @files;