Bugzilla – Attachment 77808 Details for
Bug 13618
Add additional template filter methods and a filter presence test to Koha
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 13618: [DO NOT PUSH] Add script to add the html filters
Bug-13618-DO-NOT-PUSH-Add-script-to-add-the-html-f.patch (text/plain), 7.55 KB, created by
Jonathan Druart
on 2018-08-14 21:14:08 UTC
(
hide
)
Description:
Bug 13618: [DO NOT PUSH] Add script to add the html filters
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2018-08-14 21:14:08 UTC
Size:
7.55 KB
patch
obsolete
>From 54b44a3973d3718dedb3a4db6246010e3ccae6e7 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Thu, 19 Jul 2018 15:53:47 -0300 >Subject: [PATCH] Bug 13618: [DO NOT PUSH] Add script to add the html filters > >Usage: perl add_html_filters.pl **/*.inc **/*.tt >It also takes --test for debugging > >Then: >git grep Asset|grep -v USE | grep -v '| $raw' >=> We should deal with them on a separate commit > >Signed-off-by: Owen Leonard <oleonard@myacpl.org> > >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >--- > add_html_filters.pl | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 207 insertions(+) > create mode 100644 add_html_filters.pl > >diff --git a/add_html_filters.pl b/add_html_filters.pl >new file mode 100644 >index 0000000000..2f108ada73 >--- /dev/null >+++ b/add_html_filters.pl >@@ -0,0 +1,207 @@ >+use Modern::Perl; >+use Text::WordDiff; >+my @tt_directives = ( >+ qr{^\s*INCLUDE}, >+ qr{^\s*USE}, >+ qr{^\s*IF}, >+ qr{^\s*UNLESS}, >+ qr{^\s*ELSE}, >+ qr{^\s*ELSIF}, >+ qr{^\s*END}, >+ qr{^\s*SET}, >+ qr{^\s*FOR}, >+ qr{^\s*FOREACH}, >+ qr{^\s*MACRO}, >+ qr{^\s*SWITCH}, >+ qr{^\s*CASE}, >+ qr{^\s*PROCESS}, >+ qr{^\s*DEFAULT}, >+ qr{^\s*TRY}, >+ qr{^\s*CATCH}, >+ qr{^\s*BLOCK}, >+ qr{^\s*FILTER}, >+ qr{^\s*STOP}, >+); >+use File::Slurp; >+my @files = @ARGV; >+my $test_mode = grep{/--test/} @files; >+ >+if ( $test_mode ) { >+ my $input = input_for_test(); >+ my $expected = expected_for_test(); >+ my @lines = process_tt_content($input); >+ my $output = join "\n", @lines; >+ $output .= "\n"; # Why? >+ if ( $output ne $expected ) { >+ say "KO"; >+ say word_diff(\$expected, \$output); >+ } else { >+ say "OK"; >+ } >+} else { >+ for my $file ( @ARGV ) { >+ next if $file eq '--test'; # This is really dirty but who cares? >+ say $file; >+ my $content = read_file($file); >+ my @lines = process_tt_content($content); >+ my $output = join "\n", @lines; >+ $output .= "\n"; >+ write_file($file, $output); >+ } >+} >+sub process_tt_content { >+ my ( $content ) = @_; >+ my ( @new_lines, $i, $use_raw ); >+ for my $line (split "\n", $content ) { >+ my $new_line = $line; >+ if ( $line =~ m{\[%[^%]+%\]} ) { >+ # handle exceptions first >+ if ( $line =~ m{^\s*\[% Asset} ) { >+ $use_raw = 1; >+ $new_line =~ s{\[% Asset\.(.*)\.raw %\]}{[% Asset.$1 | \$raw %]}; >+ #$new_line =~ s{\[% Asset\.(.*)\.raw %\]}{[% Asset.$1 %]}; >+ push @new_lines, $new_line; >+ next; >+ } >+ >+ while ( $line =~ m{ >+ \[% >+ (?<pre_chomp>(\s|\-|~)*) >+ (?<tt_block>[^%\-~]+) >+ (?<post_chomp>(\s|\-|~)*) >+ %\]}gmxs ) { >+ my $tt_block = $+{tt_block}; >+ my $pre_chomp = $+{pre_chomp}; >+ my $post_chomp = $+{post_chomp}; >+ if ( $pre_chomp and $pre_chomp =~ m|-| ) { >+ $pre_chomp = '- '; >+ } elsif ( $pre_chomp and $pre_chomp =~ m|~| ) { >+ $pre_chomp = '~ '; >+ } else { >+ $pre_chomp = ' '; >+ } >+ if ( $post_chomp and $post_chomp =~ m|-| ) { >+ $post_chomp = ' -'; >+ } elsif ( $post_chomp and $post_chomp =~ m|~| ) { >+ $post_chomp = ' ~'; >+ } else { >+ $post_chomp = ' '; >+ } >+ >+ if ( $tt_block =~ m{^(?<before>\S+)\s+UNLESS\s+(?<after>\S+)} ) { # Specific for [% foo UNLESS bar %] >+ my $before = $+{before}; >+ my $after = $+{after}; >+ $new_line =~ s{$before\s+UNLESS\s+$after}{$before | html UNLESS $after}; >+ next; >+ } >+ next if grep {$tt_block =~ $_} @tt_directives; >+ >+ next if $tt_block =~ m{\s?\|\s?\$KohaDates\s?$}; # We could escape it but should be safe >+ next if $tt_block =~ m{^\#}; # Is a comment, skip it >+ >+ if ( $tt_block =~ m{\.raw} ) { >+ $use_raw = 1; >+ $new_line =~ s{\s*\.raw\s*%\]}{ | \$raw$post_chomp%]}; >+ #$new_line =~ s{\s*\.raw\s*%\]}{ $post_chomp%]}; >+ } >+ elsif ( $tt_block =~ m{\|\s?\$raw} ) { # Already escaped correctly with raw >+ } >+ elsif ( $tt_block =~ m{\|\s?ur(l|i)} ) { # has url or uri filter, do nothing >+ } >+ elsif ( $tt_block !~ m{\|\s?html} ) { # Do not have the html filter >+ $tt_block =~ s/^\s*|\s*$//g; # trim >+ $new_line =~ s{ >+ \[% >+ \s*$pre_chomp\s* >+ \Q$tt_block\E >+ \s*$post_chomp\s* >+ %\] >+ }{[%$pre_chomp$tt_block | html$post_chomp%]}xms; >+ } >+ } >+ } >+ push @new_lines, $new_line; >+ #last if $i++ > 10; >+ } >+ >+ # Adding [% USE raw %] on top if the filter is used >+ @new_lines = ( '[% USE raw %]', @new_lines ) >+ if $use_raw >+ and not $content =~ m{\[% USE raw %\]}; >+ >+ return @new_lines; >+} >+ >+sub input_for_test { >+ return <<INPUT; >+[% USE Asset %] >+[% INCLUDE 'doc-head-open.inc' %] >+<title>Koha › Patrons › >+ [% UNLESS blocking_error %] >+ Patron details for [% INCLUDE 'patron-title.inc' no_html = 1 %] >+ [% just_a_var %] A N D [% another_one_on_same_line %] >+ [% just_a_var_filtered |html %] >+ [% END %] >+ [% IF ( patron.othernames | html ) %]“[% patron.othernames %]”[% END %] >+ [% Asset.css("css/datatables.css").raw %] >+ [% Asset.css("lib/jquery/jquery-ui-1.11.4.min.css").raw %] >+</title> >+<a href="tel:[% patron.phone %]">[% patron.phone %]</a> >+<a title="[% patron.emailpro %]" href="mailto:[% patron.emailpro | url %]">[% patron.emailpro %]</a> >+[% patron_message.get_column('manager_surname') %] >+[%# do_nothing %] >+[% # do_nothing %] >+[% SWITCH var %] >+[% CASE 'foo' %]foo >+[% CASE %] >+[% END %] >+[%- SWITCH var -%] >+[%- CASE 'foo' -%]foo >+[%- CASE -%] >+[%- END -%] >+[%- var -%] >+[% - var - %] >+[%~ var ~%] >+[% ~ var ~ %] >+[% var | \$raw %] >+[% foo UNLESS bar %] >+[%END%] >+INPUT >+} >+sub expected_for_test { >+ return <<EXPECTED; >+[% USE raw %] >+[% USE Asset %] >+[% INCLUDE 'doc-head-open.inc' %] >+<title>Koha › Patrons › >+ [% UNLESS blocking_error %] >+ Patron details for [% INCLUDE 'patron-title.inc' no_html = 1 %] >+ [% just_a_var | html %] A N D [% another_one_on_same_line | html %] >+ [% just_a_var_filtered |html %] >+ [% END %] >+ [% IF ( patron.othernames | html ) %]“[% patron.othernames | html %]”[% END %] >+ [% Asset.css("css/datatables.css") | \$raw %] >+ [% Asset.css("lib/jquery/jquery-ui-1.11.4.min.css") | \$raw %] >+</title> >+<a href="tel:[% patron.phone | html %]">[% patron.phone | html %]</a> >+<a title="[% patron.emailpro | html %]" href="mailto:[% patron.emailpro | url %]">[% patron.emailpro | html %]</a> >+[% patron_message.get_column('manager_surname') | html %] >+[%# do_nothing %] >+[% # do_nothing %] >+[% SWITCH var %] >+[% CASE 'foo' %]foo >+[% CASE %] >+[% END %] >+[%- SWITCH var -%] >+[%- CASE 'foo' -%]foo >+[%- CASE -%] >+[%- END -%] >+[%- var | html -%] >+[%- var | html -%] >+[%~ var | html ~%] >+[%~ var | html ~%] >+[% var | \$raw %] >+[% foo | html UNLESS bar %] >+[%END%] >+EXPECTED >+} >-- >2.11.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 13618
:
35488
|
35516
|
40480
|
40481
|
40482
|
40483
|
40484
|
40485
|
40487
|
41346
|
41347
|
41348
|
41349
|
41350
|
41351
|
41352
|
41353
|
41354
|
41355
|
41746
|
41747
|
41776
|
42009
|
42054
|
42072
|
42073
|
42075
|
42078
|
42079
|
42087
|
42090
|
42091
|
42092
|
42093
|
42094
|
42095
|
42096
|
42097
|
45141
|
45784
|
45785
|
45788
|
46767
|
46768
|
46769
|
46770
|
46771
|
46772
|
46773
|
46774
|
46775
|
46776
|
46777
|
46778
|
46779
|
46780
|
46781
|
46782
|
46783
|
46784
|
46785
|
46786
|
46787
|
46788
|
46789
|
46790
|
46791
|
46792
|
46793
|
46794
|
46795
|
46796
|
46797
|
46798
|
46799
|
46800
|
46801
|
46802
|
46803
|
46804
|
46805
|
46806
|
46807
|
46808
|
46809
|
46810
|
46811
|
46812
|
46813
|
46814
|
46815
|
46816
|
46817
|
46818
|
46819
|
46820
|
46821
|
46822
|
47425
|
47426
|
47427
|
47428
|
47429
|
47430
|
47431
|
47432
|
47433
|
47434
|
47435
|
47436
|
47437
|
47438
|
47439
|
47440
|
47441
|
47442
|
47443
|
47444
|
47445
|
47446
|
47447
|
47448
|
47449
|
47450
|
47451
|
47452
|
47631
|
59983
|
59984
|
59987
|
77804
|
77805
|
77806
|
77807
| 77808 |
77809
|
77997
|
77998
|
78158
|
78274
|
78415
|
79404
|
79818
|
81183
|
83479
|
83480