ee699835cb1304ef89d73aafe671dffa3615f3ae
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder / Template.pm
1 package HTML::Zoom::FilterBuilder;
2
3 use strict;
4 use warnings FATAL => 'all';
5 use Template::Tiny;
6
7 sub _template_object {
8   shift->{_template_object} ||= Template::Tiny->new;
9 }
10
11 sub template_text {
12   my ($self, $vars) = @_;
13   my $parser = $self->_zconfig->parser;
14   my $tt = $self->_template_object;
15   $self->collect({
16     filter => sub {
17       $_->map(sub {
18         return $_ unless $_->{type} eq 'TEXT';
19         my $unescape = $parser->html_unescape($_->{raw});
20         $tt->process(\$unescape, $vars, \my $out);
21         return { %$_, raw => $parser->html_escape($out) }
22       })
23     },
24     passthrough => 1,
25   })
26 }
27
28 sub template_text_raw {
29   my ($self, $vars) = @_;
30   my $tt = $self->_template_object;
31   my $parser = $self->_zconfig->parser;
32   $self->collect({
33     filter => sub {
34       $_->map(sub {
35         return $_ unless $_->{type} eq 'TEXT';
36         $tt->process(\($_->{raw}), $vars, \my $out);
37         return { %$_, raw => $out }
38       })
39     },
40     passthrough => 1,
41   })
42 }
43
44 1;