Added coverage to unit test for TTParser
[koha.git] / C4 / TTParser.pm
1 #!/usr/bin/env perl
2
3 # Copyright Tamil 2011
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 #simple parser for HTML with Template Toolkit directives. Tokens are put into @tokens and are accesible via next_token and peep_token
21 package C4::TTParser;
22 use base qw(HTML::Parser);
23 use C4::TmplToken;
24 use strict;
25 use warnings;
26
27 #seems to be handled post tokenizer
28 ##hash where key is tag we are interested in and the value is a hash of the attributes we want
29 #my %interesting_tags = (
30 #    img => { alt => 1 },
31 #);
32
33 #tokens found so far (used like a stack)
34 my ( @tokens );
35
36 #shiftnext token or undef
37 sub next_token{
38     return shift @tokens;
39 }
40
41 #unshift token back on @tokens
42 sub unshift_token{
43     unshift @tokens, shift;
44 }
45
46 #have a peep at next token
47 sub peep_token{
48     return $tokens[0];
49 }
50
51 #wrapper for parse
52 #please use this method INSTEAD of the HTML::Parser->parse_file method (and HTML::Parser->parse)
53 #signature build_tokens( self, filename)
54 sub build_tokens{
55     my ($self, $filename) = @_;
56     $self->{filename} = $filename;
57     $self->handler(start => "start", "self, line, tagname, attr, text"); #signature is start( self, linenumber, tagname, hash of attributes, origional text )
58     $self->handler(text => "text", "self, line, text, is_cdata"); #signature is text( self, linenumber, origional text, is_cdata )
59     $self->handler(end => "end", "self, line, tag, attr, text"); #signature is end( self, linenumber, tagename, origional text )
60     $self->handler(declaration => "declaration", "self, line, text, is_cdata"); # declaration
61     $self->handler(comment => "comment", "self, line, text, is_cdata"); # comments
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     $self->parse_file($filename);
66     return $self;
67 }
68
69 #handle parsing of text
70 sub text{
71     my $self = shift;
72     my $line = shift;
73     my $work = shift; # original text
74     my $is_cdata = shift;
75     while($work){
76         # if there is a template_toolkit tag
77         if( $work =~ m/\[%.*?\]/ ){
78             #everything before this tag is text (or possibly CDATA), add a text token to tokens if $`
79             if( $` ){
80                 my $t = C4::TmplToken->new( $`, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
81                 push @tokens, $t;
82             }
83
84             #the match itself is a DIRECTIVE $&
85             my $t = C4::TmplToken->new( $&, C4::TmplTokenType::DIRECTIVE, $line, $self->{filename} );
86             push @tokens, $t;
87
88             # put work still to do back into work
89             $work = $' ? $' : 0;
90         } else {
91             # If there is some left over work, treat it as text token
92             my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
93             
94             push @tokens, $t;
95             last;
96         }
97     }
98 }
99
100 sub declaration {
101     my $self = shift;
102     my $line = shift;
103     my $work = shift; #original text
104     my $is_cdata = shift;
105     my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
106     push @tokens, $t;  
107 }      
108
109 sub comment {
110     my $self = shift;
111     my $line = shift;
112     my $work = shift; #original text
113     my $is_cdata = shift;
114     my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
115     push @tokens, $t;  
116 }      
117
118 sub default {
119     my $self = shift;
120     my $line = shift;
121     my $work = shift; #original text
122     my $is_cdata = shift;
123     my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
124     push @tokens, $t;  
125 }      
126
127
128 #handle opening html tags
129 sub start{
130     my $self = shift;
131     my $line = shift;
132     my $tag = shift;
133     my $hash = shift; #hash of attr/value pairs
134     my $text = shift; #origional text
135     my $t = C4::TmplToken->new( $text, C4::TmplTokenType::TAG, $line, $self->{filename});
136     my %attr;
137     # tags seem to be uses in an 'interesting' way elsewhere..
138     for my $key( %$hash ) {
139         next unless defined $hash->{$key};
140         if ($key eq "/"){
141             $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 1 ];
142             }
143         else {
144         $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 0 ];
145             }
146     }
147     $t->set_attributes( \%attr );
148     push @tokens, $t;
149 }
150
151 #handle closing html tags
152 sub end{
153     my $self = shift;
154     my $line = shift;
155     my $tag = shift;
156     my $hash = shift;
157     my $text = shift;
158     # what format should this be in?
159     my $t = C4::TmplToken->new( $text, C4::TmplTokenType::TAG, $line, $self->{filename} );
160     my %attr;
161     # tags seem to be uses in an 'interesting' way elsewhere..
162     for my $key( %$hash ) {
163         next unless defined $hash->{$key};
164         $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 0 ];
165     }
166     $t->set_attributes( \%attr );
167     push @tokens, $t;
168 }
169
170 1;