ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / installer / html-template-to-template-toolkit.pl
index a128678..60cd951 100755 (executable)
@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 
-use strict;
-use warnings;
+use Modern::Perl;
 use Carp;
 use Data::Dumper;
 
@@ -27,6 +26,14 @@ EOH
 my $tmpl_in_dir      = 'koha-tmpl';
 my $tmpl_out_dir     = 'koha-tt';
 
+# template toolkit variables NOT to scope, in other words, variables that need to remain global (case sensitive)
+my @globals = ("themelang","JacketImages","OPACAmazonCoverImages","GoogleJackets","BakerTaylorEnabled",
+"SyndeticsEnabled", "OpacRenewalAllowed", "item_level_itypes","noItemTypeImages",
+"virtualshelves", "RequestOnOpac", "COinSinOPACResults", "OPACXSLTResultsDisplay",
+"OPACItemsResultsDisplay", "LibraryThingForLibrariesID", "opacuserlogin", "TagsEnabled",
+"TagsShowOnList", "TagsInputOnList","loggedinusername","opacbookbag",
+"OPACAmazonEnabled", "SyndeticsCoverImages","using_https");
+
 # Arguments:
 my $KOHA_ROOT;
 my $tmpl_extn_match  = "tmpl|inc|xsl"; # Type match defaults to *.tmpl plus *.inc if not specified
@@ -75,6 +82,9 @@ foreach my $file (@template_files) {
     my $for_loop_found = 0;
 
     for my $input_tmpl(<$ITMPL>){
+        my @parts = split "<", $input_tmpl;
+        for( my $i=0; $i<=$#parts; ++$i ){
+            my $input_tmpl = $i ? "<" . $parts[$i] : $parts[$i]; # add < sign back in to every part except the first
        $for_loop_found = 0;
 
        # handle poorly names variable such as f1!, f1+, f1-, f1| and mod
@@ -82,7 +92,7 @@ foreach my $file (@template_files) {
        $input_tmpl =~ s/"(\w+)\+"/"$1plus"/ig;
        $input_tmpl =~ s/"(\w+)\-"/"$1minus"/ig;
        $input_tmpl =~ s/"(\w+)!"/"$1exclamation"/ig;
-       $input_tmpl =~ s/"(\w+),(\w+)"/"$1comma$2"/ig;
+       $input_tmpl =~ s/"(\w+),(\w+)"/"$1comma$2"/ig; #caused a problem in patron search
        $input_tmpl =~ s/NAME="mod"/NAME="modname"/ig;
        # handle 'naked' TMPL_VAR "parameter" by turning them into what they should be, TMPL_VAR NAME="parameter"
        $input_tmpl =~ s/TMPL_VAR\s+"(\w+)"/TMPL_VAR NAME="$1"/ig;
@@ -91,7 +101,7 @@ foreach my $file (@template_files) {
 
        # Process..
        # NB: if you think you're seeing double, you probably are, *some* (read:most) patterns appear twice: once with quotations marks, once without.
-       #     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
+    #     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
 
        # variables
        $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;
@@ -128,7 +138,13 @@ foreach my $file (@template_files) {
        $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?\.inc)"\s*-*>/[% INCLUDE '$1' %]/ig;
        $input_tmpl =~ s/<[!-]*\s*TMPL_INCLUDE\s+NAME\s?=\s?"(.*?)"\s*-*>/[% INCLUDE $1 %]/ig;
 
-       if ($input_tmpl =~ m/<[!-]*\s*TMPL_LOOP/i ){
+        #reverse scoping bug fix
+        for my $tag (@globals){
+            next unless $cur_scope[-1];
+            $input_tmpl =~ s/$cur_scope[-1]$tag/$tag/g;
+        }
+
+       if ($input_tmpl =~ m/<[!-]*\s*TMPL_LOOP/i ){
            $for_loop_found = 1;
        }
 
@@ -152,8 +168,9 @@ foreach my $file (@template_files) {
 
        # misc 'patches'
        $input_tmpl =~ s/\seq\s/ == /ig;
-       $input_tmpl =~ s/HTML/html/ig;
-       $input_tmpl =~ s/URL/url/ig;
+       $input_tmpl =~ s/HTML/html/g;
+       $input_tmpl =~ s/URL/url/g;
+        $input_tmpl =~ s/dhtmlcalendar_dateformat/DHTMLcalendar_dateformat/ig;
        $input_tmpl =~ s/\w*\.__first__/loop.first/ig;
        $input_tmpl =~ s/\w*\.__last__/loop.last/ig;
        $input_tmpl =~ s/\w*\.__odd__/loop.odd/ig;
@@ -161,12 +178,13 @@ foreach my $file (@template_files) {
        $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
 
        # hack to get around lack of javascript filter
-       $input_tmpl =~ s/\|JS/|replace("'", "\\'") |replace('"', '\\"') |replace('\\n', '\\\\n') |replace('\\r', '\\\\r')/ig;
+       $input_tmpl =~ s/\|\s*JS/|replace("'", "\\'") |replace('"', '\\"') |replace('\\n', '\\\\n') |replace('\\r', '\\\\r')/ig;
     
        # Write out..
-       print $OTT $input_tmpl;
+        print $OTT $input_tmpl;
+        }
     }
-    close $ITMPL
+    close $ITMPL;
     close $OTT;
 }