* Andy Wardley <[EMAIL PROTECTED]> [2002-12-18 04:52]:
> Tatsuhiko Miyagawa wrote:
> > So why not adding this filter as 'uri_full' or something, to TT itself?
>
> I borrowed the code from URI::Escape which doesn't escape the '&' on the
> premise that it could be a valid character in a URI.
Out of curiosity, why does uri_filter implement the whole of uri_escape?
It could be made into a dynamic filter, and accept an optional
pattern. Patch attached. It modifies lib/Template/Filters.pm and
t/filters.t.
(darren)
--
Violence is to dictatorship as propaganda is to democracy.
-- Noam Chomsky
Index: t/filter.t
===================================================================
RCS file: /template-toolkit/Template2/t/filter.t,v
retrieving revision 2.18
diff -u -r2.18 filter.t
--- t/filter.t 2002/11/01 18:57:38 2.18
+++ t/filter.t 2002/12/18 15:31:25
@@ -900,6 +900,11 @@
guitar&amp;file.html
-- test --
+[% "foo" | uri("f") %]
+-- expect --
+%66oo
+
+-- test --
[% 'foobar' | ucfirst %]
-- expect --
Foobar
Index: lib/Template/Filters.pm
===================================================================
RCS file: /template-toolkit/Template2/lib/Template/Filters.pm,v
retrieving revision 2.65
diff -u -r2.65 Filters.pm
--- lib/Template/Filters.pm 2002/11/04 19:45:59 2.65
+++ lib/Template/Filters.pm 2002/12/18 15:31:26
@@ -51,7 +51,6 @@
'html_break' => \&html_para_break,
'html_para_break' => \&html_para_break,
'html_line_break' => \&html_line_break,
- 'uri' => \&uri_filter,
'upper' => sub { uc $_[0] },
'lower' => sub { lc $_[0] },
'ucfirst' => sub { ucfirst $_[0] },
@@ -78,6 +77,7 @@
'file' => [ \&redirect_filter_factory, 1 ], # alias
'stdout' => [ \&stdout_filter_factory, 1 ],
'latex' => [ \&latex_filter_factory, 1 ],
+ 'uri' => [ \&uri_filter, 1 ],
};
# name of module implementing plugin filters
@@ -268,8 +268,9 @@
# URI::Escape is Copyright 1995-2000 Gisle Aas.
#------------------------------------------------------------------------
+my %subst;
sub uri_filter {
- my $text = shift;
+ my ($context, $patn) = @_;
# construct and cache a lookup table for escapes (faster than
# doing a sprintf() for every character in every string each
@@ -277,9 +278,27 @@
$URI_ESCAPES ||= {
map { ( chr($_), sprintf("%%%02X", $_) ) } (0..255),
};
-
- $text =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$URI_ESCAPES->{$1}/g;
- $text;
+
+ if (defined $patn) {
+ unless (exists $subst{$patn}) {
+ # Because we can't compile the regex we fake it with a cached sub
+ (my $tmp = $patn) =~ s,/,\\/,g;
+ eval "\$subst{\$patn} = sub {\$_[0] =~ s/([$tmp])/\$URI_ESCAPES->{\$1}/g;
+}";
+ $context->throw('uri_filter', $@) if ($@);
+ }
+ }
+
+ return sub {
+ my $text = shift;
+
+ if (defined $patn) {
+ &{$subst{$patn}}($text);
+ }
+ else {
+ $text =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$URI_ESCAPES->{$1}/g;
+ }
+ $text;
+ };
}
@@ -1407,4 +1426,4 @@
=head1 SEE ALSO
-L<Template|Template>, L<Template::Context|Template::Context>,
L<Template::Manual::Filters|Template::Manual::Filters>
\ No newline at end of file
+L<Template|Template>, L<Template::Context|Template::Context>,
+L<Template::Manual::Filters|Template::Manual::Filters>