From: markie Date: Mon, 22 Feb 2010 04:36:56 +0000 (-0800) Subject: Template::Tiny support for text filtering X-Git-Tag: release_0.009004~70 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=9e21970968c8cf92b343e8d877bbe099334cba5b Template::Tiny support for text filtering --- diff --git a/lib/HTML/Zoom/FilterBuilder/Template.pm b/lib/HTML/Zoom/FilterBuilder/Template.pm new file mode 100644 index 0000000..ee69983 --- /dev/null +++ b/lib/HTML/Zoom/FilterBuilder/Template.pm @@ -0,0 +1,44 @@ +package HTML::Zoom::FilterBuilder; + +use strict; +use warnings FATAL => 'all'; +use Template::Tiny; + +sub _template_object { + shift->{_template_object} ||= Template::Tiny->new; +} + +sub template_text { + my ($self, $vars) = @_; + my $parser = $self->_zconfig->parser; + my $tt = $self->_template_object; + $self->collect({ + filter => sub { + $_->map(sub { + return $_ unless $_->{type} eq 'TEXT'; + my $unescape = $parser->html_unescape($_->{raw}); + $tt->process(\$unescape, $vars, \my $out); + return { %$_, raw => $parser->html_escape($out) } + }) + }, + passthrough => 1, + }) +} + +sub template_text_raw { + my ($self, $vars) = @_; + my $tt = $self->_template_object; + my $parser = $self->_zconfig->parser; + $self->collect({ + filter => sub { + $_->map(sub { + return $_ unless $_->{type} eq 'TEXT'; + $tt->process(\($_->{raw}), $vars, \my $out); + return { %$_, raw => $out } + }) + }, + passthrough => 1, + }) +} + +1;