Bug 17585: Replace ->get_account_lines with ->account
[koha.git] / installer / html-template-to-template-toolkit.pl
blob5f0e0ac98e5a86d381baaa17f57e8371a2d2b7e4
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 use Carp;
6 use Data::Dumper;
8 use Getopt::Long;
9 use File::Basename;
10 use File::Copy;
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.
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
27 my $tmpl_in_dir = 'koha-tmpl';
28 my $tmpl_out_dir = 'koha-tt';
30 # template toolkit variables NOT to scope, in other words, variables that need to remain global (case sensitive)
31 my @globals = ("themelang","JacketImages","OPACAmazonCoverImages","GoogleJackets","BakerTaylorEnabled",
32 "SyndeticsEnabled", "OpacRenewalAllowed", "item_level_itypes","noItemTypeImages",
33 "virtualshelves", "RequestOnOpac", "COinSinOPACResults", "OPACXSLTResultsDisplay",
34 "OPACItemsResultsDisplay", "LibraryThingForLibrariesID", "opacuserlogin", "TagsEnabled",
35 "TagsShowOnList", "TagsInputOnList","loggedinusername","opacbookbag",
36 "OPACAmazonEnabled", "SyndeticsCoverImages","using_https");
38 # Arguments:
39 my $KOHA_ROOT;
40 my $tmpl_extn_match = "tmpl|inc|xsl"; # Type match defaults to *.tmpl plus *.inc if not specified
41 my $copy_other_files = 0;
42 my @template_files;
43 my @files_w_tmpl_loops;
44 my $verbose = 0;
45 GetOptions (
46 "koharoot=s" => \$KOHA_ROOT,
47 "type|t=s" => \$tmpl_extn_match,
48 "copyall|c" => \$copy_other_files,
49 "file|f=s" => \@template_files, # array of filenames
50 "verbose+" => \$verbose, # incremental flag
51 ) or die $help_msg;
53 if ( ! $KOHA_ROOT || ! -d $KOHA_ROOT ) {
54 croak "Koha root not passed or is not correct.";
56 if ( ! -d "$KOHA_ROOT/$tmpl_in_dir" ) {
57 croak "Cannot find template dir ($tmpl_in_dir)";
60 # Attempt to create koha-tt dir..
61 if ( ! -d "$KOHA_ROOT/$tmpl_out_dir" ) {
62 mkdir("$KOHA_ROOT/$tmpl_out_dir") #, '0755'
63 or croak "Cannot create $tmpl_out_dir directory in $KOHA_ROOT: $!";
66 # Obtain list of files to process - go recursively through tmpl_in_dir and subdirectories..
67 unless ( scalar(@template_files) ) {
68 @template_files = mirror_template_dir_structure_return_files("$KOHA_ROOT/$tmpl_in_dir", "$tmpl_extn_match");
70 foreach my $file (@template_files) {
71 (my $new_path = $file) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
72 $new_path =~ s/\.tmpl/.tt/;
73 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
75 open my $ITMPL, '<', $file or croak "Can't open $file for input: $!";
76 open my $OTT, '>', $new_path or croak "Can't open $new_path for output: $!";
78 # allows 'proper' handling of for loop scope
79 # cur_scope is a stack of scopes, the last being the current
80 # when opening a for loop push scope onto end, when closing for loop pop
81 my @cur_scope = ("");
82 # flag representing if we've found a for loop this iteration
83 my $for_loop_found = 0;
85 for my $input_tmpl(<$ITMPL>){
86 my @parts = split "<", $input_tmpl;
87 for( my $i=0; $i<=$#parts; ++$i ){
88 my $input_tmpl = $i ? "<" . $parts[$i] : $parts[$i]; # add < sign back in to every part except the first
89 $for_loop_found = 0;
91 # handle poorly names variable such as f1!, f1+, f1-, f1| and mod
92 $input_tmpl =~ s/"(\w+)\|"/"$1pipe"/ig;
93 $input_tmpl =~ s/"(\w+)\+"/"$1plus"/ig;
94 $input_tmpl =~ s/"(\w+)\-"/"$1minus"/ig;
95 $input_tmpl =~ s/"(\w+)!"/"$1exclamation"/ig;
96 $input_tmpl =~ s/"(\w+),(\w+)"/"$1comma$2"/ig; #caused a problem in patron search
97 $input_tmpl =~ s/NAME="mod"/NAME="modname"/ig;
98 # handle 'naked' TMPL_VAR "parameter" by turning them into what they should be, TMPL_VAR NAME="parameter"
99 $input_tmpl =~ s/TMPL_VAR\s+"(\w+)"/TMPL_VAR NAME="$1"/ig;
100 # make an end (ESCAPE NAME DEFAULT) into a ned (NAME ESCAPE DEFAULT)
101 $input_tmpl =~ s/ESCAPE="(\w+?)"\s+NAME=['"](\w+?)['"]\s+DEFAULT=['"](.+?)['"]/NAME="$2" ESCAPE="$1" DEFAULT="$3"/ig;
103 # Process..
104 # NB: if you think you're seeing double, you probably are, *some* (read:most) patterns appear twice: once with quotations marks, once without.
105 # 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
107 # variables
108 $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;
109 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $cur_scope[-1]$1 |$2 %]/ig;
110 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?(\w*?)\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $cur_scope[-1]$1 |$2 %]/ig;
111 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+ESCAPE=['"]?(\w*?)['"]?\s+NAME\s?=\s?['"]?([\w-]*?)['"]?\s*-*>/[% $cur_scope[-1]$2 |$1 %]/ig;
112 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?(\w*?)['"]?\s+DEFAULT=['"](.*?)['"]\s*-*>/[% DEFAULT $cur_scope[-1]$1="$2" %]/ig;
113 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s+DEFAULT=(.*?)\s*-*>/[% DEFAULT $cur_scope[-1]$1=$2 %]/ig;
114 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s*-*>/[% $cur_scope[-1]$1 %]/ig;
115 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% $1 %]/ig; # TMPL_VAR NAME and TMPL_VAR EXPR are logically equiv
116 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?(.*?)\s*-*>/[% $1 %]/ig;
118 # if, elseif and unless blocks
119 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% IF ( $1 ) %]/ig;
120 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?(.*?)\s*-*>/[% IF ( $1 ) %]/ig;
121 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
122 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?(\w*?)\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
123 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+['"](.*?)['"]\s*-*>/[% IF ( $cur_scope[-1]$1 ) %]/ig;
124 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+([\w\s]*?)\s*-*>/[% IF ( $cur_scope[-1]$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+EXPR\s?=\s?(.*?)\s*-*>/[% ELSIF ( $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+NAME\s?=\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;
131 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+(\w*?)\s*-*>/[% ELSIF ( $cur_scope[-1]$1 ) %]/ig;
133 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSE\s*-*>/[% ELSE %]/ig;
134 $input_tmpl =~ s/<[!-]*\s*\/TMPL_IF\s*-*>/[% END %]/ig;
136 $input_tmpl =~ s/<[!-]*\s*TMPL_UNLESS\s+NAME\s?=\s?['"]?(\w*?)['"]?\s*-*>/[% UNLESS ( $cur_scope[-1]$1 ) %]/ig;
137 $input_tmpl =~ s/<[!-]*\s*\/TMPL_UNLESS\s*-*>/[% END %]/ig;
138 # includes
139 $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?\.inc)"\s*-*>/[% INCLUDE '$1' %]/ig;
140 $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?)"\s*-*>/[% INCLUDE $1 %]/ig;
142 #reverse scoping bug fix
143 for my $tag (@globals){
144 next unless $cur_scope[-1];
145 $input_tmpl =~ s/$cur_scope[-1]$tag/$tag/g;
148 if ($input_tmpl =~ m/<[!-]*\s*TMPL_LOOP/i ){
149 $for_loop_found = 1;
152 $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?['"](?<SCOPE>.*?)['"]\s*-*>/"[% FOREACH " . substr($+{SCOPE}, 0, -1) . " IN $cur_scope[-1]$1 %]"/ieg;
153 $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?(?<SCOPE>.*?)\s*-*>/"[% FOREACH " . substr($+{SCOPE}, 0, -1) . " IN $cur_scope[-1]$1 %]"/ieg;
155 # handle new scope
156 if($for_loop_found){
157 my $scope = substr($+{SCOPE}, 0, -1) . ".";
158 push(@cur_scope, $scope);
159 $for_loop_found = 0;
162 # handle loops and old scope
163 if ( $input_tmpl =~ m/<!--[\s\/]*TMPL_LOOP\s*-->/i ) {
164 push(@files_w_tmpl_loops, $new_path);
165 pop(@cur_scope);
168 $input_tmpl =~ s/<[!-]*\s*\/TMPL_LOOP\s*-*>/[% END %]/ig;
170 # misc 'patches'
171 $input_tmpl =~ s/\seq\s/ == /ig;
172 $input_tmpl =~ s/HTML/html/g;
173 $input_tmpl =~ s/URL/url/g;
174 $input_tmpl =~ s/dhtmlcalendar_dateformat/DHTMLcalendar_dateformat/ig;
175 $input_tmpl =~ s/\w*\.__first__/loop.first/ig;
176 $input_tmpl =~ s/\w*\.__last__/loop.last/ig;
177 $input_tmpl =~ s/\w*\.__odd__/loop.odd/ig;
178 $input_tmpl =~ s/\w*\.__even__/loop.even/ig;
179 $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
181 # hack to get around lack of javascript filter
182 $input_tmpl =~ s/\|\s*JS/|replace("'", "\\'") |replace('"', '\\"') |replace('\\n', '\\\\n') |replace('\\r', '\\\\r')/ig;
184 # Write out..
185 print $OTT $input_tmpl;
188 close $ITMPL;
189 close $OTT;
192 if ( scalar(@files_w_tmpl_loops) && $verbose ) {
193 print "\nThese files contain TMPL_LOOPs that need double checking:\n";
194 foreach my $file (@files_w_tmpl_loops) {
195 print "$file\n";
199 ## SUB-ROUTINES ##
201 # Create new directory structure and return list of template files
202 sub mirror_template_dir_structure_return_files {
203 my($dir, $type) = @_;
205 my @files = ();
206 if ( opendir(DIR, $dir) ) {
207 my @dirent = readdir DIR; # because DIR is shared when recursing
208 closedir DIR;
209 for my $dirent (@dirent) {
210 my $path = "$dir/$dirent";
211 if ( $dirent =~ /^\./ ) {
214 elsif ( -f $path ) {
215 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
216 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
217 if ( !defined $type || $dirent =~ /\.(?:$type)$/) {
218 push(@files, $path);
220 elsif ( $copy_other_files ) {
221 copy($path, $new_path)
222 or croak "Failed to copy $path to $new_path: $!";
225 elsif ( -d $path ) {
226 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
227 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
228 if ( ! -d $new_path ) {
229 mkdir($new_path) #, '0755'
230 or croak "Failed to create " . $new_path ." directory: $!";
232 my @sub_files = mirror_template_dir_structure_return_files($path, $type);
233 push(@files, @sub_files) if ( scalar(@sub_files) );
236 } else {
237 warn("Cannot open $dir: $! ... skipping");
240 return @files;