Revision 1177

Date:
2008/11/13 08:48:16
Author:
abw
Revision Log:
Fixed Apache::Util(s) typo in html_entity filter and added use_html_entities() and use_apache_util() methods to Template::Filters
Files:

Legend:

 
Added
 
Removed
 
Modified
  • trunk/Changes

     
    11 11 #========================================================================
    12 12
    13 13 #------------------------------------------------------------------------
    14 # Version 2.21 - Not yet released
    14 # Version 2.21 - Not yet officially released - currently at 2.20_01
    15 15 #------------------------------------------------------------------------
    16 16
    17 17 * Tweaked Template::Parser to work better with the ANYCASE option. It
     
    20 20 LAST keyword. Thanks to Sean McAfee for the post that inspired it.
    21 21 http://lists.tt2.org/pipermail/templates/2008-September/010462.html
    22 22
    23 * Fixed a broken test for Apache::Util in the html_entity filter. Added
    24 the use_html_entities() and use_apache_util() class methods to
    25 Template::Filters to allow end-user selection of one or the other.
    26 http://rt.cpan.org/Public/Bug/Display.html?id=40870
    27 http://template-toolkit.org/svnweb/Template2/revision/?rev=1177
    23 28
    29
    24 30 #------------------------------------------------------------------------
    25 31 # Version 2.20 - 13th August 2008
    26 32 #------------------------------------------------------------------------
  • trunk/lib/Template/Filters.pm

     
    377 377 # modules can't be located.
    378 378 #------------------------------------------------------------------------
    379 379
    380 sub use_html_entities {
    381 require HTML::Entities;
    382 return ($AVAILABLE->{ HTML_ENTITY } = \&HTML::Entities::encode_entities);
    383 }
    384
    385 sub use_apache_util {
    386 require Apache::Util;
    387 Apache::Util::escape_html(''); # TODO: explain this
    388 return ($AVAILABLE->{ HTML_ENTITY } = \&Apache::Util::escape_html);
    389 }
    390
    380 391 sub html_entity_filter_factory {
    381 392 my $context = shift;
    382 393 my $haz;
    383 394
    384 395 # if Apache::Util is installed then we use escape_html
    385 396 $haz = $AVAILABLE->{ HTML_ENTITY }
    386 ||= eval {
    387 require Apache::Util;
    388 Apache::Utils::escape_html('');
    389 \&Apache::Util::escape_html
    390 }
    391 || eval {
    392 require HTML::Entities;
    393 \&HTML::Entities::encode_entities
    394 }
    397 || eval { use_apache_util() }
    398 || eval { use_html_entities() }
    395 399 || -1; # we use -1 for "not available" because it's a true value
    396 400
    397 401 return ref $haz eq 'CODE'
     
    687 691 When the C<TOLERANT> option is set, errors are automatically downgraded to
    688 692 a C<STATUS_DECLINE> response.
    689 693
    694 =head2 use_html_entities()
    695
    696 This class method can be called to configure the C<html_entity> filter to use
    697 the L<HTML::Entities> module. An error will be raised if it is not installed
    698 on your system.
    699
    700 use Template::Filters;
    701 Template::Filters->use_html_entities();
    702
    703 =head2 use_apache_util()
    704
    705 This class method can be called to configure the C<html_entity> filter to use
    706 the L<Apache::Util> module. An error will be raised if it is not installed on
    707 your system.
    708
    709 use Template::Filters;
    710 Template::Filters->use_apache_util();
    711
    690 712 =head1 CONFIGURATION OPTIONS
    691 713
    692 714 The following list summarises the configuration options that can be provided
  • trunk/lib/Template/Manual/Filters.pod

     
    120 120 range of HTML entities that your text may contain. The C<html_entity>
    121 121 filter uses either the C<Apache::Util> module (which is written in C and
    122 122 is therefore faster) or the C<HTML::Entities> module (written in Perl but
    123 equally as comprehensive) to perform the encoding. If one or other of
    124 these modules are installed on your system then the text will be
    125 encoded (via the C<escape_html()> or C<encode_entities()> subroutines
    126 respectively) to convert all extended characters into their
    127 appropriate HTML entities (e.g. converting 'C<é>' to 'C<&eacute;>'). If
    128 neither module is available on your system then an 'C<html_entity>' exception
    129 will be thrown reporting an appropriate message.
    123 equally as comprehensive) to perform the encoding.
    130 124
    125 If one or other of these modules are installed on your system then the text
    126 will be encoded (via the C<escape_html()> or C<encode_entities()> subroutines
    127 respectively) to convert all extended characters into their appropriate HTML
    128 entities (e.g. converting 'C<é>' to 'C<&eacute;>'). If neither module is
    129 available on your system then an 'C<html_entity>' exception will be thrown
    130 reporting an appropriate message.
    131
    132 If you want to force TT to use one of the above modules in preference to
    133 the other, then call either of the L<Template::Filters> class methods:
    134 L<use_html_entities()|Template::Filters/use_html_entities()> or
    135 L<use_apache_util()|Template::Filters/use_apache_util()>.
    136
    137 use Template::Filters;
    138 Template::Filters->use_html_entities;
    139
    131 140 For further information on HTML entity encoding, see
    132 141 L<http://www.w3.org/TR/REC-html40/sgml/entities.html>.
    133 142
  • trunk/t/html.t

     
    30 30 # behaviour of html filter depends on these being available
    31 31 #------------------------------------------------------------------------
    32 32
    33 use constant HAS_HTML_Entities => eval { require HTML::Entities };
    34 use constant HAS_Apache_Util => eval { require Apache::Util;
    35 Apache::Utils::escape_html(''); };
    33 use constant HAS_HTML_Entities => eval {
    34 require HTML::Entities;
    35 1;
    36 };
    37 use constant HAS_Apache_Util => eval {
    38 require Apache::Util;
    39 Apache::Utils::escape_html('');
    40 1;
    41 };
    36 42
    43 #print "Has HTML::Entities: ", HAS_HTML_Entities ? 'yes' : 'no', "\n";
    44 #print "Has Apache::Util: ", HAS_Apache_Util ? 'yes' : 'no', "\n";
    37 45
    38 46 my $html = -d 'templates' ? 'templates/html' : '../templates/html';
    39 47 die "cannot grok templates/html directory\n" unless -d $html;
    40 48
    41 49 my $h = Template::Plugin::HTML->new('foo');
    42 ok( $h );
    50 ok( $h, 'created HTML plugin' );
    43 51
    44 52 my $cfg = {
    45 53 INCLUDE_PATH => $html,
     
    69 77
    70 78 -- test --
    71 79 -- name html entity --
    72 [% TRY;
    73 text =
    74 "Léon Brocard" | html_entity;
    75 CATCH;
    76 error;
    77 END;
    78 "passed" IF text == "L&eacute;on Brocard";
    79 "passed" IF text == "L&#233;on Brocard";
    80 [% TRY;
    81 text = "Léon Brocard" | html_entity;
    82
    83 IF text == "L&eacute;on Brocard";
    84 'passed';
    85 ELSIF text == "L&#233;on Brocard";
    86 'passed';
    87 ELSE;
    88 "failed: $text";
    89 END;
    90 CATCH;
    91 error;
    92 END;
    80 93 %]
    81 94 -- expect --
    82 95 -- process --