From 9c7d9b0934f36789177c3e7ba45b26687be384f2 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Sun, 14 Feb 2010 18:05:45 -0500 Subject: [PATCH] new test script to help debug template errors From the POD: NAME show-template-structure.pl DESCRIPTION This script displays the structure of loops and conditional statements in an HTML::Template::Pro template, and is an aid for debugging errors reported by the xt/author/valid-templates.t test. It also identifies the following errors: * TMPL_IF/TMPL_UNLESS/TMPL_LOOP with no closing tag * TMPL_ELSE with no initial TMPL_IF or TMPL_UNLESS * extra closing tags * HTML comment of the form /g) { + + my $norm_comment = lc $comment; + $norm_comment =~ s/^\s+//; + next unless $norm_comment =~ m!^/{0,1}tmpl_!; + my ( $tmpl_tag_close, $tmpl_tag ) = $norm_comment =~ m!^(/{0,1})(tmpl_\S+)!; + $tmpl_tag_close = "" unless defined $tmpl_tag_close; + + unless ( exists $valid_tmpl_tags{$tmpl_tag} ) { + print "ERROR (line $lineno): $tmpl_tag is not a valid HTML::Template::Pro tag\n"; + } + next unless exists $tmpl_structure_tags{$tmpl_tag}; # only care about tags that affect loop or conditional structure + if ( $tmpl_structure_tags{$tmpl_tag} ) { + + # we'll either be pushing or popping the tag stack + if ($tmpl_tag_close) { + + # popping tag + emit "${tmpl_tag_close}${tmpl_tag} (line $lineno)"; + if ( scalar(@tag_stack) < 1 ) { + print "\nERROR (line $lineno): $tmpl_tag causes tag stack underflow\n"; + } else { + my ( $popped_tag, $target, $popped_lineno ) = @{ pop @tag_stack }; + if ( $tmpl_tag ne $popped_tag ) { + print "\nERROR (line $lineno): got /$tmpl_tag but expected /$popped_tag to", + " match $popped_tag from line $popped_lineno\n"; + } else { + print " # $target from $popped_lineno\n"; + } + } + } elsif ( $tmpl_structure_tags{$tmpl_tag} ) { + + # pushable tag + my ($target) = $comment =~ /(?:EXPR|NAME)\s*=\s*['"](.*?)['"]/i; + push @tag_stack, [ $tmpl_tag, $target, $lineno ]; + emit "${tmpl_tag_close}${tmpl_tag} ($target, line $lineno)\n"; + } + } else { + + # we're either a tmpl_else or tmpl_elsif, so make sure that + # top of stack contains a tmpl_if + emit "${tmpl_tag_close}${tmpl_tag} (line $lineno)\n"; + if ( scalar @tag_stack < 1 ) { + print "ERROR: found $tmpl_tag, but tag stack is empty.\n"; + } else { + my ( $peeked_tag, $target, $peeked_lineno ) = @{ $tag_stack[0] }; + if ( $peeked_tag ne "tmpl_if" and $peeked_tag ne "tmpl_unless" ) { + print "ERROR: found $tmpl_tag, but it does not appear to match a tmpl_if. Top of stack is $peeked_tag.\n"; + } + } + } + } +} + +close IN; + +# anything left in the stack? +if (scalar @tag_stack > 0) { + print "ERROR: tag stack is not empty - the following template structures have not been closed:\n"; + my $i = 0; + while (my $entry = pop @tag_stack) { + $i++; + my ( $popped_tag, $target, $popped_lineno ) = @{ $entry }; + print "$i: $popped_tag $target (line $popped_lineno)\n"; + } +} + +exit 0; + +=head1 AUTHOR + +Koha Development team + +Galen Charlton + +=cut -- 2.39.5