1 package t::lib::QA::TemplateFilters;
31 return _process_tt_content( @_ )->{new_content};
35 return @{_process_tt_content( @_ )->{errors}};
39 sub _process_tt_content {
41 my ( $use_raw, $has_use_raw );
45 for my $line ( split "\n", $content ) {
48 if ( $line =~ m{\[%[^%]+%\]} ) {
50 # handle exceptions first
51 if ( $line =~ m{\|\s*\$raw} ) { # Is the file use the raw filter?
55 # Do we have Asset without the raw filter?
56 if ( $line =~ m{^\s*\[% Asset} && $line !~ m{\|\s*\$raw} ) {
59 error => 'asset_must_be_raw',
61 line_number => $line_number
63 $new_line =~ s/\)\s*%]/) | \$raw %]/;
65 push @new_lines, $new_line;
70 if $line =~ m{\[%(\s|-|~)*USE raw(\s|-|~)*%\]}; # Does [% Use raw %] exist?
73 if ( $line =~ qr{<a href="([^"]+)} ) {
74 my $to_uri_escape = $1;
78 (?<pre_chomp>(\s|\-|~)*)
80 (?<post_chomp>(\s|\-|~)*)
84 ( $new_line, $e ) = process_tt_block($new_line, { %+, filter => 'uri' });
85 push @errors, { line => $line, line_number => $line_number, error => $e } if $e;
93 (?<pre_chomp>(\s|\-|~)*)
95 (?<post_chomp>(\s|\-|~)*)
99 ( $new_line, $e ) = process_tt_block($new_line, \%+);
100 push @errors, { line => $line, line_number => $line_number, error => $e } if $e;
103 push @new_lines, $new_line;
106 push @new_lines, $new_line;
111 # Adding [% USE raw %] on top if the filter is used
112 @new_lines = ( '[% USE raw %]', @new_lines )
113 if $use_raw and not $has_use_raw;
115 my $new_content = join "\n", @new_lines;
116 return { errors => \@errors, new_content => $new_content };
119 sub process_tt_block {
120 my ( $line, $params ) = @_;
121 my $tt_block = $params->{tt_block};
122 my $pre_chomp = $params->{pre_chomp};
123 my $post_chomp = $params->{post_chomp};
124 my $filter = $params->{filter} || 'html';
127 return ( $line, $error ) if
128 # It's a TT directive, no filters needed
129 grep { $tt_block =~ $_ } @tt_directives
132 or $tt_block =~ m{^\#}
134 # Already escaped with a special filter
135 # We could escape it but should be safe
136 or $tt_block =~ m{\s?\|\s?\$KohaDates\s?$}
137 or $tt_block =~ m{\s?\|\s?\$Price\s?$}
139 # Already escaped correctly with raw
140 or $tt_block =~ m{\|\s?\$raw}
142 # Assignment, maybe we should require to use SET (?)
145 # Already has url or uri filter
146 or $tt_block =~ m{\|\s?ur(l|i)}
148 # Specific for [% foo UNLESS bar %]
149 or $tt_block =~ m{^(?<before>\S+)\s+UNLESS\s+(?<after>\S+)}
162 ? $post_chomp =~ m|-|
164 : $post_chomp =~ m|~|
170 # Use the uri filter is needed
171 # If html filtered or not filtered
175 or $tt_block =~ m{\|\s?html}
176 or $tt_block !~ m{\s*|\s*(uri|url)}
179 $tt_block =~ s/^\s*|\s*$//g; # trim
180 $tt_block =~ s/\s*\|\s*html\s*//;
184 \Q$tt_block\E(\s*\|\s*html)?
187 }{[%$pre_chomp$tt_block | uri$post_chomp%]}xms;
189 $error = 'wrong_html_filter';
192 $tt_block !~ m{\|\s?html} # already has html filter
195 $tt_block =~ s/^\s*|\s*$//g; # trim
202 }{[%$pre_chomp$tt_block | html$post_chomp%]}xms;
204 $error = 'missing_filter';
206 return ( $line, $error );
213 t::lib::QA::TemplateFilters - Module used by tests and QA script to catch missing filters in template files
217 my $content = read_file($filename);
218 my $new_content = t::lib::QA::TemplateFilters::fix_filters($content);
219 my $errors = t::lib::QA::TemplateFilters::missing_filters($content);
223 The goal of this module is to make the subroutine reusable from the QA scripts
224 and to not duplicate the code.
230 Take a template content file in parameter and return the same content with
231 the correct (guessed) filters.
232 It will also add the [% USE raw %] statement if it is needed.
234 =head2 missing_filters
236 Take a template content file in parameter and return an arrayref of errors.
238 An error is a hashref with 3 keys, error and line, line_number.
240 asset_must_be_raw - When Asset is called without using raw
241 missing_filter - When a TT variable is displayed without filter
242 wrong_html_filter - When a TT variable is using the html filter when uri (or url)
243 should be used instead.
245 * line is the line where the error has been found.
246 * line_number is the line number where the error has been found.
251 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
255 Copyright 2017 - Koha Development Team
259 This file is part of Koha.
261 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
262 the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
264 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
266 You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.