Bug 17355: Add the ability to remove authorised value categories
[koha.git] / C4 / TTParser.pm
bloba55eccdfedf24008c280a5f50d363363d1ca7410
1 #!/usr/bin/env perl
3 # Copyright Tamil 2011
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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;
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 #);
33 #tokens found so far (used like a stack)
34 my ( @tokens );
36 #shiftnext token or undef
37 sub next_token{
38 return shift @tokens;
41 #unshift token back on @tokens
42 sub unshift_token{
43 my $self = shift;
44 unshift @tokens, shift;
47 #have a peep at next token
48 sub peep_token{
49 return $tokens[0];
52 #wrapper for parse
53 #please use this method INSTEAD of the HTML::Parser->parse_file method (and HTML::Parser->parse)
54 #signature build_tokens( self, filename)
55 sub build_tokens{
56 my ($self, $filename) = @_;
57 $self->{filename} = $filename;
58 $self->handler(start => "start", "self, line, tagname, attr, text"); #signature is start( self, linenumber, tagname, hash of attributes, original text )
59 $self->handler(text => "text", "self, line, text, is_cdata"); #signature is text( self, linenumber, original text, is_cdata )
60 $self->handler(end => "end", "self, line, tag, attr, text"); #signature is end( self, linenumber, tagename, original text )
61 $self->handler(declaration => "declaration", "self, line, text, is_cdata"); # declaration
62 $self->handler(comment => "comment", "self, line, text, is_cdata"); # comments
63 $self->handler(process => "process", "self, line, text, is_cdata"); # processing statement <?...?>
64 # $self->handler(default => "default", "self, line, text, is_cdata"); # anything else
65 $self->marked_sections(1); #treat anything inside CDATA tags as text, should really make it a C4::TmplTokenType::CDATA
66 $self->unbroken_text(1); #make contiguous whitespace into a single token (can span multiple lines)
67 $self->parse_file($filename);
68 return $self;
71 #handle parsing of text
72 sub text{
73 my $self = shift;
74 my $line = shift;
75 my $work = shift; # original text
76 my $is_cdata = shift;
77 while($work){
78 # if there is a template_toolkit tag
79 if( $work =~ m/\[%.*?%\]/ ){
80 #everything before this tag is text (or possibly CDATA), add a text token to tokens if $`
81 if( $` ){
82 my $t = C4::TmplToken->new( $`, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
83 push @tokens, $t;
86 #the match itself is a DIRECTIVE $&
87 my $t = C4::TmplToken->new( $&, C4::TmplTokenType::DIRECTIVE, $line, $self->{filename} );
88 push @tokens, $t;
90 # put work still to do back into work
91 $work = $' ? $' : 0;
92 } else {
93 # If there is some left over work, treat it as text token
94 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
96 push @tokens, $t;
97 last;
102 sub declaration {
103 my $self = shift;
104 my $line = shift;
105 my $work = shift; #original text
106 my $is_cdata = shift;
107 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
108 push @tokens, $t;
111 sub comment {
112 my $self = shift;
113 my $line = shift;
114 my $work = shift; #original text
115 my $is_cdata = shift;
116 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
117 push @tokens, $t;
120 sub process {
121 my $self = shift;
122 my $line = shift;
123 my $work = shift; #original text
124 my $is_cdata = shift;
125 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
126 push @tokens, $t;
129 sub default {
130 my $self = shift;
131 my $line = shift;
132 my $work = shift; #original text
133 my $is_cdata = shift;
134 my $t = C4::TmplToken->new( $work, ($is_cdata? C4::TmplTokenType::CDATA : C4::TmplTokenType::TEXT), $line, $self->{filename} );
135 push @tokens, $t;
139 #handle opening html tags
140 sub start{
141 my $self = shift;
142 my $line = shift;
143 my $tag = shift;
144 my $hash = shift; #hash of attr/value pairs
145 my $text = shift; #original text
146 my $t = C4::TmplToken->new( $text, C4::TmplTokenType::TAG, $line, $self->{filename});
147 my %attr;
148 # tags seem to be uses in an 'interesting' way elsewhere..
149 for my $key( %$hash ) {
150 next unless defined $hash->{$key};
151 if ($key eq "/"){
152 $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 1 ];
154 else {
155 $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 0 ];
158 $t->set_attributes( \%attr );
159 push @tokens, $t;
162 #handle closing html tags
163 sub end{
164 my $self = shift;
165 my $line = shift;
166 my $tag = shift;
167 my $hash = shift;
168 my $text = shift;
169 # what format should this be in?
170 my $t = C4::TmplToken->new( $text, C4::TmplTokenType::TAG, $line, $self->{filename} );
171 my %attr;
172 # tags seem to be uses in an 'interesting' way elsewhere..
173 for my $key( %$hash ) {
174 next unless defined $hash->{$key};
175 $attr{+lc($key)} = [ $key, $hash->{$key}, $key."=".$hash->{$key}, 0 ];
177 $t->set_attributes( \%attr );
178 push @tokens, $t;