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|pref"; # 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 =~ s/\.tmpl/.tt/;
65 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
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: $!";
70 # Slurp in input file..
71 my $input_tmpl = do { local $/; <$ITMPL> };
74 # handle poorly names variable such as f1!, f1+, f1-, f1| and mod
75 $input_tmpl =~ s/"(\w+)\|"/"$1pipe"/ig;
76 $input_tmpl =~ s/"(\w+)\+"/"$1plus"/ig;
77 $input_tmpl =~ s/"(\w+)\-"/"$1minus"/ig;
78 $input_tmpl =~ s/"(\w+)!"/"$1exclamation"/ig;
79 $input_tmpl =~ s/"(\w+),(\w+)"/"$1comma$2"/ig;
80 $input_tmpl =~ s/NAME="mod"/NAME="modname"/ig;
81 # handle 'naked' TMPL_VAR "parameter" by turning them into what they should be, TMPL_VAR NAME="parameter"
82 $input_tmpl =~ s/TMPL_VAR\s+"(\w+)"/TMPL_VAR NAME="$1"/ig;
83 # make an end (ESCAPE NAME DEFAULT) into a ned (NAME ESCAPE DEFAULT)
84 $input_tmpl =~ s/ESCAPE="(\w+?)"\s+NAME=['"](\w+?)['"]\s+DEFAULT=['"](.+?)['"]/NAME="$2" ESCAPE="$1" DEFAULT="$3"/ig;
87 # NB: if you think you're seeing double, you probably are, *some* (read:most) patterns appear twice: once with quotations marks, once without.
88 # 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
90 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s+ESCAPE=['"](\w*?)['"]\s+DEFAULT=['"]?(.*?)['"]?\s*-*>/[% DEFAULT $1="$3" |$2 %]/ig; # CHECK ME PLEASE
91 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $1 |$2 %]/ig;
92 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?(\w*?)\s+ESCAPE=['"]?(\w*?)['"]?\s*-*>/[% $1 |$2 %]/ig;
93 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+ESCAPE=['"]?(\w*?)['"]?\s+NAME\s?=\s?['"]?([\w-]*?)['"]?\s*-*>/[% $2 |$1 %]/ig;
94 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?(\w*?)['"]?\s+DEFAULT=['"](.*?)['"]\s*-*>/[% DEFAULT $1="$2" %]/ig; # if a value being assigned is wrapped in quotes, keep them intact
95 $input_tmpl =~ s/<[!-]*\s*TMPL_VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s+DEFAULT=(.*?)\s*-*>/[% DEFAULT $1=$2 %]/ig;
96 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+NAME\s?=\s?['"]?\s*(\w*?)\s*['"]?\s*-*>/[% $1 %]/ig;
97 $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
98 $input_tmpl =~ s/<[!-]*\s*TMPL[_\s]VAR\s+EXPR\s?=\s?(.*?)\s*-*>/[% $1 %]/ig;
100 # if, elseif and unless blocks
101 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% IF ( $1 ) %]/ig;
102 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+EXPR\s?=\s?(.*?)\s*-*>/[% IF ( $1 ) %]/ig;
103 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?['"]\s*(\w*?)\s*['"]\s*-*>/[% IF ( $1 ) %]/ig;
104 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+NAME\s?=\s?(\w*?)\s*-*>/[% IF ( $1 ) %]/ig;
105 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+['"](.*?)['"]\s*-*>/[% IF ( $1 ) %]/ig;
106 $input_tmpl =~ s/<[!-]*\s*TMPL_IF\s+([\w\s]*?)\s*-*>/[% IF ( $1 ) %]/ig;
108 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+EXPR\s?=\s?['"](.*?)['"]\s*-*>/[% ELSEIF ( $1 ) %]/ig;
109 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+EXPR\s?=\s?(.*?)\s*-*>/[% ELSEIF ( $1 ) %]/ig;
110 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+NAME\s?=\s?['"](\w*?)['"]\s*-*>/[% ELSEIF ( $1 ) %]/ig;
111 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+NAME\s?=\s?(\w*?)\s*-*>/[% ELSEIF ( $1 ) %]/ig;
112 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+['"](\w*?)['"]\s*-*>/[% ELSEIF ( $1 ) %]/ig;
113 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSIF\s+(\w*?)\s*-*>/[% ELSEIF ( $1 ) %]/ig;
115 $input_tmpl =~ s/<[!-]*\s*TMPL_ELSE\s*-*>/[% ELSE %]/ig;
116 $input_tmpl =~ s/<[!-]*\s*\/TMPL_IF\s*-*>/[% END %]/ig;
118 $input_tmpl =~ s/<[!-]*\s*TMPL_UNLESS\s+NAME\s?=\s?['"]?(\w*?)['"]?\s*-*>/[% UNLESS ( $1 ) %]/ig;
119 $input_tmpl =~ s/<[!-]*\s*\/TMPL_UNLESS\s*-*>/[% END %]/ig;
121 $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?\.inc)"\s*-*>/[% INCLUDE '$1' %]/ig;
122 $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?)"\s*-*>/[% INCLUDE $1 %]/ig;
124 if ( $input_tmpl =~ m/<!--[\s\/]*TMPL_LOOP\s*-->/i ) {
125 push(@files_w_tmpl_loops, $new_path);
128 $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?['"](.*?)['"]\s*-*>/"[% FOREACH ".substr($1, 0 , -1)." = ".$1." %]"/ieg;
129 $input_tmpl =~ s/<[!-]*\s*TMPL_LOOP\s+NAME\s?=\s?(.*?)\s*-*>/"[% FOREACH ".substr($1, 0 , -1)." = ".$1." %]"/ieg;
130 $input_tmpl =~ s/<[!-]*\s*\/TMPL_LOOP\s*-*>/[% END %]/ig;
133 $input_tmpl =~ s/\seq\s/ == /ig;
134 $input_tmpl =~ s/HTML/html/ig;
135 $input_tmpl =~ s/URL/url/ig;
137 #hack to get around lack of javascript filter
138 $input_tmpl =~ s/\|JS/|replace("'", "\\'") |replace('"', '\\"') |replace('\\n', '\\\\n') |replace('\\r', '\\\\r')/ig;
141 print $OTT $input_tmpl;
145 if ( scalar(@files_w_tmpl_loops) && $verbose ) {
146 print "\nThese files contain TMPL_LOOPs that need double checking:\n";
147 foreach my $file (@files_w_tmpl_loops) {
154 # Create new directory structure and return list of template files
155 sub mirror_template_dir_structure_return_files {
156 my($dir, $type) = @_;
159 if ( opendir(DIR, $dir) ) {
160 my @dirent = readdir DIR; # because DIR is shared when recursing
162 for my $dirent (@dirent) {
163 my $path = "$dir/$dirent";
164 if ( $dirent =~ /^\./ ) {
168 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
169 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
170 if ( !defined $type || $dirent =~ /\.(?:$type)$/) {
173 elsif ( $copy_other_files ) {
174 copy($path, $new_path)
175 or croak "Failed to copy $path to $new_path: $!";
179 (my $new_path = $path) =~ s/$tmpl_in_dir/$tmpl_out_dir/;
180 $new_path = "$KOHA_ROOT/$new_path" unless ( $new_path =~ m/^$KOHA_ROOT/ );
181 if ( ! -d $new_path ) {
182 mkdir($new_path) #, '0755'
183 or croak "Failed to create " . $new_path ." directory: $!";
185 my @sub_files = mirror_template_dir_structure_return_files($path, $type);
186 push(@files, @sub_files) if ( scalar(@sub_files) );
190 warn("Cannot open $dir: $! ... skipping");