Template::Tiny support for text filtering
markie [Mon, 22 Feb 2010 04:36:56 +0000 (20:36 -0800)]
lib/HTML/Zoom/FilterBuilder/Template.pm [new file with mode: 0644]

diff --git a/lib/HTML/Zoom/FilterBuilder/Template.pm b/lib/HTML/Zoom/FilterBuilder/Template.pm
new file mode 100644 (file)
index 0000000..ee69983
--- /dev/null
@@ -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;