Bug 26922: Regression tests
[koha.git] / C4 / TTParser.pm
blob7007fb66046bf228814e541013ee57dd8ff48710
1 # Copyright Tamil 2011
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 #simple parser for HTML with Template Toolkit directives. Tokens are put into @tokens and are accesible via next_token and peep_token
19 package C4::TTParser;
20 use base qw(HTML::Parser);
21 use C4::TmplToken;
22 use strict;
23 use warnings;
25 #seems to be handled post tokenizer
26 ##hash where key is tag we are interested in and the value is a hash of the attributes we want
27 #my %interesting_tags = (
28 # img => { alt => 1 },
29 #);
31 #tokens found so far (used like a stack)
32 my ( @tokens );
34 #shiftnext token or undef
35 sub next_token{
36 return shift @tokens;
39 #unshift token back on @tokens
40 sub unshift_token{
41 my $self = shift;
42 unshift @tokens, shift;
45 #have a peep at next token
46 sub peep_token{
47 return $tokens[0];
50 #wrapper for parse
51 #please use this method INSTEAD of the HTML::Parser->parse_file method (and HTML::Parser->parse)
52 #signature build_tokens( self, filename)
53 sub build_tokens{
54 my ($self, $filename) = @_;
55 $self->{filename} = $filename;
56 $self->handler(start => "start", "self, line, tagname, attr, text"); #signature is start( self, linenumber, tagname, hash of attributes, original text )
57 $self->handler(text => "text", "self, line, text, is_cdata"); #signature is text( self, linenumber, original text, is_cdata )
58 $self->handler(end => "end", "self, line, tag, attr, text"); #signature is end( self, linenumber, tagename, original text )
59 $self->handler(declaration => "declaration", "self, line, text, is_cdata"); # declaration
60 $self->handler(comment => "comment", "self, line, text, is_cdata"); # comments
61 $self->handler(process => "process", "self, line, text, is_cdata"); # processing statement <?...?>
62 # $self->handler(default => "default", "self, line, text, is_cdata"); # anything else
63 $self->marked_sections(1); #treat anything inside CDATA tags as text, should really make it a C4::TmplTokenType::CDATA
64 $self->unbroken_text(1); #make contiguous whitespace into a single token (can span multiple lines)
65 open(my $fh, "<:encoding(utf8)", $filename) || die "Cannot open $filename ($!)";
66 $self->parse_file($fh);
67 return $self;
70 #handle parsing of text
71 sub text{
72 my $self = shift;
73 my $line = shift;
74 my $work = shift; # original text
75 my $is_cdata = shift;
76 while($work){
77 # if there is a template_toolkit tag
78 if( $work =~ m/\[%.*?%\]/ ){
79 #everything before this tag is text (or possibly CDATA), add a text token to tokens if $`
80 if( $` ){
81 my $t = C4::TmplToken->new( $`, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
82 push @tokens, $t;
85 #the match itself is a DIRECTIVE $&
86 my $t = C4::TmplToken->new( $&, C4::TmplTokenType::DIRECTIVE, $line, $self->{filename} );
87 push @tokens, $t;
89 # put work still to do back into work
90 $work = $' ? $' : 0;
91 } else {
92 # If there is some left over work, treat it as text token
93 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
95 push @tokens, $t;
96 last;
101 sub declaration {
102 my $self = shift;
103 my $line = shift;
104 my $work = shift; #original text
105 my $is_cdata = shift;
106 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
107 push @tokens, $t;
110 sub comment {
111 my $self = shift;
112 my $line = shift;
113 my $work = shift; #original text
114 my $is_cdata = shift;
115 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
116 push @tokens, $t;
119 sub process {
120 my $self = shift;
121 my $line = shift;
122 my $work = shift; #original text
123 my $is_cdata = shift;
124 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
125 push @tokens, $t;
128 sub default {
129 my $self = shift;
130 my $line = shift;
131 my $work = shift; #original text
132 my $is_cdata = shift;
133 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
134 push @tokens, $t;
138 #handle opening html tags
139 sub start{
140 my $self = shift;
141 my $line = shift;
142 my $tag = shift;
143 my $hash = shift; #hash of attr/value pairs
144 my $text = shift; #original text
145 my $t = C4::TmplToken->new( $text, C4::TmplTokenType::TAG, $line, $self->{filename});
146 my %attr;
147 # tags seem to be uses in an 'interesting' way elsewhere..
148 for my $key( %$hash ) {
149 next unless defined $hash->{$key};
150 if ($key eq "/"){
151 $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 1 ];
153 else {
154 $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 0 ];
157 $t->set_attributes( \%attr );
158 push @tokens, $t;
161 #handle closing html tags
162 sub end{
163 my $self = shift;
164 my $line = shift;
165 my $tag = shift;
166 my $hash = shift;
167 my $text = shift;
168 # what format should this be in?
169 my $t = C4::TmplToken->new( $text, C4::TmplTokenType::TAG, $line, $self->{filename} );
170 my %attr;
171 # tags seem to be uses in an 'interesting' way elsewhere..
172 for my $key( %$hash ) {
173 next unless defined $hash->{$key};
174 $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 0 ];
176 $t->set_attributes( \%attr );
177 push @tokens, $t;